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. 

To make these points, we’ll plot some distances on a map with some handy JavaScript. And because to calculate any sort of distance requires point A and point B, we’ll start by adding two markers to a basic Google Map:

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.

Distance1

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.

To visualize the calculation, we can draw a Polyline between the two markers. Add the following lines after the markers in the JavaScript:

Reload the map and you should see a dark, diagonal line connecting the two markers, from one side of Central Park to the other. Using the JavaScript equivalent of the Haversine formula, we can determine the length of the Polyline, the straight distance between our two markers.

Add this function to your JavaScript, before the initMap function:

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 initMap function:

Load the map and you’ll see the following:

DistanceBtwn2Markers

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).

Of course, unless you’re a pigeon, your jaunt between the two locations is likely to be longer. A quick glance at the map shows there is no road or even pathway straight across Central Park. The Haversine formula is useful for basic proximity, but is insufficient for many use cases, especially within cities. For a more accurate travel distance, we’ll need to use another feature of the Maps JavaScript API.

Get Directions with the Maps JavaScript API
When the straight line distance is not adequate, you’ll need more than a formula to determine the travel distance. 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. 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.

Since our JavaScript map is already set, we’ll continue by using the Directions Service. Paste the following at the end of your initMap function:

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.

DistanceDriving

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 BICYCLING, TRANSIT, and 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