For a project I need encoded geographical polylines in Google Maps. A geographical polyline can be used to show a route or an area on a map. A polyline is made of several geographical points. Each point is composed of latitude and longitude. Example for a polyline:
[
// point
{
// latitude
Latitude: 49.75121628642191,
// longitude
Longitude: 6.6281890869140625
},
{
Latitude: 49.76252796566851,
Longitude: 6.633853912353516
},
{
Latitude: 49.757537844205025,
Longitude: 6.649990081787109
},
{
Latitude: 49.749441665946,
Longitude: 6.642951965332031
}
]
If you load a big bunch of points from your server, this can take a while. The better solution is to encode them to binary format. In a forum I found a dead link to an Google Maps’ encoding algorithm coded in PHP ;(, so I found one implemented in JS and adapted it to PHP. You must take care of one thing: Replacing the JavaScript function String.fromCharCode() by PHP’s chr() will not work, because it does not support unicode. On php.net I found unichar(), which considers unicode. The client side encoder can be tested here. Download the ZIP which includes a PHP class and the whole thing in JavaScript: polyline.zip