Points within n km's of route / path - google-maps

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

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.

Related

Google maps waypoints limit - show long routes accurately from raw GPS data

I am well aware that this question was asked many times and i've spent days looking through stackoverflow answers but couldn't find anything that goes behind workarounds.
The problem is pretty simple: we need to show exact routes passed by the car (so, from GPS long/lat history data) on google maps and the routes are hundreds or even thousands kms long. What that mean is that we will always exceed waypoints limits by at least an order of magnitude (8 for free or 25 for premium). My gut is telling me that batching GPS locations to batches of 8/25, sending many requests that way and then snap all of those to roads and at the end merge all together into a single route is a bit wild solution.
If we are on a highway then its not that big problem since snap and expected/calculated route will work but if some long route is a combination of highway but also in-town drive through small streets with alot of turning, then i can imagine huge discrepancy between an actual (from raw GPS data) and shown route.
I am wondering if i am missing something? Is there some 'more proper' way to approach this problem?
PS.
I don't need any code at the moment, just a proper way to architecture the idea.
Thanks
[UPDATE]
To put a few numbers into the mix:
1) average route distance is about 1800kms
2) number of raw GPS points generated is about 15000-18000 (every 100-150 meters)
3) number of points that the route must go through (waypoints) is in at least in hundreds and sometimes in thousands (when most of the route is in urban areas)
One possible solution is the Roads API.
Related questions:
draw a path from GEO locations from GPS snapped to road
Google Map Road API not interpolating path and not giving smooth route
Issues
The Roads API doesn't seem ready for prime time (reports of inconsistent results in the issue tracker)
Issue 9436: Roads-API - Snapping point defects
has a limit of 100 points at a time with a (not documented) recommended maximum separation of 400m (reference a comment under the answer to Google Map Road API not interpolating path and not giving smooth route).
You could render the routes as images yourself and load them as an overlay layer on top of google maps.
For example in my website we render weather forecast layers and add them as layers on top of the base map:
http://www.weather.gr/en/maps.aspx

How accurate is the Lat/Lng that you get from SV API's getPosition()?

I am asking the same question on Google's product forum, so excuse me if you see the same question.
I am using Street View Service to develop my application that is similar to this example (figure above). In this application, I want to store where, in lat/lng coordinates, users have traveled on the map. I figured out StreetViewPanorama class provides an API, getPosition(), and this returns LatLng. Lat/lng coordinates you extract from LatLng object look like lat=38.924066 and lng=-77.032172. I can use these values to store what I want.
Although the aforementioned latlng values have 8 significant figures, I was actually wondering how accurate these values are, because GPS locations that Google collects while running a Street View car would have errors.
So my question is, how accurate is the latlng value returned by getPosition()? In other words, how much concordance can I expect between latlng values from getPosition() and latlng position on Google Map?
Thank you very much!
I got an answer in another forum so I copy it over here too and mark the question as solved. The following is the answer from barryhunter in Google Forum.
A 6dp lat/long is on the order of precise to 0.1m. (ie 10cm)
So such a lat/long should match up very precisely with the Google Map.
Ie its a precise location.
But as to how accurate the underlying map is, that varies a lot. Its
almost impossible to tell. Most locations I would estimate to be
within about 6-30m
You can check the accuracy of a lat or a lng value with 6 decimal points in the following page:
http://nearby.org.uk/precision-ll.cgi?lat=38.924066&long=-77.032172
As indicated in the above answer, the real limitation is the positioning of the map, not the numerical value obtained from it.
For example, I happened to be taking readings at a location where two Google Map sections overlapped. There was an obvious discontinuity in color and terrain features when going from one to the other. The photos must have been taken at least a year apart.
A marker placed on one side of this overlap jumped by at least 10 m when viewed on the other overlapping section. In other words, one (or both) map sections were off by at least a few meters relative to the true GPS coordinates.
If you are lucky enough to be working on a single section, distances between map points are probably quite accurate, but if you check a point against the actual GPS coordinate on the ground, who knows? A bias error of 6-30m seems likely to me.

check whether a point lies in a route - Google maps

