Leaflet - 使用latLon +距离(米)+角度(度)创建标记

问题描述 投票:2回答:1

如何计算距离标记A的给定距离(以度为单位)给定距离的新点(标记B)?

谷歌API有这个,但我在传单中找不到它:

var pointA = new google.maps.LatLng(25.48, -71.26); 
var radiusInKm = 10;
var pointB = pointA.destinationPoint(90, radiusInKm);

enter image description here

javascript leaflet
1个回答
2
投票

您可以使用destinationLeaflet.GeometryUtil方法计算目标点并在那里创建一个标记:

var center = [40.69, -73.98];
var radiusInKm = 10;
var angleInDegrees = 90;

var A = L.marker(center).addTo(map);
var B = L.GeometryUtil.destination(markerA.getLatLng(), angleInDegrees, radiusInKm * 1000);
L.marker(B).addTo(map);

和一个演示

var center = [40.69,-73.98];
var radiusInKm = 10;
var angleInDegrees = 90;

var map = L.map('map').setView(center, 11);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var marker = L.marker(center).addTo(map);
L.circle(marker.getLatLng(), {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.2,
    radius: radiusInKm * 1000
}).addTo(map);

var to = L.GeometryUtil.destination(marker.getLatLng(), angleInDegrees, radiusInKm * 1000);
L.marker(to).addTo(map);
html, body {
  height: 100%;
  margin: 0;
}
#map {
  width: 100%;
  height: 100%;
}
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>

<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>
 
 <script src="https://npmcdn.com/leaflet-geometryutil"></script>
 
 <div id='map'></div>

如果你想避免使用外部库,你可以从destination method source code(目前在712行)获取灵感。

© www.soinside.com 2019 - 2024. All rights reserved.