Tôi đang làm việc trên một bản đồ với Mapbox và Leaflet và tôi cho phép người dùng vẽ đa giác và tính toán và hiển thị các đa giác đó và tôi cũng cần cho phép người dùng vẽ một polyline và hiển thị khoảng cách của polyline.Làm thế nào để tính toán khoảng cách của một polyline trong Leaflet như geojson.io?
Tôi đã tìm ra tính năng vùng đa giác nhưng tôi không thể tìm ra cách tính khoảng cách của đường polyline.
Mã của tôi là như sau:
loadScript('https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js', function(){
loadScript('https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js', function(){
var featureGroup = L.featureGroup().addTo(map);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: featureGroup
},
draw: {
polygon: true,
polyline: true,
rectangle: false,
circle: false,
marker: false
}
}).addTo(map);
map.on('draw:created', showPolygonArea);
map.on('draw:edited', showPolygonAreaEdited);
function showPolygonAreaEdited(e) {
e.layers.eachLayer(function(layer) {
showPolygonArea({ layer: layer });
});
}
function showPolygonArea(e) {
var type = e.layerType,
layer = e.layer;
if (type === 'polygon') {
featureGroup.clearLayers();
featureGroup.addLayer(e.layer);
e.layer.bindPopup(((LGeo.area(e.layer)/1000000) * 0.62137).toFixed(2) + ' mi<sup>2</sup>');
e.layer.openPopup();
}
if (type === 'polyline') {
featureGroup.clearLayers();
featureGroup.addLayer(e.layer);
// What do I do different here to calculate the distance of the polyline?
// Is there a method in the LGeo lib itself?
// e.layer.bindPopup(((LGeo.area(e.layer)/1000000) * 0.62137).toFixed(2) + ' mi<sup>2</sup>');
e.layer.openPopup();
}
}
});
});
Có một phương pháp trong LGeo lib chính nó mà sẽ giúp tôi tính toán khoảng cách của polyline? Các nhà phát triển tại geogson.io cũng có một cách để tính toán khoảng cách nhưng tôi không thể hình dung nó ra nhìn vào mã của họ. Tôi không phải là nhà phát triển Javascript dày dặn. Bất kỳ trợ giúp đều được chào đón. :)
Bravo, giải pháp hoàn hảo! Điều gì về tính toán diện tích của vòng tròn? Làm thế nào tôi có thể nhận được bán kính của một vòng tròn (tạo ra hoặc chỉnh sửa)? – serifsadi