Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps polygon optimization

I extracted country outline data from somewhere and successfully managed to convert it into an array of lat-lng coordinates that I can feed to Google maps API to draw polyline or polygons.

The problem is that that there are about 1200+ points in that shape. It renders perfectly in Google maps but I need to reduce the number of points from 1200 to less than 100. I don't need a very smooth outline, i just need to throw away the points that I can live without. Any algorithm or an online tool that can help me reduce the number of points is needed.

like image 843
Salman A Avatar asked Mar 14 '11 13:03

Salman A


People also ask

Can you optimize a route in Google Maps?

Google Maps actually has zero route optimization features, meaning you have to do all the calculations manually when you have multiple stops to make.


1 Answers

I was looking for exactly the same thing and found Simplify.js. It does exactly what you want and is incredibly easy to use. You simply pass in your coordinates and it will remove all excess points.

enter image description here

simplify(points, tolerance, highQuality)

The points argument should contain an array of your coordinates formatted as {x: 123, y: 123}. (Afterwards you can convert it back to the format you wish.)

The tolerance should be the precision in decimal degrees. E.g. 0.0001 for 11 meters. Increasing this number will reduce the output size.

Set highQuality to true for better results if you don't mind waiting a few milliseconds longer.

like image 98
Duncan Luk Avatar answered Oct 03 '22 21:10

Duncan Luk