gpt4 book ai didi

javascript - 谷歌地图加载路线

转载 作者:行者123 更新时间:2023-11-28 20:18:30 25 4
gpt4 key购买 nike

我有以下字符串形式的谷歌地图路线

NewRoute = '{"start":{"lat":51.5479454,"lng":-0.04057739999996102},"end":{"lat":51.5166787,"lng":-0.0596227999999428},"waypoints":[[51.5474343,-0.07557889999998224]]}';

我想将其加载到 map 上。如何将字符串 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);
}
});
}

最佳答案

假设JSON是RouteName id的元素的值,只需修改以下行:

var NewRoute = oRoute.value;

至:

var NewRoute = JSON.parse(oRoute.value);

关于javascript - 谷歌地图加载路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18806881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com