我的JSON是:
var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
obj = JSON.parse(json);
alert(obj.coordinate);
但我需要设置坐标值如下:
var myTrip=[new google.maps.LatLng(1,2),new google.maps.LatLng(3,4),new google.maps.LatLng(5,6)];
答案 0 :(得分:2)
遍历坐标并为每个坐标创建一个新对象,并将其添加到myTrip
:
var myTrip = [];
for(var i=0; i < obj.coordinate.length; i++) {
myTrip.push(new google.maps.LatLng(obj.coordinate[i][0],obj.coordinate[i][1]))
}
答案 1 :(得分:1)
这个怎么样:
var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
var obj = JSON.parse(json);
var myTrip = [];
for (var i = 0; i < obj.coordinate.length; i++) {
myTrip[i] = new google.maps.LatLng(obj.coordinate[i][0], obj.coordinate[i][1]);
}
答案 2 :(得分:1)
您需要遍历坐标集合:
var myTrip = getCoordinate ();
var getCoordinate = function(){
var ret = [];
for(var i = 0, length = coordinate.length; i < length; i++){
ret.push(new google.maps.LatLng(coordinate[i][0], coordinate[i][1]))
}
return ret;
}