更改路径后,如何在Google地图中获取多边形对象? [英] How can I get the polygon object in google maps, when a path changed?

查看:54
本文介绍了更改路径后,如何在Google地图中获取多边形对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多使用API​​ v3在Google Maps上动态创建的多边形.我没有遍布全球的产品.创建后,我为每个事件分配事件,因为我需要跟踪用户对其进行的更改.一切正常,除了一件事.

I have a lot of polygons created dynamically on a Google Maps, using API v3. I don't have a global array of them. I assign event for each after creation, because I need to track the changes made to them by the user. Everything work fine, except one thing.

path = polygon.getPath(); //Note, that polygon isn't a global variable
//DragEnd
google.maps.event.addListener(polygon, "dragend", function(){
    SavePolygonToDb(this);
});
//Edited
google.maps.event.addListener(path, "set_at", function() {
    //Now 'this' is the path array, not the polygon 
    SavePolygonToDb(????);
});

我需要以某种方式获取路径所属的多边形对象,但是我找不到解决方法.我想到的唯一解决方案是,我需要将所有多边形及其路径存储在数组中,然后,如果发生某些变化,我将遍历数组并将路径值与实际值进行比较.如果某些LatLng坐标不同,则多边形会更改.将新值保存到后端数组,然后继续.但是我认为,必须有其他(更简便的)解决方案来做到这一点.

Somehow I need to get the polygon object, which the path belongs to, but I can't find a way for it. The only solution, that I came up with, that I need to store all the polygons and their paths in an array, then if something changes, I loop through the array and compare the path values to the actual ones. If some LatLng coordinate differs, then the polygon changed. Save the new values to tha backend array, and go on. But I think, there must be some other (easier) solution to do this.

推荐答案

同时,我想出了一个简单的解决方案.为多边形定义一个全局数组:

Meanwhile I came up with a simple solution. Define a global array for the polygons:

var polygonArray = [];

创建多边形时,将其推入该数组:

When creating the polygons, push them into that array:

polygonArray.push(polygon);

并添加一个这样的事件侦听器:

And add an event listener like this:

google.maps.event.addListener(path, "set_at", function(){
    for (i in polygonArray) {
        if (polygonArray[i].getPath() == this) {
            alert('Got it'); //polygonArray[i]
        }
    }
});

这篇关于更改路径后,如何在Google地图中获取多边形对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