【发布时间】:2021-04-23 10:22:00
【问题描述】:
如果初始位置不正确,我还想在移动标记后在弹出窗口中显示 lat 和 lng。 移动标记后是否可以在弹出窗口中查看新坐标?
<script>
var map = L.map('map').fitWorld();
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
}).addTo(map);
function onLocationFound(e) {
var radius = e.accuracy / 2;
var myMarker = L.marker((e.latlng), {draggable: true}).addTo(map)
.bindPopup("You are within " + radius + " " + e.latlng +" meters from this point").openPopup();
myMarker.on("dragend", function(e){
var newCoords = e.target.getLatLng().toString();
});
L.circle(e.latlng, radius).addTo(map);
}
function onLocationError(e) {
alert(e.message);
}
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({setView: true, maxZoom: 16});
</script>
【问题讨论】:
标签: javascript leaflet geolocation latitude-longitude marker