I need to highlight relevant locations in a google map when a user requests for a route.
I have found a few steps to implement. But got stuck at one point.
Let's assume that we have a collection of large number of points (a few thousands). We need to find out which of them lie in a (dynamic) route.
Step1: First filter out the points based on the route. That means, find the max-lat, min-lat, max-lon, min-lon of the route and apply this filter out to our group. So that we can figure out a lesser number of eligible candidates.
Step2: We can still filter out the unnecessary points using a utility library called RouteBoxer. (http://google-maps-utility-library-v3.googlecode.com/svn/tags/routeboxer/1.0/examples/routeboxer-v3.html)
Step3: After above 2 steps, we have reduced the number of points to a few hundred from a few thousand. And these points would be somewhere around that route, though not exactly on it. This is where I face the challenge. How to check whether these points lie in that route? I dont think getting all the points a route and check the distance is a good solution (yeah, it will work. but..) as the route can be of any distance. Is there any way to check the minimum distance between a point and a route?
RouteBoxer will give you a list of polygons or LatLngBounds (if if gives polygons you can get LatLngBounds fron it anyway). You can cycle through each of your points and check if that point lies on any of those bounds using the contains method of LatLngBounds class.
Yes, there is dijkstra or kruskal algorithm.

Find Routes within Range of a Point

I am seeking a way using Google Maps to take a predefined latlng point and find routes that pass within 20 miles. The start and end points for the route exist in a MySQL DB.
Would it be best, when storing the routes, to include all the polylines that make up the route?
Should I store the RouteBoxer coordinates and find all boxes that contain the predefined latlng point? This may be up to 3x greater distance then 20 miles
How should I store the data for both predefined latlng points and the route information?
EDIT:
This article may bring some ideas but I am still unsure of the best method? I am concerned with scaling because I am thinking of 50,000+ routes and thousands of routes.
--> Find how many (googlemap) polygons near x distance of a point
--> Get polygons close to a lat,long in MySQL
--> MySQL Find Polygon Nearest to Point
Thanks very much!
David

How can I sort/group Salesforce leads by geography?

If I had lat/long data for all our leads in Salesforce, is there a way to write a query to group them, or say list all the leads within 10 miles of San Francisco, CA ?
[EDIT: Clarification]
I have thousands of leads with both a full address, and long/lats.
I want to build a query on these leads that will give me all of the leads near San Francisco, CA. This means doing GIS type work within salesforce.
I could of course filter specifically on city, or zipcodes or area code, but this presents some problems when trying to rollup a whole metro area.
Yes. You need to Reverse GeoCode them with a tool/service. In the past I have used Maporamas service but it was quite expensive and that was before Google maps and virtual earth existed so I am sure there is something cheaper(free) out there now.... Googling around I have found this and this
EDIT:
OK from What I understand you are trying to calculate the distance between 2 lat/long points. I would start by discounting the ones that where outside you sphere of (lets say) 10 miles. So from your central point you will want to get the the coordinates 10 miles, East, West, South and North. To do this you need to use the Great-circle distance formula.
From that point you have you Sales Force Data if you wish to break this data up further then you need to order the points by distance from the central point. To do this you need to use the Haversine formula
I am not sure what you language preference is so I just included some examples from SQL(mainly) and C#
Haversine Formula in C# and in SQL
Determine the distance between ZIP codes using C#
Great Circle SQL
Great Circle 2
Use GeoHash.org (either as a web service or implement the algorithm). It hashes your lat-long coords into a form that appears similar for nearby places. For example A may have a hash like "akusDf3af" and B might have a hash like "akusDf3b2" if they are nearby. Then do a SOQL query that looks for places starting with the same n characters as a known location. Your n will determine the radius of the lookup.
These are some great technical solutions that can provide very exact answers, but two things to consider:
geospatial proximity does not map neatly to responsibility
Ownership calculation seems to be done best through postal code lookups or other rules that don't allow for gaps or overlaps. Otherwise, you'll have two (or more) salespeople fighting over leads that are close to both of them, and ignore those leads that are far away from both of them.
So, if you're using geo-calculations like those above to assign ownership, just acknowledge the system will leak and create business rules to accomodate for that. But a simple postal lookup to define territories (as salesforce's own territory management feature does) might be better.
I'd suggest the problem we're trying to solve geospatially is not who owns which lead. Rather, given all the leads you own, which are nearby?
maps often offer more data per pixel than columnar reports
Again, geospatial data in a report may not be the best answer. A lead 50km away, but along a major road, is more interesting than another lead 50km away on the other side of a mountain or lake. Or a lead close to other leads is more interesting than a lead by itself.
A report can't show this, but a map can.
Salesforce has some great examples of Google Maps integrations. Instead of a columnar report called "My Nearby Leads", why not a visualforce page, with a google map inside? You're giving the user far more information than a columnar report could. They might like it better, and it's easier to implement than trying to calculate some of the equations above.
Just another perspective that may (or may not) be appropriate to the problem at hand.
This post is really old, but is showing up at the top of Google results, so I figured I would post some info to it anyways.
2 nice mapping tools are batchgeo.com and geocod.io. Geocod.io can even give you lat and long coordinates from an address.
If you just need a one time calculation, you can use Excel. Export all your leads with the lat and long. Then go to Google Maps and get the lat and long in decimal degrees for the city center of wherever you want to measure to.
Then use this formula in excel to calculate the distance between the coordinates in miles. Lat1dd and Long1dd are the coordinates for one point, and the lat2dd and long2dd are coordinate points for the other point.
=3963*ACOS(COS(RADIANS(90-lat1dd))*COS(RADIANS(90-lat2dd))+SIN(RADIANS(90-lat1dd))*SIN(RADIANS(90-lat2dd))*COS(RADIANS(long1dd-long2dd)))
After you run it, just sort the results from smallest to largest to get those results that are the closest.
I haven't done this next part yet, but conceptually it should work. We have a field that lists the major market each account is in. Example, Chicago IL. I am going to build a trigger or formula field that essentially says IF(Market="Chicago IL") then use X and Y for the lat and long. These will be hardcoded as the city center for that specific market. The query will then run each individual account's lat and long against the one from the city center to calculate a distance.
If you wanted to break the market into different zones, you could adjust your formula so it uses < and > on the lat and long fields. Everything less than X but greater than Y goes in Zone A, etc.
Hope this helps someone.