There’s a lot your users can figure out about a place just by exploring your map–what’s nearby, whether the locations are relatively close to each other, and even roughly how far a place is based on the map’s scale. With Google Maps Platform you can take some of the guesswork out of the picture by quantifying distances via straight line distance and route distance. Each uses a different approach and solves for different user problems or actions. So today we’re explaining what each is, when to use one over the other, and how to get started.
Here we can see Central Park, as well as two nearby landmarks. The Dakota, perhaps most famous as John Lennon’s home. And The Frick Collection, an art gallery.
Suppose these were both on a New York City tour. You might be interested to know how far it is from one to the other. We’ll answer that question with some 200 year-old number crunching.
Calculate the straight line distance from latitude and longitude
The simplest method of calculating distance relies on some advanced-looking math. Known as the Haversine formula, it uses spherical trigonometry to determine the great circle distance between two points. Wikipedia has more on the formulation of this popular straight line distance approximation.
The function accepts two marker objects and returns the distance between them in miles. To use kilometers, set
R = 6371.0710. Before applying the Haversine formula, the function converts each marker’s latitude and longitude points into radians.
To call the function and report the distance below the map, add this code below your Polyline in the
Load the map and you’ll see the following:
After a successful call to the directions service, you’ll have the route added to the map. The message below the map is also extended to include the distance.
With a quick visual inspection, it’s clear that driving directions are much farther than the straight line distance. We can dig into the data that comes back in the response to find driving distance (1.6 miles, two and a half times farther), as well as the estimated duration. Here’s a JSON representation of the relevant section from the response:
While our example used driving directions, you can also pass other values to the
travelMode field in your route object. The directions service can also accept
WALKING values. Try adjusting the mode and running the code again to see how the directions result changes.
Which distance should I use?
The two distance types in this post are useful in different scenarios. To give your users the best experience, you should use each in the appropriate situation.
You can use straight line distance to make quick calculations without a call to an external service. 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. However, as the name suggests, the distance will be a simple straight line. Roads, obstructions, and traffic are not factored into straight line distance.
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. 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.
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. To explore even more tools for helping users get from here to there, check out the Google Maps Platform Routes APIs. And for more information on Google Maps Platform, visit our website.
Source: Google Cloud Blog