2014-11-14 105 views
0

您好我有使用我的地图接触这个代码,我想添加2个多的地方就可以了,你可以帮 我请:GMAP多个标记

/* --- Google Map --- */ 
    var mapOptions = { 
    center: new google.maps.LatLng(49.5564021,5.8628159), 
    zoom: 15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions); 
    var image = "img/marker.png"; 
    var marker = new google.maps.Marker({ 
    position: mapOptions.center, 
    map: map, 
    icon: image 
    }); 

回答

0

您需要定义你的第二个位置和第三标记,并添加它们,就像您添加的第一个

var image = "img/marker.png"; 
    var marker = new google.maps.Marker({ 
    position: mapOptions.center, 
    map: map, 
    icon: image 
    }); 

    var position2={lat:49.555,lng:5.861}; 
    var marker2 = new google.maps.Marker({ 
    position: position2, 
    map: map, 
    icon: image 
    }); 

    var position3={lat:49.557,lng:5.863}; 
    var marker3 = new google.maps.Marker({ 
    position: position3, 
    map: map, 
    icon: image 
    }); 

,当然,还有改进的空间,如果你在一个循环中添加标记或声明功能encapsule该任务。

+0

认为你的作品 – 2014-11-14 14:43:20

+0

我很高兴它为你工作。您介意将我的答案标记为已接受吗? – amenadiel 2014-11-14 14:58:08

-2

我使用此函数向地图添加其他标记: 项目是我想要添加到地图中的自定义对象(本例中为商店的细节)。

我相信你可以从这里找到你所需要的。

function _setDealer (item) { 

    //the position of the marker 
    var myLatlng = new google.maps.LatLng(item.Latitude, item.Longitude), 
     markerOptions = { 
      animation: google.maps.Animation.DROP, 
      position: myLatlng, 
      //your map instance 
      map: map, 
      country: { 
       name: (item.Country ? item.Country.Name : ''), 
       code: (item.Country ? item.Country.Code : '') 
      }, 
      geocoding: { 
       coordinate: { 
        latitude: (item.Geocoding ? item.Geocoding.Coordinate.Latitude : false), 
        longitude: (item.Geocoding ? item.Geocoding.Coordinate.Longitude : false) 
       }, 
       state: (item.Geocoding ? item.Geocoding.State : false) 
      } 
     }, 
     classification = item.Classification.toLowerCase(), 
     image = new google.maps.MarkerImage(
      mapOptions.mapIcons[classification].normal.filename, 
      new google.maps.Size(mapOptions.mapIcons[classification].normal.size[0], mapOptions.mapIcons[classification].normal.size[1]), 
      new google.maps.Point(0, 0), 
      new google.maps.Point(mapOptions.mapIcons[classification].normal.center[0], mapOptions.mapIcons[classification].normal.center[1]) 
     ), 
     marker = new google.maps.Marker(markerOptions); 

    marker.setIcon(image); 

} 
+0

我相信这会起作用。然而,当你只想打印标记时,弄清楚你的代码在做什么有点过头了。为什么你不简化它,所以它可能适合一般情况? – amenadiel 2014-11-14 14:47:21

+1

是的你是对的,我只是从我做过一次的项目复制/粘贴它。 其他答案(你的)也是正确的..使用你喜欢的。 :) – 2014-11-14 14:59:12

+0

你的答案可能仍然是有用的封装标记添加,所以你不必重复自己。 – amenadiel 2014-11-14 15:00:18