Antonio Laguna
Antonio Laguna

Reputation: 9300

Get distance to walk in Google Maps

I'm trying to do some experiment with an app. I want that, given my location and given a distance, I get a point in Google Map to which I could walk that distance.

That has some complications since getting a point X miles out from you is "easy" but it's unlikely you'll actually walk that amount of miles unless you can actually go in a straight line.

The approach I followed is using google.maps.places.PlacesService and the function nearbySearch given a radius. Then I process the distance to each returned point and luckily I get one point I could walk that distance far.

However, I've been trying some edge cases, like picking a road near a desert and then it won't work cause "places" are points of interest and that may be only just a plain road with nothing else than boring bushes for a long, long way.

In my mind it just should follow that road for the given distance and then stop.

To be clear:

I want to know from my starting point, which would be my ending point if I were to walk 1 Km

Do you have any better approach to this kind of situations?

Upvotes: 0

Views: 913

Answers (1)

Tyler Eich
Tyler Eich

Reputation: 4248

The google.maps.DirectionsService class will calculate the route a traveller would take, given an origin and a destination. It accepts a google.maps.DirectionsRequest object. The DirectionsRequest has an option called TravelMode, which will accept any of four constants (BICYCLING, DRIVING, TRANSIT, WALKING) from the google.maps.TravelMode class.

To find a point, say, 3 kilometers from your location, I would:

  1. Request walking directions from DirectionsService, using your current location and a point approximately 3 kilometers away from you.
  2. Iterate over the overview_path, continually adding the length of the current segment to your grand total.
  3. Cut the last segment to length when you exceed 3 kilometers, creating a length whose final length is very close to 3 kilometers.

I made you a Plunk demonstrating how this could be done.

Upvotes: 2

Related Questions