GOOGLE MAP API v3,需要路径的多边形

GOOGLE MAP API v3, Need polygon of a path

本文关键字:路径 多边形 MAP API v3 GOOGLE      更新时间:2023-09-26

我使用的是谷歌地图v3,我的问题是,我有200+多边形在一个地图上,他们都是可编辑的,我需要使一个ajax调用在事件监听器的变化使用路径而不是多边形来检测变化。

所以在回调函数this =多边形。getpath(),我怎么能得到它所属的多边形。在多边形中,我使用set来设置ajax调用所需的信息。

    poly1.set('name', 'poly1');
    poly1.set('id', 1);
    google.maps.event.addListener(poly1, 'dragend', setNewArea);
    google.maps.event.addListener(poly1.getPath(), 'insert_at', setNewArea);
    google.maps.event.addListener(poly1.getPath(), 'remove_at', setNewArea);
    google.maps.event.addListener(poly1.getPath(), 'set_at', setNewArea);

在setNewArea中,我可以很容易地检查这个,看它是多边形还是路径,但如果是路径,我没有办法得到它的父多边形。我不想有200个自定义回调只是硬编码多边形,必须有其他更干净的方式。

这样做的一种方法是将poly1的对象引用添加到分配的回调中。下面是我使用maps API编写的一些代码,它为打开信息窗口的特定标记上的单击事件添加侦听器。

var latLng = new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({
    position: latLng,
    map: window.map,
    title: pinName
});
var infoWindow = new google.maps.InfoWindow({
    content: content
});
google.maps.event.addListener(marker, 'click', function() {
    infoWindow.open(window.map, marker);
});

对于你的例子,你可能想做如下所示。这将确保你的回调函数得到对多边形对象的引用,即使触发事件与路径相关。

poly1.set('name', 'poly1');
poly1.set('id', 1);
google.maps.event.addListener(poly1, 'dragend', function() { 
    setNewArea(poly1); 
});
google.maps.event.addListener(poly1.getPath(), 'insert_at', function() { 
    setNewArea(poly1); 
});

可以在对象之间循环引用。

var thePath = poly1.getPath();
thePath.parent = poly1;
google.maps.event.addListener(thePath, 'set_at', function () {
    console.log('My parent is the polygon', this.parent);
}.bind(this);