Google maps api: get length of polyline in Serverside - php

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

Related

Get all GPS location points of a given route ( 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.

How can I get clusters from distance matrix, using PHP?

I have distance matrix as two-dimensional array, like this:
So, I need to find clusters, of elements with its help. I can do it, using hierarchic clusterization, like k-means. I have found such example here PHP K-Means
How can I convert my two-dimensional array into array of points, listed in this example?
$points = [
[80,55],[86,59],[19,85],[41,47],[57,58],
[76,22],[94,60],[13,93],[90,48],[52,54],
[62,46],[88,44],[85,24],[63,14],[51,40],
[75,31],[86,62],[81,95],[47,22],[43,95],
[71,19],[17,65],[69,21],[59,60],[59,12],
[15,22],[49,93],[56,35],[18,20],[39,59],
[50,15],[81,36],[67,62],[32,15],[75,65],
[10,47],[75,18],[13,45],[30,62],[95,79],
[64,11],[92,14],[94,49],[39,13],[60,68],
[62,10],[74,44],[37,42],[97,60],[47,73],
];
First: a nitpick: k-Means is not a hierarchical clustering algorithm, see https://www.quora.com/What-is-the-difference-between-k-means-and-hierarchical-clustering for details o the difference.
Second: you don't want to convert a distance matrix back to the points it originated from as you take a step back. Sadly the k-Means implementation you linked only has an API that allows you to enter raw coordinates and assumes Euclidean distance, therefore you have some possibilities, depending on your requirements:
Where do you get the distance matrix from? If it is possible, get the raw coordinates (and make sure the distance measure is euclidean distance) and use the library you linked.
Override the Point class in the library you linked: specifically the getDistanceWith method to return values from your matrix
If you only need to calculate the cluster once, use python and sklearn. This library does exactly what you want. Especially: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.cluster.hierarchy.linkage.html
Write your own code: clustering is quite an easy topic and therefore it is a nice coding exercise.

Generate countries map based on geoip data

I have searched a lot on this topic, and have found no answers.
I have my own statistics package where I'm saving the geoip data of my users (along with a bunch of other data). I'm using the maxmind geoip library to get this information.
So, in my backend I'm visualizing this data as text that have basically two columns, one for the country name and another for the number of visits from the country.
I'd like to generate a map with this data.
something like a world map with the countries I have visits from highlighted.
Heat mapping would be nice, but not required.
I dont really care if it's generated with php (GD image library) or jquery, since I'm already using both those technologies for the statistics backend. But I'd REALLY like to do this without google analytics or their graphing APIs.
I will try to suppose. The way I would try do it ...
Get FullHD (or HighRes world map)
Coordinates I'd keep in WGS84 standard (float values, otherwise it is easy to convert)
I would try to approach real coordinates to the scale of image, but before this ...
The major part in this work is Math. I'm actually not a mathematician, but I know that it should be applied here and why.
Main goal here is to project coordinates on a flat surface, bec. WGS84 uses oblate spheroid as a reference surface (with radius = 6378137 m; flattering = 1/298.257223563), so it is not ideal circumference and it should be taken into account. + image should be GEO binded somehow (you should know coordinates of corner angles of this image #least. it is the easiest case if so).
Calculations for this case are not very massive, everything leads to the elementary plane geometry.
Here is the library that could help you working with geospatial data http://www.gdal.org/.
My advice to you, to consult some specialists in this field if you know nothing about it (maybe SatCom spec. or MobCom spec. in some university/academy) (or try Google if you are familiar enough with math and GEO) and to ask for a math model for projection of GPS coordinates to the flat surface and you will definitely get the answer.
If you don't need very high accuracy, try it yourself maybe you'll have luck.
You can try yourself in MatLab (more applicable in this case) or Mathcad if you know math enough and try yourself to position few points on the raster.
If you will find the answer in the nearest future, I would be glad if you post it here or share with me your solution for the particular case.
i need to finally answer my own question here. for anyone else who stumbles across this:
i have been using the d3js library with the topojson extension.
https://github.com/mbostock/topojson

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

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