Get all GPS location points of a given route ( PHP ) - php

I was searching a method to get all GPS coordinates of a given route without using Google maps JS library, since I wanted to get data on server end.
So I have figured it out and posted the solution as an answer. Hope this will be useful for somebody :)
Cheers !!

Using Google Directions API you can get the "overview_polyline" of a route. And through a polyline decoder you can get all the GPS coordinates on the given route.
Example link :-
https://maps.googleapis.com/maps/api/directions/json?origin=Panadura&destination=Moratuwa&key=YOUR_KEY
above link will return all the steps as a json response and at the end of the response there's a element named "overview_polyline" ;
Example response :-
"overview_polyline": {
"points": "_p_h#ilzfNQtAWOiAk#c#OeAJgBn#eAj#cAf#wChBo#f#OXGf#GLKFWFBz#TlAJj#b#jARt#Hv#R`BRdANx#LZz#fBv#tAb#n#Zt#d#pAd#rADNH`#Vn#TV`#ZtAlAf#x#Jh##^?zAQhBWrBMrBAl#G~#Mr#On#s#`BU|AKxAHv#Az#KjBi#~DEp#Dl##h#A\\GtAF`#Vd#FZAl#KhAE\\f#Xf#^RVP^?v#MrASfECzD?fCCpC#La#BiAFoAFmCPoBPuCb#_ARcAVy#Xo#\\_At#uAnA}AnAOLABCBA#]|#Ib#Gx#UlDCdBWnGQpCHj#BJ#BBFALEDYvBWdAM\\k#xAs#pAs#dAi#n#oBjBoAbA}#h#SHaL|CiWnHgLzCuD`A{Cl#aKlB_BX{A^kFvA}#XyEfBoBr#uBv#gDnA{E|A{GrBmHdBmD|#aIzBkBf#mCj#sB^{Ch#mCh#yD|#}GjB_EnAoDbAoCp#oDz#cDx#gDv#gIhBoB^cALuAN}ALmCLeDDaGAoB#iBHaEZyBX{Fp#eFl#uAJeBDgA?kACaAGkAMyAYeBa#cCy#wHqCwAa#mEs#mC_#{CUmIc#oCIw#IsAW}Bc#IBK?IEGK?KBK\\[JOV{#p#yC?u#K_BSsBCk#?iCFm##QEs#GYS[K[A]B]As#K_A?oA#cA"
},
using a polyline decoder you can get the all GPS coordinates. There are number of polyline decoders available on the internet.
Here are some,
Javascript : https://github.com/mapbox/polyline
PHP : https://github.com/dyaaj/polyline-encoder
Google Maps : https://developers.google.com/maps/documentation/utilities/polylineutility
Make sure to replace \\ (double backslash) into \ (single backslash) in polyline string.
if you still want to get points according to specific distance split ( example: 200m ) you may can write a code to measure distance on adjacent locations and build up a points array by removing unwanted points.

Related

Google maps api: get length of polyline in Serverside

I have polyline data in mysql. I want to measure its length in php. I have looked distance matrix api. But it requests origin_addresses and destination_addresses , i haven't got those values.
I found decodePolylineToArray gist but when i decode my polyline, there are a lot of values. How can i measure length of polyline? I am using php but if you have a method in another language, i can port it to php.
You need to use Haversine's formula for every Path in the polyline:
http://en.wikipedia.org/wiki/Haversine_formula
For different programming languages search for :
https://www.google.ch/search?q=php+haversine+formula&aq=0&oq=php+Haversine&aqs=chrome.1.57j0l3j62l2.8586&sourceid=chrome&ie=UTF-8
This is a good sample for manual calculation in JS Calculate distance between two points in google maps V3, but there is a simpler way:
google.maps.geometry.spherical.computeDistanceBetween(path.getAt(i), path.getAt(i+1));
Check out this sample:
http://www-users.mat.umk.pl/~kaskader/polyline_simple.html
that uses it.
HTH

Calculating driving distance using zip codes

