简体   繁体   English

Leaflet.js 圆到多边形的转换

[英]Leaflet.js Circle to Polygon Conversion

I'm using Leaflet.js to save coverage maps and am giving the user the option of using polygons or circles.我正在使用 Leaflet.js 来保存覆盖图,并为用户提供使用多边形或圆形的选项。

To keep all objects in the same format, I'm converting the circle to a polygon before saving.为了使所有对象保持相同的格式,我在保存之前将圆转换为多边形。

However, when I then reload the circle it is oval-shaped.但是,当我重新加载圆圈时,它是椭圆形的。

I know that this is due to the earth's curve but I'm unsure how to correct my method to take this into account?我知道这是由于地球的曲线,但我不确定如何纠正我的方法以考虑到这一点? (I've looked but can't find anything that gives the solution I'm after). (我已经看过但找不到任何能提供我所追求的解决方案的东西)。

The main issue is the javascript method I'm using below as that doesn't take into account the earth's curve.主要问题是我在下面使用的 javascript 方法,因为它没有考虑到地球的曲线。

 // GenerateCirlcePolygon - Creates Circle from 360 line Segments 
 function GenerateCirlcePolygon(origin, radius) {
     var earthRadius = 6371;

     //latitude in radians
     var lat = (origin.Latitude * Math.PI) / 180;

     //longitude in radians 
     var lon = (origin.Longitude * Math.PI) / 180;
     //angular distance covered on earth's surface
     var d = parseFloat(radius) / earthRadius;
     polyPoints = new Array();

     for (i = 0; i <= 360; i++) {
         var point = new VELatLong(0, 0)
         var bearing = i * Math.PI / 180; //rad
         point.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
         point.Longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(point.Latitude))) * 180) / Math.PI;
         point.Latitude = (point.Latitude * 180) / Math.PI;
         polyPoints.push(point);
     }

Any advice at all would be great.任何建议都会很棒。

You can use the built in function from leaflet-geoman : L.PM.Utils.circleToPolygon(circle, sides)您可以使用来自 Leaflet-geoman 的内置leaflet-geomanL.PM.Utils.circleToPolygon(circle, sides)

L.PM.Utils.circleToPolygon(circle, 60).addTo(map)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM