Javascript 多标记Gmap
嗨,我有这个代码用于我的地图联系人,我想在上面再添加两个位置,你能帮我吗 请告诉我:Javascript 多标记Gmap,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,嗨,我有这个代码用于我的地图联系人,我想在上面再添加两个位置,你能帮我吗 请告诉我: /* --- 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(docum
/* --- 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
});
您需要定义第二个和第三个标记的位置,并像添加第一个标记一样添加它们
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
});
当然,如果您在循环中添加标记或声明一个函数来封装该任务,则还有改进的余地。我使用此函数向映射添加其他标记: item是我要添加到地图中的自定义对象,在本例中是存储的详细信息 我相信你能从这里找到你需要的东西
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);
}
我相信这会奏效。然而,当您只想打印一个标记时,要弄清楚您的代码在做什么有点过分了。你为什么不把它简化一点,使之适合一般情况呢?是的,你是对的,我只是从我曾经做过的一个项目中复制/粘贴了它。其他答案你的答案也是正确的。。用你喜欢的您的答案对于封装标记添加仍然有用,所以您不必重复自己的答案。