【问题标题】:Javascript with custom Google Map markers / drop animation带有自定义谷歌地图标记/拖放动画的Javascript
【发布时间】:2011-07-22 00:48:41
【问题描述】:

我是 JS 的新手,但我希望自定义地图能够正常工作。我编写了两个渲染地图的脚本:One with the drop animation (maptest1)、one with the custom markers (maptest2)。

就个人而言,它们都工作得很好,但我似乎无法弄清楚如何实现动画和自定义标记。我知道drop 和addMarker 函数正在maptest1 中制作动画。我试图将这些功能与我在maptest2 中的代码集成,但我似乎无法使其工作。

任何见解将不胜感激!

ma​​ptest1:(放置动画)

var locations = [
  new google.maps.LatLng(47.578033, -122.288116, 1),
  new google.maps.LatLng(47.675625, -122.304115, 2),
  new google.maps.LatLng(47.681580, -122.355278, 3)
];
var markers = [];
var iterator = 0;
function initialize() {
  drop();
  var mapOptions = {
    zoom: 12,
    panControl: false,
    zoomControl: true,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: new google.maps.LatLng(47.63,-122.33),
  };
  map = new google.maps.Map(document.getElementById("map_canvas"),
          mapOptions);
}
function drop() {
  for (var i = 0; i < locations.length; i++) {
    setTimeout(function() {
      addMarker();
    }, i * 200);
  }
}
function addMarker() {
  markers.push(new google.maps.Marker({
    position: locations[iterator],
    map: map,
    draggable: false,
    animation: google.maps.Animation.DROP
  }));
  iterator++;
}

ma​​ptest2:(自定义标记)

function initialize() {
  var mapOptions = {
    zoom: 12,
    panControl: false,
    zoomControl: true,
    scaleControl: false,
    streetViewControl: false,
   overviewMapControl: false,
    center: new google.maps.LatLng(47.63,-122.33),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
  setMarkers(map, studios);
}
var studios = [
  ['Mount Baker', 47.578033, -122.288116, 3],
  ['Ravenna', 47.675625, -122.304115, 2],
  ['Phinney', 47.681580, -122.355278, 1]
];

function setMarkers(map, locations) {
  var image = new google.maps.MarkerImage('mapimages/image.png',
      new google.maps.Size(30, 52),
      new google.maps.Point(0,0),
      new google.maps.Point(15, 52));
  var shadow = new google.maps.MarkerImage('mapimages/shadow.png',
      new google.maps.Size(60, 52),
      new google.maps.Point(0,0),
      new google.maps.Point(15, 52));
  var shape = {
      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
    var studio = locations[i];
    var myLatLng = new google.maps.LatLng(studio[1], studio[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        title: studio[0],
        zIndex: studio[3]
    });
  }
}

编辑

If anyone is wondering, using Ryan's additions to the script here it working currently.

【问题讨论】:

    标签: javascript html google-maps animation google-maps-api-3


    【解决方案1】:

    您可以在 addMarker() 中将其添加到 maptest1.html

    var image = new google.maps.MarkerImage('mapimages/image.png',
        new google.maps.Size(30, 52),
        new google.maps.Point(0,0),
        new google.maps.Point(15, 52));
    var shadow = new google.maps.MarkerImage('mapimages/shadow.png',
        new google.maps.Size(60, 52),
        new google.maps.Point(0,0),
        new google.maps.Point(15, 52));
    var shape = {
        coord: [1, 1, 1, 20, 18, 20, 18 , 1],
        type: 'poly'
    };
    

    并改变这个:

    markers.push(new google.maps.Marker({
        position: locations[iterator],
        map: map,
        draggable: false,
        animation: google.maps.Animation.DROP
      }));
    

    到这里:

    markers.push(new google.maps.Marker({
        position: locations[iterator],
        map: map,
        draggable: false,
        animation: google.maps.Animation.DROP,
        shadow: shadow,
        icon: image,
        shape: shape,
      }));
    

    我认为会奏效。

    【讨论】:

    • 谢谢,成功了!你太棒了。我试图解决这个问题 2 小时!
    • 没问题,有时需要第二双眼睛才能弄清楚:)
    • shape 后面的最后一个逗号:shape,是多余的,可能会导致 IE 出错。
    • @Ryan 我真的感谢您的意见并提供了完美的解决方案。我用后续行动更新了 OP。我不想在这里滥用你的慷慨,所以没有压力。
    • @AndrewR :创建一个新问题,我宁愿将问题分开,这样以后阅读本文的人就不会感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2011-03-15
    • 2013-05-20
    • 2015-01-28
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多