Google Maps Directions weird route path - google-maps

I developed an app that sends GPS points to a server and the server convert those GPS points into a path, which is shown in a map.
Now the server makes a call to Google Maps Directions service and gets a response. From that response I use the value overview_polyline, which based on the documentation:
Contains an object holding an array of encoded points that represent
an approximate path of the resulting directions.
The problem is that sometimes the line I get is really weird. Sometimes it gives me a path where there's no street at all.
Here are some screenshots to describe my problem. The yellow marker is the start point (A), and the blue marker is the end point (B).
So I asked Google to give a route from A to B, with driver mode, so it shows the actually path.
And this is the result:
This is the API call I use. My question is: Any idea why would it return that path?

http://www.geocodezip.com/v3_example_geo2.asp?addr1=53%20Imperial%20Avenue,%20Westport,%20CT%2006880,%20USA&addr2=2%20Harborview%20Road,%20Westport,%20CT%2006880,%20USA&geocode=1&geocode=2&type=m
Looks to me like the encoded polyline is not being rendered correctly. How is your code handling that?
example using the geometry library decodePath function
Looks to me like Google broke their polyline encoding.
If I paste it into here (which admittedly doesn't expect polylines, it expects polyons)
I need to escape the "`", change:
{pazFraw~L|#|#f#^`#N\\?XIx#a#n#[
to:
{pazFraw~L|#|#f#^\`#N\\?XIx#a#n#[
to make it have a more reasonable shape.

Related

Is it safe to use `PolyUtil.encode()` to save important Polygons on Google Maps?

I'm working with a maps application, One of the functionality of this app is to allow the user to draw a polygon on the map for specific task.
This Polygon will be save on our servers for later use.
I managed to encode the points and produce an encoded string path to reduce the size of storing and transferring the polygon from and to the server.
String path = PolyUtil.encode(latLngs); // Android
But I read on Google developer webiste that:
Polyline encoding is a lossy compression algorithm that allows you to store a series of coordinates as a single string. Point coordinates are encoded using signed values. If you only have a few static points, you may also wish to use the interactive polyline encoding utility.
I get a shock when read this :( because I use encodedPath in more than one App.
What does they mean by lossy compression ?
So is it safe to use this type for storing users' polygons ?
I mean: is it true that every decode of an encoded string will produce the same points ?
Or it is better to store the polygon on the server as multi points ?
Thanks for any help.
If you take a look at the encode method that is used to encode a sequence of latitude/longitudes on the Google Maps Android API Utility Library you will see that it rounds the fifth decimal place:
long lat = Math.round(point.latitude * 1e5);
long lng = Math.round(point.longitude * 1e5);
From the Wikipedia, 0.00001 decimal degrees is equivalent to 1.1132 meters at equator.
Anyway, take into account that this is the behaviour now, and it's transparent to you, so you may not notice if the implementation changes to allow more precision or if the precision is more truncated. Thus, if the precision of the coordinates is important to you, you may want to store the coordinates as a list of points on your system.

Google My Maps: How can I export coordinates from a network-linked KML file?

I've been checking out the documentation for Google's "My Maps" service, and I can't figure out if it will let me do what I need to do.
Our client has a map, created using My Maps, that defines several delivery areas using colored polygons. I understand that it's possible to export a KML file containing the coordinates for these polygons, and that it's also possible to create a network link so said KML file will be updated whenever the map is.
My problem: I don't know what to actually do with the KML file. I need some sort of script that can take the network-linked KML, and return a human-readable set of coordinates that I can use to define delivery areas on the client's site. In other words, they need to be able to edit or create delivery zones on this map, then on their site, bind each polygon to a particular store to determine which store must deliver which orders. There's a lot of complicated stuff going on here, but for this first step I just need to know how to get the coordinates for the separate shapes in a reliable way.
I wish I had some code to post to make this a more useful post, but it's more a question of whether or not this type of functionality is actually possible. Please help me figure this out!
Thanks for any and all help.
The KML file is a XML file then you can inspect the file both server side with a XML parser or client side via JQuery.
this is an essential javascript sample for a client side approch:
var request = new XMLHttpRequest();
request.open("GET", filename, false); // filename = kml_filename
xmlDocObj = $($.parseXML(request.responseText));
var placemarks = xmlDocObj.find("Placemark");
placemarks.each(function (index) {
... your parsing for the geometry object and coordinates content you need
}

rendering a json with direction service

when i make a request for a specific latlang direction service responses with the same json. so instead of making the same requests always, i want to store the json somehow.i tried parsing the json and sent to my function which i render the responses, but it didn't work. is something like this even possible?
var direction = JSON.parse('{"routes":[{"bounds":......}');
directionRenderer.setDirections(direction);
It's not possible by using directionRenderer.setDirections() , because not all properties of a directionsResult may be stored in JSON.
But you may of course draw the route on your own, fetch the important details (LatLng's, descriptions etc. out of the directionsResult and use them to 1. draw a polyline and 2. providing route-details)

google maps XML Import

hi I am trying to develop an application where I import an XML file with addresses and then google maps should tell me the closest places according to my current location.
Can anyone please help me with this.
Thanks
You may use google.maps.geometry.spherical.computeDistanceBetween() to calculate the distance between 2 LatLng's. When you didn't know the LatLng's first request them via the geocoding-service.
This will give you the calculated distance between the points.
When you wan't to get the distance based on a route(e.g. driving-distance) request the directions-Service , the response will contain route and the distance. To request distances for multiple routes with 1 request you also may use a DistanceMatrixRequest
Looks like the perfect use case for Distance Matrix. You'll have to parse the XML file first, and then create a distance matrix request that includes all your locations. That will return a response that gives you the distances to all locations.

How to get Google Maps Public Transit Directions by HTTP server-side request

I use such urls like:
http://maps.google.com/?saddr=546%206th%20Avenue,%20New%20York,%20NY%2010011%20(Sixth%20Avenue,%20New%20York)&daddr=W%20103rd%20St,%20New%20York,%20NY%20&dirflg=r
But it returns full html page. Does anybody know how to get such info in json or xml or any parsable format?
I need public transit.
You just add &output=json to the end of the url.
I'd like to extend Mathias's answer a little bit.
There's no official Google Transit API at the momemnt. Transits are provided by agencies, and most of Transits are not public. So, Google is not allowed to open them as API.
You may try to consume the "unofficial" data using your link + "&output=json".
However, the result won't be a valid JSON. Instead, that's something, that can be easily converted to a JavaScript object. (The differences are: there is no quotes around property names,
the strings are not properly encoded etc.)
Imagine you got this JavaScript object. However, it won't allow you to easily get the structured route details. Object's properties contain the route points coordinates, but no descriptions. The only place where the descriptions may be found is 'panel' property, which contains a chunk of HTML text (you may find a link to the sample of HTML in my blog post)
So, you'll have to convert this HTML into XML (X-HTML) and then build the parser of this XML to get the essence data of a trip.
Seems like a bit of overkill to me. Having in mind, that "unofficial" API may change in the future, including slight changes in 'panel' HTML structure that will kill your parser.
#MathiasLin, how did you overcome this?
Google Maps Javascript v3 and the Directions Web Service now has this capability as per :
https://googlegeodevelopers.blogspot.in/2012/06/public-transit-routing-and-layer-now.html
Update:
Latest server side API docs:
https://developers.google.com/maps/documentation/directions/get-directions
Re choosing public transport - it seems that it defaults to public transport now (at least when I use it).
Changing the last URL parameter, dirflg=r, into dirflg=w switches the directions to walking mode.
Plus see:
Walking, bicycle and public transport directions with time required in each mode , iPhone
for more detail about the parameters.
Google direction API response is in HTML, JSON format please check https://developers.google.com/maps/documentation/javascript/directions?hl=lv
& section 'The DirectionsResult Object'