使用多边形叠加将颜色设置为谷歌地图中的特定区域

时间:2013-06-27 12:08:39

标签: javascript google-maps google-maps-api-3

我需要为谷歌地图的特定区域着色。我试过了 polygon overlay example.。这里使用的坐标如下:

var triangleCoords = [   
    new google.maps.LatLng(23.8099144,90.40607589999999,0),
    new google.maps.LatLng(24.8199144,86.40607589999999,0),
    new google.maps.LatLng(25.8299144,92.40607589999999,0),
    new google.maps.LatLng(23.8099144,90.40607589999999,0)
];

但我的区域坐标如下格式:

  

89.90277900000007,22.36819100000007,0 89.90277900000007,22.36833399999999,0 89.90334400000013,22.36861000000009,0 89.903611,22.36889000000003,0 89.90416700000004,22.36917099999998,0 89.90499900000009,22.37001000000014,0 89.90499900000009,22.37111000000006,0 89.905281,22.37138800000008,0 89.905281,22.37833300000009,0 89.90333500000007, 22.37833300000009,0 89.90166400000005,22.37527899999996,0 89.901391,22.3750010000001,089.90166400000005,22.36944400000003,0 89.90166400000005,22.36929200000004,0 89.90166400000005,22.36916600000013,0 89.90110800000001,22.36861000000009,0 89.90132199999998,22.36829099999999,0 89.90135199999997,22.368179,0 89.901391,22.36805600000009,0 89.90277900000007,22.36805600000009 ,0 89.90277900000007,22.36819100000007,0

现在我如何使用此区域协调代码值。该值来自数据库单个字段,并且很难一个接一个地合并到对象中,如:

new google.maps.LatLng(89.90277900000007,22.36819100000007,0),
new google.maps.LatLng(89.90277900000007,22.36833399999999,0),
.
.
.
new google.maps.LatLng(89.90277900000007,22.36819100000007,0),

请给我一个合适的解决方案。

2 个答案:

答案 0 :(得分:1)

使用此代码...

var res = "89.90277900000007,22.36819100000007,....,0 89.90277900000007,22.36819100000007,0";
resArray = res.split(" ");
var triangleCoords = [];

for(var i=0; i<resArray.length; i++){
    triangleCoords.push(new google.maps.LatLng(resArray[0]))
}

答案 1 :(得分:0)

你可以尝试这种方法。

从数据库中获取所有数据,然后按空格分割(使用您正在使用的任何服务器端语言或使用javascript)。然后你将得到一个数组,尝试循环该数组并构建latlng对象。但是按空格划分并不是最好的方法,因为数据库中可能存在没有空格的数据。

注意:新的google.maps.LatLng(23.8099144,90.40607589999999,0),这里不需要最后一个arg。只有纬度和经度才有效。