Google maps polygon optimization

霸气de小男生 提交于 2019-12-04 03:17:27
Unreason

I think MapShaper can do this online

Otherwise, implement some algorithm

Found this simple javascript by Bill Chadwick. Just feed in the LatLng to an array and pass in to the source arguments in a function here Douglas Peucker line simplification routine

it will output an array with less points for polygon.

 var ArrayforPolygontoUse= GDouglasPeucker(theArrayofLatLng,2000) 
 var polygon=new google.maps.Polygon({ 

    path:ArrayforPolygontoUse,
    geodesic:true,
    strokeColor:"#0000FF",
    strokeOpacity:0.8,
    strokeWeight:2,
    fillColor:"#0000FF",
    fillOpacity:0.4,
    editable:true
  });

theArrayofLatLng is an array of latlng that you collected using google maps api. The 2000 value is kink in metres. My assumption is, the higher the value, more points will be deleted as an output.

For real beginners: Make sure you declare the js file on your html page before using it. :)

<script type="text/javascript" src="js/GDouglasPeucker.js"></script>

If you can install postgis which i think is easy as they provide an installer then you can import the data and execute snaptogrid() or st_simplify() for which i cannot find an equivalent in mysql.If you decide to go with postgis which i recommend cause it will help you down the road i can provide you with the details.

Now for an easy custom solution you can reduce size by cutting or rounding some of the last digits of the coords and then merge the same coords resulting actually in a simple snaptogrid().

Hope it helps

Mostly likely what you want to divide the points into 2 half and want to try my Javascript function:

function shortenAndShow ( polyline, color ) {
  var dist = 0, copyPoints = Array ( );
  for ( var n = 0, var end = polyline.getVertexCount ( ) - 1; n < end ; n++ ) {
    dist += polyline.getVertex ( n ).distanceFrom ( polyline.getVertex ( n +1 ) );
    copyPoints.push ( polyline.getVertex (n) );
   }
   var lastPoint = copyPoints [copyPoints.length-1];
   var newLine = new GPolyline (copyPoints, color, 2, 1);
   gmap2.addOverlay ( newLine );
} 

I agree the Unreason's anwser,The website support GeoJson,I used it in my website,and it cut down my geoJson ,But I think you also need this world country geo Json

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!