无法在Google地图中保存多边形 [英] Can't save polygon in google maps

查看:316
本文介绍了无法在Google地图中保存多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google地图多边形对象:

I have a google maps polygon object:

var poly = new google.maps.Polygon({
  paths: [
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.75737)
  ]
});

我试图通过jQuery的AJAX将它发送到MySQL db:

I'm trying to send it to MySQL db via jquery's AJAX:

$.post("savePolygon.php", {polygon: poly});

在控制台中出现此错误:

I get this error in console:

TypeError: Cannot call method 'lat' of undefined

I看过其他一些关于保存多边形的帖子,他们都说要从poly中提取latLng并将它们保存在一个分贝中。当我这样做时:

I've seen some other posts about saving polygons, and they all say to extract the latLng's from the poly's and save them in a db. When I do this though:

var latLngs = poly.getPath().getArray();
$.post("savePolygon.php", {polygon: latLngs});

我得到同样的错误。看起来阵列原型中有一个函数叫'lat'。我想知道我究竟能够提取这些值并通过AJAX发送它们,以及为什么会出现此错误?

I get the same error. It seems there is a function in array's prototype called 'lat'. I'd like to know how exactly can I extract those values and send them via AJAX, and also why do I get this error?

推荐答案

使用 google.maps.geometry .encoding.encodePath(来编码路径。此方法返回一个字符串,非常适合存储到数据库中。重复使用 google.maps.geometry.encoding.decodePath()

解码字符串请注意: geometry-library默认不加载,您必须通过将& libraries = geometry 附加到maps-API-script的src来加载它。

Please note: the geometry-library isn't loaded by default, you must load it by appending &libraries=geometry to the src of the maps-API-script.

解释为什么不能直接存储路径(例如JSON):

当存储包含LatLng的路径时(数组),您将失去LatLng的原型,这是必不可少的,因为它定义了检索属性的方法( lat() lng()

To explain why you can't store the path directly(e.g. as JSON):
when you store the path(array), which contains the LatLng's, you will lose the prototype of the LatLng's, which is essential because it defines the methods for retrieving the properties(lat() and lng() )

这篇关于无法在Google地图中保存多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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