2014-11-14 107 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