Maps API 3 Directions with fuzzy via co-ordinates - google-maps-api-3

I have a number of GPS co-ordinates that describe a route. My intention is to draw a polyline along the route, and then colour segments based on some data I have.
Problem is, the GPS coordinates can occur on the roadside either side of main roads. When using the obvious Directions service solution a lot of "back and forth" occurs as Google tries to get me either side of a split lane road
What I am aiming for is a direct route from A-H passing through every way point. I have considered reverse geocoding the coordinates to a street name and having the directions use the street as a way point, but that picks a specific point on the street that may not be related to the actual route. Single polylines are also not an option as some routes have turns in them.
Is there a way to 'fuzzy' my waypoints so that the directions are happy when passing within a certain radius of the points? If not, has anyone got any other solutions?
Thanks heaps.
Edit: It's also not an option to just not use the middle way points because sometimes the path is not optimal.
Also have tried my own fuzzy coords now, by +- some small value to each consecutive lat/lng pair. Unsurprisingly I ran into OVER_QUERY_LIMIT pretty soon.

Related

How can the HERE Routing API be used to avoid a specific section of road?

I'm trying to get a route that avoids a specific section of a road using Here routing API. I tried passing in a rectangle area to avoid the road like avoid[areas]=bbox:13.082,52.416,13.628,52.62. The problem is that the road I'm trying to avoid is diagonal. Using a rectangle to define the coordinates of the beginning and end of the diagonal road ends up causing nearby roads to be avoided. What would help me is if I could avoid an area in the shape of a polygon. Is there a better way to get directions that avoid a road?
You can use the avoid[segments] parameter to indicate that you want to avoid a specific section of the road. Here's an example request including this parameter:
https://router.hereapi.com/v8/routes?
apikey=*******&
origin=52.508900,13.388871&
destination=52.512990,13.390536&
return=polyline,summary,actions,instructions&
transportMode=car&
avoid[segments]=here:cm:segment:88390186
The easiest way to find out what's the Segment ID for the section of the road you want to avoid is by calculating a route where both the origin and destination are set to the coordinates of that section, and request the segmentId attribute in the spans parameter:
https://router.hereapi.com/v8/routes?
apikey=*******&
origin=52.51143775483142,13.386767963569321&
destination=52.51143775483142,13.386767963569321&
return=polyline&
spans=segmentId&
transportMode=car

HERE Maps Api Reverse Geocode and Street/Road Bearing

Using HERE Maps Api, for a geographic coordinate (lat, lng), I would like to find address information for the nearest routable road. Information that includes:
Address (street, house number, city, postal code, country)
Bearing/Heading of the road (0 to 360 degrees from True North)
How can I achieve this through HERE Maps API?
So far, this is what I have been able to achieve:
Calling the calculate Route API with identical start and end endpoints and mode=car will force the API to snap to the nearest routable point. This step is necessary for when the input coordinates are for a pedestrian street.
Using the nearest routable point coordinates, I can call reverse GeoCode API and I will be able to find the address information I need.
The next step is where I'm currently stuck. I'm able to find information like direction=NW, however I can't find the ~exact bearing in grad, for example bearing=265.
Is this achievable? Are 3 steps really necessary or is there a HERE Maps API endpoint that can provide all of this?
You can use the Fleet Telematics API to get the headings value for a particular road. Read more about the api here - https://developer.here.com/documentation/platform-data/topics/quick-start-view-map-data.html
You can also look at the field explanationshere - https://tcs.ext.here.com/pde/layer?region=WEU&release=19111&url_root=pde.api.here.com&layer=ADAS_ATTRIB_FC1
HEADINGS Horizontal road heading [10^-3 degree] at coordinate points along the link, when driving from Reference Node. Comma separated. Each value is relative to the previous. No values here for the ref/nonref nodes, because they are in the separate fields REFNODE_LINKCURVHEADS and NREFNODE_LINKCURVHEADS.

Create directions on a map based on custom data

