我使用direction.setDirection方法在地图中设置路线,并从存储的json文件获取路线。当我加载没有航路点的路线时,它可以正常工作。但是当我使用航点加载路线时,它会给我错误 a.lat不是函数。在地图上显示起始位置(A)和一个航点。
在地图上加载路线的代码
function route() {
$.ajax({
type: 'POST',
url: '/Home/GetRoute',
success: function (response) {
//alert(response);
var route = JSON.parse(response);
alert(route.Data);
//alert(data);
directionDisplay.setDirections(JSON.parse(route.Data));
},
error: function (error) {
alert('Error: ' + error);
}
});
更新:我正在对它进行双重解析,因为它是一个具有属性Data的对象。 response是一个包含属性Data的对象,它包含json string。
{“Data”:“{”geocoded_waypoints“:[{”geocoder_status“:”OK“,”p ... so on
首先我解析obj以获取obj.Data输出,然后解析obj.Data以获取obj.Data的内部json字符串。
这就是我保存路线的方式:
function saveRoute()
{
var result = directionDisplay.getDirections();
alert(JSON.stringify(result));
var url = "/Home/SaveRoute";
directionDisplay.setDirections(result);
$.ajax({
type: 'POST',
url: url,
data: JSON.stringify({ jsonRoute: { Data: JSON.stringify(result) } }), //JSON.stringify(result)
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: true,
processData: false,
cache:false,
success: function (data) {
alert("Success");
},
error: function (error) {
alert("Error: "+error.statusText);
}
});
}
这是我正在加载的json文件数据:
{ "Data": "{\"geocoded_waypoints\":[{\"geocoder_status\":\"OK\",\"place_id\"
:\"ChIJ3dTEVV0DGTkRXqaSzKf50y8\",\"types\":[\"sublocality_level_1\",\"sublocal
ity\",\"political\"]},{\"geocoder_status\":\"OK\",\"place_id\":\"ChIJE4xqmkIDG
TkRQGFN4MjBPzU\",\"types\":[\"route\"]},{\"geocoder_status\":\"OK\",\"place_id
\":\"ChIJifnoE2gDGTkRtEVKM-PhYGU\",\"types\":[\"route\"]}],\"routes\":[{\"boun
ds\":{\"south\":31.534670000000002,\"west\":74.29780000000001,\"north\":31.535
54,\"east\":74.29865000000001},\"copyrights\":\"Map data ©2016
Google\",\"legs\":[{\"distance\":{\"text\":\"0.2
km\",\"value\":210},\"duration\":{\"text\":\"1
min\",\"value\":52},\"end_address\":\"Poonch Rd, Lahore, Pakistan\",\"end_loca
tion\":{\"lat\":31.5346676,\"lng\":74.29839379999999},\"start_address\":\"Sama
nabad, Lahore, Pakistan\",\"start_location\":{\"lat\":31.5355392,\"lng\":74.29
846520000001},\"steps\":[{\"distance\":{\"text\":\"19
m\",\"value\":19},\"duration\":{\"text\":\"1 min\",\"value\":4},\"end_location
\":{\"lat\":31.5354648,\"lng\":74.29864729999997},\"polyline\":{\"points\":\"c
hn_EmlndMJ]BE\"},\"start_location\":{\"lat\":31.5355392,\"lng\":74.29846520000
001},\"travel_mode\":\"DRIVING\",\"encoded_lat_lngs\":\"chn_EmlndMJ]BE\",\"pat
h\":[{\"lat\":31.53554,\"lng\":74.29847000000001},{\"lat\":31.535480000000003,
\"lng\":74.29862},{\"lat\":31.535460000000004,\"lng\":74.29865000000001}],\"la
t_lngs\":[{\"lat\":31.53554,\"lng\":74.29847000000001},{\"lat\":31.53548000000
0003,\"lng\":74.29862},{\"lat\":31.535460000000004,\"lng\":74.29865000000001}]
,\"instructions\":\"Head <b>southeast</b> on <b>Rajput Street</b>\",\"maneuver
\":\"\",\"start_point\":{\"lat\":31.5355392,\"lng\":74.29846520000001},\"end_p
oint\":{\"lat\":31.5354648,\"lng\":74.29864729999997}},{\"distance\":{\"text\"
:\"21 m\",\"value\":21},\"duration\":{\"text\":\"1 min\",\"value\":9},\"end_lo
cation\":{\"lat\":31.5352878,\"lng\":74.29856440000003},\"maneuver\":\"turn-ri
ght\",\"polyline\":{\"points\":\"sgn_EqmndM@@^N\"},\"start_location\":{\"lat\"
:31.5354648,\"lng\":74.29864729999997},\"travel_mode\":\"DRIVING\",\"encoded_l
at_lngs\":\"sgn_EqmndM@@^N\",\"path\":[{\"lat\":31.535460000000004,\"lng\":74.
29865000000001},{\"lat\":31.53545,\"lng\":74.29864},{\"lat\":31.53529000000000
3,\"lng\":74.29856000000001}],\"lat_lngs\":[{\"lat\":31.535460000000004,\"lng\
":74.29865000000001},{\"lat\":31.53545,\"lng\":74.29864},{\"lat\":31.535290000
000003,\"lng\":74.29856000000001}],\"instructions\":\"Turn <b>right</b>\",\"st
art_point\":{\"lat\":31.5354648,\"lng\":74.29864729999997},\"end_point\":{\"la
t\":31.5352878,\"lng\":74.29856440000003}},{\"distance\":{\"text\":\"0.1
km\",\"value\":103},\"duration\":{\"text\":\"1 min\",\"value\":25},\"end_locat
ion\":{\"lat\":31.535005,\"lng\":74.29780319999998},\"maneuver\":\"turn-right\
",\"polyline\":{\"points\":\"qfn_E_mndM_@pAHHd@Vh@b@\"},\"start_location\":{\"
lat\":31.5352878,\"lng\":74.29856440000003},\"travel_mode\":\"DRIVING\",\"enco
ded_lat_lngs\":\"qfn_E_mndM_@pAHHd@Vh@b@\",\"path\":[{\"lat\":31.5352900000000
03,\"lng\":74.29856000000001},{\"lat\":31.53545,\"lng\":74.29815},{\"lat\":31.
535400000000003,\"lng\":74.2981},{\"lat\":31.535210000000003,\"lng\":74.297980
00000001},{\"lat\":31.535000000000004,\"lng\":74.29780000000001}],\"lat_lngs\"
:[{\"lat\":31.535290000000003,\"lng\":74.29856000000001},{\"lat\":31.53545,\"l
ng\":74.29815},{\"lat\":31.535400000000003,\"lng\":74.2981},{\"lat\":31.535210
000000003,\"lng\":74.29798000000001},{\"lat\":31.535000000000004,\"lng\":74.29
780000000001}],\"instructions\":\"Turn <b>right</b>\",\"start_point\":{\"lat\"
:31.5352878,\"lng\":74.29856440000003},\"end_point\":{\"lat\":31.535005,\"lng\
":74.29780319999998},\"Hc\":[{\"path\":[{\"lat\":31.535290000000003,\"lng\":74
.29856000000001},{\"lat\":31.5354257,\"lng\":74.29821700000002}],\"polyline\":
{},\"travel_mode\":\"DRIVING\"},{\"path\":[{\"lat\":31.5354257,\"lng\":74.2982
1700000002},{\"lat\":31.53545,\"lng\":74.29815},{\"lat\":31.535400000000003,\"
lng\":74.2981},{\"lat\":31.535210000000003,\"lng\":74.29798000000001},{\"lat\"
:31.535000000000004,\"lng\":74.29780000000001}],\"polyline\":{},\"travel_mode\
":\"DRIVING\"}]},{\"distance\":{\"text\":\"67
m\",\"value\":67},\"duration\":{\"text\":\"1 min\",\"value\":14},\"end_locatio
n\":{\"lat\":31.5346676,\"lng\":74.29839379999999},\"maneuver\":\"turn-left\",
\"polyline\":{\"points\":\"wdn_EghndM@E|@iB@E\"},\"start_location\":{\"lat\":3
1.535005,\"lng\":74.29780319999998},\"travel_mode\":\"DRIVING\",\"encoded_lat_
lngs\":\"wdn_EghndM@E|@iB@E\",\"path\":[{\"lat\":31.535000000000004,\"lng\":74
.29780000000001},{\"lat\":31.534990000000004,\"lng\":74.29783},{\"lat\":31.534
68,\"lng\":74.29836},{\"lat\":31.534670000000002,\"lng\":74.29839000000001}],\
"lat_lngs\":[{\"lat\":31.535000000000004,\"lng\":74.29780000000001},{\"lat\":3
1.534990000000004,\"lng\":74.29783},{\"lat\":31.53468,\"lng\":74.29836},{\"lat
\":31.534670000000002,\"lng\":74.29839000000001}],\"instructions\":\"Turn <b>l
eft</b>\",\"start_point\":{\"lat\":31.535005,\"lng\":74.29780319999998},\"end_
point\":{\"lat\":31.5346676,\"lng\":74.29839379999999}}],\"via_waypoint\":[{\"
location\":{\"lat\":31.5354257,\"lng\":74.29821700000002},\"step_index\":2,\"s
tep_interpolation\":0.3536570332799496}],\"via_waypoints\":[{\"lat\":31.535425
7,\"lng\":74.29821700000002}]}],\"overview_polyline\":\"chn_EmlndMNc@`@P_@pAn@
`@h@b@@E~@oB\",\"summary\":\"Rajput Street\",\"warnings\":[],\"waypoint_order\
":[],\"overview_path\":[{\"lat\":31.53554,\"lng\":74.29847000000001},{\"lat\":
31.535460000000004,\"lng\":74.29865000000001},{\"lat\":31.535290000000003,\"ln
g\":74.29856000000001},{\"lat\":31.53545,\"lng\":74.29815},{\"lat\":31.5352100
00000003,\"lng\":74.29798000000001},{\"lat\":31.535000000000004,\"lng\":74.297
80000000001},{\"lat\":31.534990000000004,\"lng\":74.29783},{\"lat\":31.5346700
00000002,\"lng\":74.29839000000001}]}],\"status\":\"OK\",\"request\":{\"origin
\":\"Samanabad, Lahore, Punjab, Pakistan\",\"destination\":{\"lat\":31.5346676
,\"lng\":74.29839379999999},\"travelMode\":\"DRIVING\",\"Xc\":1,\"waypoints\":
[{\"location\":{\"lat\":31.535360412959733,\"lng\":74.29818126631949},\"stopov
er\":false}],\"optimizeWaypoints\":false,\"mg\":17},\"j\":true}" }