我使用JSON方法在数据库中保存Google叠加层。例如,我通过以下代码在数据库中保存一个圆圈:
var shape_properties;
shape_properties.center = array_shapes_object[counter].center;
shape_properties.redius = array_shapes_object[counter].radius;
shape_properties.type = array_shapes_object[counter].type;
shape_properties.zIndex = array_shapes_object[counter].zIndex;
shape_properties.fillColor = array_shapes_object[counter].get('fillColor');
shape_properties.name = array_shapes_object[counter].name;
array_shapes_string.push(JSON.stringify(shape_properties));
我有一个名为shape_properties的变量,我保存其中的所有圆属性,然后使用JSON.stringify(shape_properties)
将其更改为字符串,最后将此字符串保存到数据库。
当我想在地图上绘制这个圆圈时,我会使用类似的东西。我的意思是读取这些字符串包含数据库中的圆属性,然后使用JSON.parse(array_shapes_string[counter])
将其更改为对象,最后使用此代码绘制它:
newShape = new google.maps.Circle({
center:new google.maps.LatLng(array_shapes_object[counter].center.d, array_shapes_object[counter].center.e),
radius:array_shapes_object[counter].redius,
zIndex:array_shapes_object[counter].zIndex,
fillColor:array_shapes_object[counter].fillColor,
fillOpacity: 0.15,
strokeColor: '#FF0000',
strokeWeight: 1
});
一切都还好。但我有一个大问题。 看看这一行:
center:new google.maps.LatLng(array_shapes_object[counter].center.d, array_shapes_object[counter].center.e),
这是包含纬度和经度的圆心,它保存在数据库中,如下所示:
"中心" {" K":32.9372338139709," A":50.91888427734375}
所有属性都保存在数据库中,如下所示:
{"用户":"用户1,""中心" {" K":32.9372338139709," A& #34;:50.91888427734375}" redius":25644.7738046513,"类型":"圆"" zIndex的":2&#34 ;填充颜色":"#FF0000""名称":"圈"}
我的问题是这样的: 有时圆圈的中心由k和A保存在数据库中,如: "中心" {" K":32.9372338139709," A":50.91888427734375}
但有时候它会用d和e而不是k和A来保存: "中心" {" d":32.9372338139709," E":50.91888427734375}
当我读它绘制圆圈时,它会显示错误。我确定这个问题与谷歌地图v3有关,似乎它们有时会改变变量的名称。我该如何解决?
答案 0 :(得分:2)
您无法存储这些属性,这些属性的名称(代表google.maps
- 类实例,如LatLng
或LatLngBounds
)不一致。
您需要自己的格式/方法才能存储&恢复这些数据(通过API提供的方法获取,例如lat()
和lng()
获取LatLng)
请参阅How to save a Google maps overlay shape in the database?了解可能的解决方案