let lat;
let lng;
getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
"Longitude: " + position.coords.longitude);
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
console.log(this.lat);
}
I am attempting to retrieve the user's current location upon application launch using HTML5 geolocation functionality. Surprisingly, it works perfectly in JavaScript but encounters issues in Angular 2, which utilizes TypeScript.