I found it, it can be calculated using google map api called Distance Matrix
it returns a json response containing The recommended path's length
and Duration
.
{
"destination_addresses" : [ "Témara, Maroc" ],
"origin_addresses" : [ "Rabat, Maroc" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "13,6 km",
"value" : 13620
},
"duration" : {
"text" : "23 minutes",
"value" : 1358
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
This should work fine:
function getDistance()
{
var url="http://maps.googleapis.com/maps/api/distancematrix/json?origins=rabat&destinations=temara&mode=driving&language=fr-FR&sensor=false";
var def = new jQuery.Deferred();
$.getJSON(url,function(json)
{
if (json.status=="OK")
{
var distance=json.rows[0].elements[0].distance.text;
}
def.resolve(distance);
});
return def.promise();
}
$.when(getDistance()).then(function(distance){
alert(distance);});