简体   繁体   中英

How To Get Current Location With Pop Up Box Using Mapbox

Is it possible to get current location (latitude and longitude) with mapbox likes vanilla javascript do? What i want is like image number one, but i think using the vanilla javascript location makes the location not accurate, and i want to using the mapbox but it's only provides using the button which must showing the map first.

图片一,使用香草javascript获取纬度和经度

图片二,使用mapbox,但用户必须先点击按钮

So, what i want is how can i get user current location (latitude and longitude) when the page is loaded or when the user click a button without showing the map on mapbox.

The Mapbox example you referenced is using Mapbox' GeoLocate Control , which in turn is utilizing the browsers Geolocation API to determine the current position.

Therefore, there is no advantage in using the Geolocate Control of Mapbox, if you do not want to display the returned position on a Mapbox map.

Example how to get geolocation in HTML page:

 <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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