我如何使用多个标记来执行gmap v3,这些是可拖动的

时间:2012-01-16 19:50:07

标签: javascript google-maps-api-3

var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    icon: image,
    title: tittle,
    zIndex: i,
    draggable: true
});

google.maps.event.addListener(marker, 'drag', function() {
    $("input[name='request']").val(marker.getPosition());
});

google.maps.event.addListener(marker, 'dragend', function() {
    $("input[name='request']").val(marker.getPosition());
});

事件被添加到单个标记中,需要至少1000或甚至无限。

1 个答案:

答案 0 :(得分:1)

使用位置数组,并为每个位置调用addMarker方法。

var locationsArray;

for(var i = 0 ; i<locationsArray.length; i++){
    addMarker(locationsArray[i]);
}


function addMarker(myLatLng){
var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    icon: image,
    title: tittle,
    zIndex: i,
    draggable: true
});

google.maps.event.addListener(marker, 'drag', function() {
    $("input[name='request']").val(marker.getPosition());
});

google.maps.event.addListener(marker, 'dragend', function() {
    $("input[name='request']").val(marker.getPosition());
});
}