我试图找到两个位置之间的距离。为此,我使用了 Google 网络服务。我从该 Web 服务获得了 JSON 响应,但我无法在 javascript 函数中获得与该 JSON 响应的距离。
请参阅下面的代码:
function myjourneysubdetails(){
var fp = jQuery('#fp').val();
var tp = jQuery('#tp').val();
var city = jQuery('#city').val();
alert(fp);
$.ajax({
type : 'GET',
url : 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=Silk board&destinations=marathahalli&language=en-US&sensor=false',
async: false,
success : function(data) {
alert("Success");
var locObject = eval('(' + JSON.stringify(data) + ')');
alert("locObject: "+locObject);
alert("Destination: "+locObject.destination_addresses);
var origins = data.origin_addresses;
var destinations = data.destination_addresses;
alert('destinations'+destinations);
for (var i = 0; i < origins.length; i++) {
var results = data.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
alert('distance'+distance);
}
}
},
error : function(xhr, type) {
alert('Internal Error Occoured! '+xhr+' : '+type);
}
});
}
提前致谢。