
To explore even more tools for helping users get from here to there, check out the Google Maps Platform Routes APIs. Whether you’re measuring distances around the globe or across town, use one of these approaches to help your users better understand their surroundings and make decisions. Due to the complexity of these calculations, route distance requires a call to the Directions Service, which will also return the duration of the route and even the path to plot visually on a map.

Route distance, on the other hand, will return the distance along a route, including necessary turns and an understanding of the area’s traffic patterns.

Roads, obstructions, and traffic are not factored into straight line distance. However, as the name suggests, the distance will be a simple straight line. You’ll need to include the Haversine function used in this tutorial, but all you need are map coordinates to get an accurate result within the browser. You can use straight line distance to make quick calculations without a call to an external service. To give your users the best experience, you should use each in the appropriate situation. The two distance types in this post are useful in different scenarios.
#GOOGLE MAPS DISTANCES BETWEEN PLACES CODE#
Try adjusting the mode and running the code again to see how the directions result changes. The directions service can also accept BICYCLING, TRANSIT, and WALKING values. While our example used driving directions, you can also pass other values to the travelMode field in your route object. Paste the following at the end of your initMap function: Since our JavaScript map is already set, we’ll continue by using the Directions Service. You can use the Directions API for server-side requests, or the Directions Service in the Maps JavaScript API for client-side requests on the web. Driving directions are one of the most popular features in Google Maps, so it’s unsurprising that it’s also made available via an API. When the straight line distance is not adequate, you’ll need more than a formula to determine the travel distance. Get Directions with the Maps JavaScript API For a more accurate travel distance, we’ll need to use another feature of the Maps JavaScript API. The Haversine formula is useful for basic proximity, but is insufficient for many use cases, especially within cities.

A quick glance at the map shows there is no road or even pathway straight across Central Park. Of course, unless you’re a pigeon, your jaunt between the two locations is likely to be longer. Now we know the straight line distance between The Dakota and The Frick Collection is 0.60 miles (rounded to two decimal places using the JavaScript toFixed function). * Set the size of the div element that contains the map */ #map
