所以我在一個地理定位項目中工作(asp.net MVC4),我在我的數據庫中有很多職位,我想在我的地圖中顯示它,我的問題是腳本只顯示第一個位置,這是我的腳本:在地圖中顯示多個標記
var checkpoints = [];
function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++)
{
var place = locations;
for (var i = 0; i < place.length; i++)
{
var points = new google.maps.LatLng(place[i][1], place[i][2]);
checkpoints.push(points);
}
var check = checkpoints[0];
var index = 0;
for (var j = 0; j < checkpoints.length; j++)
{
check = checkpoints[j];
index = j;
var myLatLng = new google.maps.LatLng(place[index][1], place[index][2]);
var marker = new google.maps.Marker
(
{
position: myLatLng,
map: map,
title: place[index][0],
zIndex: place[index][3]
}
);
}
}
return marker;
}
所以如果有人有任何解決方案或想法,我將非常感激。
謝謝大家@大衛@甜菜,甜菜根@razzak我的劇本現在工作得很好,這是我的新的腳本:
var checkpoints = [];
function setMarkers(map, locations) {
for (var i in locations) {
var points = new google.maps.LatLng(locations[i][1], locations[i][2]);
checkpoints.push(points);
var marker = new google.maps.Marker({
map: map,
position: points,
title: locations[i][0],
zindex: locations[i][4]
});
}
}
更新: 我的腳本工作正常,但我有一個小問題我事件,當我上的一個標記DBLCLICK它應該表現出他的頭銜,但它只是告訴我的最後一個標誌的標題:
var checkpoints = [];
function setMarkers(map, locations) {
for (var i in locations) {
var points = new google.maps.LatLng(locations[i][1], locations[i][2]);
checkpoints.push(points);
var marker = new google.maps.Marker({
map: map,
position: points,
title: locations[i][0],
zindex: locations[i][4],
});
//this is my event
google.maps.event.addListener(marker, 'dblclick', function() {
alert("I am marker " + marker.title);
});
}
}
再次感謝你@david它的工作原理,但你應該更新以下幾行:var points = new google.maps.LatLng(locations [i] [1 ],地點[i] [2]);和:title:locations [i] [0], zindex:locations [i] [4] – Mohammadov 2013-04-21 15:34:08
我在活動中遇到了一個小問題,它向我展示了相同的標題:google.maps.event.addListener(marker, 'dblclick',函數(){alert(「I am marker」+ marker.title); }); – Mohammadov 2013-04-21 16:22:43
@Mohammadov嘗試'alert(「我是標記」+ this.title);' – 2013-04-21 17:45:59