[英]Google Maps Load Route
I've got a google map route in the form of the following string 我有以下字符串形式的google map路线
NewRoute = '{"start":{"lat":51.5479454,"lng":-0.04057739999996102},"end":{"lat":51.5166787,"lng":-0.0596227999999428},"waypoints":[[51.5474343,-0.07557889999998224]]}';
which I want to load onto a map. 我想加载到地图上 How do I convert the string NewRoute into the array os so it can be used in the following function?
如何将字符串NewRoute转换为数组os,以便可以在以下函数中使用?
function showRoute(oViewMapButton)
{
ButtonName = oViewMapButton.name;
if(ButtonName.length == 13){
var RouteName = "strWayPoints_r" + ButtonName.slice(-1);
var DistanceName = "strDistance_r" + ButtonName.slice(-1);
}else{
var RouteName = "strWayPoints_r" + ButtonName.slice(-2);
var DistanceName = "strDistance_r" + ButtonName.slice(-2);
}
oRoute = document.getElementById(RouteName);
var NewRoute = oRoute.value;
var os = [];
os = NewRoute;
var wp = [];
for(var i=0;i<os.waypoints.length;i++)
wp[i] = {'location': new google.maps.LatLng(os.waypoints[i][0], os.waypoints[i][1]),'stopover':false }
$( "#hid_FieldName3" ).val(DistanceName);
$( "#map-form" ).dialog( "open" );
var request = {
origin:new google.maps.LatLng(os.start.lat,os.start.lng),
destination:new google.maps.LatLng(os.end.lat,os.end.lng),
waypoints: wp,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
alert("There was an unknown error in your request. Requeststatus: \n\n"+status);
}
});
}
How did you get that string? 你是怎么得到那串的? It looks like a javascript object.
它看起来像一个javascript对象。 If you assign it without the outer quotes, you will get an object and can use it like this:
如果您在不使用外部引号的情况下进行分配,则将获得一个对象并可以像这样使用它:
var route = {
"start": {"lat": 51.5479454, "lng": -0.04057739999996102},
"end": {"lat": 51.5166787, "lng": -0.0596227999999428},
"waypoints": [[51.5474343, -0.07557889999998224]]
};
route.start
route.start.lat
route.waypoints[0][1]
etc.
About javascript objects: 关于javascript对象:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects
Assuming that the JSON is the value of the element with the RouteName id, just amend the following line: 假设JSON是具有RouteName id的元素的值,只需修改以下行:
var NewRoute = oRoute.value;
to: 至:
var NewRoute = JSON.parse(oRoute.value);
You may try this (just convert the json
into an object) 您可以尝试一下(只需将
json
转换为对象)
var NewRoute = '{"start":{"lat":51.5479454,"lng":-0.04057739999996102},"end":{"lat":51.5166787,"lng":-0.0596227999999428},"waypoints":[[51.5474343,-0.07557889999998224]]}';
var os = JSON.parse(NewRoute);
So, now you can use it like, os.start.lat
or os.waypoints.length
in your function. 因此,现在您可以在函数中像
os.start.lat
或os.waypoints.length
一样使用它。
In your function, replace following line 在您的函数中,替换以下行
var NewRoute = oRoute.value;
with following line 与以下行
var NewRoute = JSON.parse(oRoute.value);
That's it. 而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.