How can I code something which'll calculate the driving distance between 2 zip codes within the USA in PHP. An API or web service that has SOAP or REST will also do.
But I understand GoogleMaps API can't be used because it violates the TOS.. So I need a way without using Google Maps, because I have no intention of using their maps, just calculate the distance.
There isn't a whole lot out there that's free, other than Google Maps. The MapQuest API may give you what you need. Here's their guide to the service - it addresses getting driving distances/times between a set of points. Their terms of service don't appear to require that you display a map, but take a look and see if that meets your needs.
Google Maps Distance API has this exact Functionality. You can get a return as either xml or json.
Here is the documentation
https://developers.google.com/maps/documentation/distancematrix/
Here is a sample of the request url :
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false
One nice thing is you dont have to calculate the long/lat first

How can I get the Latitude and Longitude of a ZIP Code?

I need some help to get the Latitude and Longitude dynamically of a ZIP Code entered by the user. so that i can work to show the shop address on those area of zip code.
Google is running some kind of geocoding, you should check it out, it might help you
http://code.google.com/apis/maps/documentation/geocoding/
http://code.google.com/apis/maps/documentation/geocoding/
There is some requirements for that like you have to register your google account for that but it's very easy and you have to pass some parameter which is describe at there.
You can make using JSON or XML as per your requirements.
You haven't specified what country you're looking to do this for but you can use the GeoNames API for postalCodeSearch.
For example (XML and JSON responses respectively)
http://api.geonames.org/postalCodeSearch?postalcode=90210&username=demo
http://api.geonames.org/postalCodeSearchJSON?postalcode=90210&username=demo
Going a little more in depth, you can get a JSON encoded object from google from the following url:
http://maps.googleapis.com/maps/api/geocode/json?address=ZIPCODE&sensor=false
just replace the ZIPCODE with the zipcode that you would like and it should return to you a json object with the latitude and longitude coordinates.

How to get list of places near given latitude and longitude

What I'm trying to create, is an app that will allow me to download list of places near given coordinates. With Wikipedia api it's fairly simple:
http://api.geonames.org/findNearbyWikipedia?lat=47&lng=9&username=some-username
This code run with curl in php gives me an xml response with list of places and possibly their descriptions along with exact coordinates matching the given ones (which are not exact). Is there a way I can do it as simple as above, with GoogleApi? This link: http://code.google.com/intl/pl/apis/maps/documentation/mapsdata/developers_guide_protocol.html#SpatialSearch tells very briefly about spatial search, and I can't find any info on this topic in the internet - like where can I find my user and mapid, how can I get my authorization token.
I know, that this may seem a stupid question for you, but google maps api doc is vast, and I just can't find anything more, than in this link above.
In sum:
I don't want to embed google map on my page, I just want to get places near given coordinates, and list them to my users, and I have to do this on server-side.
At the very top of that page, it says "Google Maps Data API (No Longer Available)", so I don't think you should use that.
Instead, look at Google Places API. It's only a developer preview, but looks like what you need.

google Maps getDistance without loading the page

Google maps has a function that lets you retreive the distance between two points: getDistance(), of the GDirections class.
Now, this is Javascript, so it'll only be able to calculate once the page has loaded.
My search form however, has the ability to indicate what's the maximum distance you want between yourself and another person and based on that, a list of search results has to be provided.
Since I can't use this function in PHP, is there another way for me to calculate the distance between 2 points on the earth? By giving up the street, postal code and city name, just like what Google maps needs.
This page has a list of google maps parameters for use in http connection. By specifying the output paramter you can choose to give back kml files or similar and can be used in any lanuage that can make http connections.
Looks like you want to calculate a Great Circle Distance
Formulas have been discussed here on stackoverflow before.
A point on earth is defined by it's latitude and longitude. If you want to calculate the distance between 2 points on earth by giving up the street, postal code and city name, you will need geo-referene data.
This data is available for free on the internet, but the accuracy and availability differ greatly from region to region. (USA data is of good quality, data for Kenia for example will be harder to come by)
So to answer your question:
Yes, there are other ways to calculate what you want. But they require more work/are more complex than just querying the google API.
You might want tot read: Creating a Store Locator with PHP, MySQL & Google Maps
Hope this points you into the right direction.
You can use kml file. It's xml-formatted file that you can recieve by link like http://maps.google.com/maps?f=d&hl=en&saddr=<src latitude>,<src longitude>&daddr=<dist latitude>,<dist longitude>&ie=UTF8&0&om=0&output=kml
in recieved file you can parse and summate distances from

Categories