我在 XML 文件中有多边形坐标以在 Google 地图上显示多边形形状,我只想从 xml 中获取它们,并且需要创建如下所示的数组。
var triangleCoords = [
new google.maps.LatLng(51.055221725218054, -3.1630325317382812),
new google.maps.LatLng(51.010961025187314, -3.1359100341796875),
new google.maps.LatLng(51.043135193868025, -3.063812255859375)
];
感谢您的回复。这是下面的代码,它不显示多边形形状。
var bermudaTriangle;
var points = "(51.055221725218054, -3.1630325317382812),(51.010961025187314, -3.1359100341796875),(51.043135193868025, -3.063812255859375)";
points = points.substr(1, points.length - 2).split("),(");
var triangleCoords = points.map(function (pointString) {
var latlon = pointString.split(", ");
return { lat: latlon[0], lon: latlon[1] };
});
// Construct the polygon
bermudaTriangle = new google.maps.Polygon({
paths: JSON.stringify(triangleCoords),
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
bermudaTriangle.setMap(map);
});
XML 代码获取坐标。
$(xml).find('Location').each(function () {
var points= $(this).find('Coodinates').text();
// 这里我会成功获取积分但是需要像上面那样创建数组 (51.055221725218054, -3.1630325317382812),(51.010961025187314, -3.1359100341796875),(51.043135193868025, -3.0653825, -3.065382)
}