So what I'm trying to do is the following:
Have a map (such as Google Maps or questMaps). It doesn't matter at all which API I need to use.
On that map have an overlay on the streets. So say (for example) the street has bad lightning at night, it will be colored red. If it has good lightning it will have a green overlay.
Based on the overlay the map creates a custom route (for example the user only wants to walk on the green/well lit streets).
I have no idea how to accomplish this (especially step 3).
First, you'll have to decide what data you need. How do you categorize certain streets as lit or unlit? What if some parts of a street are well lit and some have no lights? Do you need to know the location of every streetlight in your area? What if lights burn out?
After figuring out what data you need, you need to build your dataset. I'd be VERY surprised if this data already exists, so you will probably need to gather it yourself. Either go around town and take notes, or crowdsource the project, or figure out some other way.
Once you have gathered your data, learn the drawing API of whatever mapping tool you wish to use. They all should have functions in their API for drawing colored lines (for streets) or points (for streetlights) on top of an existing map.
Finally, learn the navigational API of the mapping tool you chose. You're right, this is a hard step. I know Google Maps lets you specify certain waypoints when requesting directions; maybe your app can calculate well-lit waypoints and feed them to Google Maps' Directions service to influence the route it generates.
Good luck!
For custom routing, you need to read up on "Graph Theory". This ignores the geography of the street map, and considers it as a set of junctions (nodes or vertices in the graph theory jargon) connected by edges. You can assign weights to edges - these could be lengths, travel times, ones and zeroes etc. Anything. They can have no relation to the position on the map.
So for your application, you'd assign a large weight to unlit streets, and a small weight to lit streets, then use a standard minimum-weight algorithm to get a route from one node to another.

Finding streets (get their coordinates) and calculating distances between markers

I have trouble finding any information on how to use the API to:
Search for streets and get some clickable results that returns a LatLng object or something (at least coordinates). So If I search for a street and click on a result, I'll pan to that street (for example).
How can I calculate distance between markers? and possibly, draw lines between them.
All I get is the API but no guides so it's fairly hard figuring out what types to use.
Here is a fiddle showing how this can be achieved:
http://jsfiddle.net/foxwisp/vQGMr/1/
To convert street names to lat/lng you need to use a Geocoder such as the one provided by Google Maps API.
Then, when you get back the results from the geocode, you use the lat lng properties to create a marker. Once your first marker is placed, you repeat the process for your second street address. We nest these calls so that we can be assured of the order of execution due to their asynchronous nature.
Once we have our second marker we use Google's polyline function to draw a line between the two latlng marker points.
Then we use a slightly complicated mathematical equation to do some distance calculations and voila.
The Google Maps API is fantastically documented, hopefully this fiddle will put it into context for you and you can explore each element step by step by reviewing functions and properties in the documentation

google maps v3 determine which states are within boundaries of circle overlay

Is there a way to determine which states are within circle overlays created by google.maps.Circle? Perhaps using reverse geocoding and getBounds or contains? Does Google provide a way to do this (using the geocoder or some other method), or must a database of Latitude and Longitude points of states be used?
There doesn't seem to be an easy way to do this within Google Maps API.
You can use getBounds() to get a LatLngBounds object approximating the circle. Note though that getBounds() will return a rectangle that approximates the circle, so you might get inaccurate results. To do it more accurately, you'd need to calculate a bunch of points to approximate the circle using something like the Haversine formula in combination with the cirlce's center and radius. The more points you calculate, the more accurate the approximation.
Once you have your set of bounding points, you're still not exactly out of the woods. There does not appear to be a way to use Google's Geocoder API to return all the states in a LatLngBounds. So you'd have to calculate a bunch more points within the bounds and send individual reverse geocode requests for them. And you still might miss a state or two. Overall, yuck.
Another approach, that doesn't seem that much more appealing to me, but who knows: For each state, get lat/lng data for a bunch of rectangular bounds that, when all combined together, approximate the shape of the state. For each state, use intersects() to see if it intersects with your circle.
There may be other possibilities, involving Google Maps API or other technologies, depending on your use case.

Resources