I am trying to grab geolocation for my app. Geolocation should be grabbed on created hook.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocationInfo);
}
function displayLocationInfo(position) {
const lng = position.coords.longitude;
const lat = position.coords.latitude;
console.log(`longitude: ${ lng } | latitude: ${ lat }`);
}
But my console is empty.
The real way to use Geolocation API is to check the availability of it on your device/browser before using it, so just add an else
block to your existing code. Hope it'll help :) See more at MDN
if ("geolocation" in navigator) { /* geolocation is available */ navigator.geolocation.getCurrentPosition(displayLocationInfo); function displayLocationInfo(position) { const lng = position.coords.longitude; const lat = position.coords.latitude; console.log(`longitude: ${ lng } | latitude: ${ lat }`); } } else { /* geolocation IS NOT available */ console.log('geolocation IS NOT available on your browser'); }
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.