Reputation: 27
I have an application to calculate distance between two points.
https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + origins + "&destinations=" + destinations + "&units=imperial...
I am setting the units to imperial , however some distances are returned in Kms and Ft. Do I need to set any other parameter?
Upvotes: 1
Views: 10056
Reputation: 841
just change unit from imperial to km,
https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + origins + "&destinations=" + destinations + "&units=km...
Upvotes: 1
Reputation: 53
Use unitSystem: google.maps.UnitSystem.METRIC if you want result in Kilometers. Use unitSystem: google.maps.UnitSystem.IMPERIAL if you want result in Miles.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<script>
$(document).ready(function(){
var distanceService = new google.maps.DistanceMatrixService();
distanceService.getDistanceMatrix({
origins: ["10 West 35th Street, Chicago, IL, USA"], //$("#autocompleteDeparture").val()
destinations: ["Union Station, South Canal Street, Chicago, IL, USA"], //$("#autocompleteArrival").val()
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL, //use "IMPERIAL" for Miles, "METRIC" for KMs
durationInTraffic: true,
avoidHighways: false,
avoidTolls: false
},
function (response, status) {
if (status !== google.maps.DistanceMatrixStatus.OK) {
console.log('Error:', status);
} else {
console.log(response);
}
});
});
</script>
</head>
<body>
</body>
</html>
Upvotes: 2
Reputation: 54
Here is the code I used to get the distance in miles.
Method 1:
You will have to call the google map api JS library with your key then use this below code.
var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = 'Greenwich, England';
var destinationA = 'Stockholm, Sweden';
var destinationB = new google.maps.LatLng(50.087692, 14.421150);
//var uniteSystem = 'IMPERIAL';
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1],
destinations: [destinationA],
travelMode: 'DRIVING' ,
unitSystem: google.maps.UnitSystem.IMPERIAL
}
, callback);
function callback(response, status) {
// See Parsing the Results for
// the basics of a callback function.
console.log(response);
}
You will have an array of element as response.
Method 2: This is the url method I used to get the distance in miles. https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=41.43206,-81.38992&key=TOUR_API_KEY
Remember to change the API key.
Upvotes: 4
Reputation: 6460
The distance.text field is expressed in the units passed in the request; the distance.value field is always expressed in meters.
Refer to the Distance Matrix developer guide and you'll find this blurb:
Note: this unit system setting only affects the text displayed within distance fields. The distance fields also contain values which are always expressed in meters.
Upvotes: 1