2

所以我在一个地理定位项目(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;
}

所以如果有人有任何解决方案或想法,我将不胜感激。

谢谢大家@david @Beetroot-Beetroot @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]
    });
}
}

更新: 我的脚本工作正常,但我的活动中有一个小问题,当我在标记上单击时,它应该显示他的标题,但它只显示最后一个标记的标题:

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);
    });
 }
}
4

3 回答 3

1

我已经简化了你的代码

function setMarkers(locations) {
    for(var i in locations) {
    var points = new google.maps.LatLng(locations[i][1].lat,locations[i][2].lng);
    checkpoints.push(points);
    var marker = new google.maps.Marker({
     map:map,
     position: points,
     icon: 'myicon.png',//
     title: locations[i][0],
     zindex: locations[i][4]
    }); 

   } 
}
于 2013-04-21T13:05:57.473 回答
0

Mohammadov,你让它变得比它需要的更复杂。

尝试这个 :

function setMarkers(map, locations) {
    for(var i=0; i<locations.length; i++) {
        var loc = locations[i];
        loc[4] = new google.maps.Marker({
            position: new google.maps.LatLng(loc[1], loc[2]),
            map: map,
            title: loc[0],
            zIndex: loc[3]
        });
    }
}

然后地图应该显示标记,并且locations数组的每个成员都应该增加对其标记的引用,作为元素 [4]。

于 2013-04-21T12:55:24.110 回答
0

主循环内的两个循环引起了问题,它们是不必要的,可以避免。循环结束时的 return marker 也只会返回最后一个,你也可以删除它:

var checkpoints = [];

function setMarkers(map, locations) {    
for (var i=0; i<locations.length; i++)
{
    var place    = locations[i],
        myLatLng = new google.maps.LatLng(place[1], place[2]),   
        marker = new google.maps.Marker
                (
                    {
                        position: myLatLng,
                        map: map,
                        title: place[0],
                        zIndex: place[3]
                    }
                );
    checkpoints.push({"point": myLatLng, "marker": marker}); 
}
}

检查点现在应该有一个数组LatLngmarker每个地方。

jsfiddle

于 2013-04-21T12:57:15.443 回答