如何更新标记的坐标?

时间:2016-05-31 17:36:30

标签: google-maps google-maps-api-3 google-maps-markers

我使用Google Maps API并在地图上添加标记:

for(var i = 1; i <= 100; i++){

var position = {lat : i, lng : i};

var marker = new google.maps.Marker({
                    position: position,
                    map: map,
                    draggable: true
                });

                marker.addListener('click', function() {
                    infowindow.open(map, marker);
                });
}

如果之前创建了此标记,我如何更新marker[i] = 22的坐标?

1 个答案:

答案 0 :(得分:0)

将标记放入数组并使用setPosition

    var markers;
    var k;
    for(var i = 1; i <= 100; i++){

    var position = {lat : i, lng : i};

    var marker = new google.maps.Marker({
                        position: position,
                        map: map,
                        draggable: true
                    });

    k = markers.push(marker);
                    markers[k-1].addListener('click', function() {
                        infowindow.open(map, this);
                    });
    }

   .....
    var myNewlatlng = new google.maps.LatLng( 24.397, 40.644);
    markers[22] setPosition(myNewlatlng);