首页 > 解决方案 > MapBox 从设备浏览器的地理位置检索位置

问题描述

我需要显示 mapbox 地图,在设备的地理定位之后,我需要将 lat,lon 值存储在变量 js 中:

mapboxgl.accessToken='pk.xxxxxxxxxxx.xxxxxxxxxxxxx';
var coordinates = document.getElementById('coordinates');
var map = new mapboxgl.Map({
container: 'map',
center: [1.000, 1.000],
zoom: 1,
style: 'mapbox://styles/mapbox/streets-v11'
});

map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl
})
);
map.addControl(new mapboxgl.NavigationControl());

map.addControl(
new mapboxgl.GeolocateControl({
positionOptions:{
enableHighAccuracy: true, timeout: 5000, maximumAge: 0 
}, // I need to retrieve lat/lon from here
fitBoundsOptions: {maxZoom:20},
showUserHeading: true,
showUserLocation: true,
trackUserLocation: true
})
);

var geojson = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point', coordinates: [94.00000,1.00000],},
properties: {title: 'Branch', description: 'nome'}
*}]*
};  

geojson.features.forEach(function(marker) {

var marker = new mapboxgl.Marker({draggable:true})
.setLngLat(marker.geometry.coordinates)
.addTo(map);

function onDragEnd() {
var lngLat = marker.getLngLat();
document.getElementById("longitude").value = lngLat.lng;
document.getElementById("latitude").value = lngLat.lat;
document.getElementById("upbranch").submit();
}

marker.on('dragend', onDragEnd);
}); 

这是 MapBox 的地图代码我需要从地图地理位置检索坐标并在输入字段中使用它来存储数据库中的位置

标签: javascriptmapbox

解决方案


推荐阅读