Here is a sample code ti find distance using google map using javascript
var fromaddress ="Atlanta, GA, USA" ;
var toaddress="140 Pine St NE, Atlanta, GA 30308, USA" ;
var urlforgooglemapapi="https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+fromaddress+"&destinations="+toaddress+"&key=YOUR_API_KEY&callback=?";
$.ajax({
type: "GET",
url: urlforgooglemapapi,
dataType: "jsonp",
success: function(data){alert(data);},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
//Response
{
"destination_addresses" : [ "140 Pine St NE, Atlanta, GA 30308, USA" ],
"origin_addresses" : [ "Atlanta, GA, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2.1 mi",
"value" : 3341
},
"duration" : {
"text" : "5 mins",
"value" : 275
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
0 Comments