3

I would like to use the Google maps API to generate a route between my client's offices and one of their customers.

I would then like to find any customers (from my internal database) that are located within e.g. 10 miles either side of that route.

Basically I want to replicate this functionality: http://gis5.com/pois_along_route/gm_pois_along_route.php

I'd appreciate any pointers that you may have on this

RobD
  • 1,695
  • 2
  • 24
  • 49
  • Making comparison to the specified link - your customers would be the restaurants from the link? – GoG Apr 06 '11 at 23:09
  • That's absolutely correct GoG. So my process at the moment is something along the lines of: Get Route points from Google and then perform the search against data held locally in a SQL2008 database, using Google's route points / line as an operand. – RobD Apr 08 '11 at 08:22

1 Answers1

8

In general, you have to find distance from a given point (your customer) and a polyline (the route). The route from the google directions response is divided into legs, and the leg is consisted of multiple steps... In each step, you have path[] array of locations (pair of latitude and longitude). Let's call each location in the path[] array, a "Vertex". You have to iterate through all vertices in the route (get all vertices from all steps in the route) and calculate the distance between the polyline created from the current "observed" vertices and the yours POI (Points of Interest - the Customers) The shortest distance can be calculated by presenting the two vertices & the Customer's location as nVector. After this, by doing Vector cross product of the vectors you find the shortest (orthogonal) distance between the point and the polyline I had similar problem recently and I solved it that way. I got the idea from the examples Here (look at the DISTANCE POINT TO POLYLINE OR POLYGON example Source code)

If you are confused from the Math involved, maybe this link might help you by explaining the concept of a Vector Cross Product. Also this could also help you to understand the nVector concept.

Because this kind of calculating the distance is pretty much time consuming (if you have many customer locations in the database), you might be interested of filtering some of the locations from the database and not performing calculations for them...look at my answer on this question about how to do filtering on locations based on the route that is shown on the map. Hope this helps and give you some basic idea on how to start solving your problem. It took me a lot of time till to figure it out. Cheers.

Community
  • 1
  • 1
GoG
  • 310
  • 2
  • 6
  • 13
  • That's great GOG! Thank you very much, this has definitely set me on the right path. – RobD Apr 11 '11 at 09:13