I want to know the user's location when they press on a certain button and push it to the database to a certain table with the user id.
How can I do this in IONIC 5?
I want to know the user's location when they press on a certain button and push it to the database to a certain table with the user id.
How can I do this in IONIC 5?
Welcome to Stackoverflow!
Have you tried '@ionic-native/geolocation' and Google Maps API? You can do something like this:
1 - Get the current location. We have two variables: latitude and longitude
getCurrentLocation() {
let options = {timeout : 10000 , enableHighAccuracy:true};
let locationObs = new Observable(observable => {
this.geolocation.getCurrentPosition(options)
.then(resp => {
this.latitude = resp.coords.latitude
this.longitude = resp.coords.longitude
let location = new google.maps.LatLng(this.latitude, this.longitude);
observable.next(location);
}).catch( error => {
loading.dismiss();
return false;
})
})
return locationObs;
}
2 - Call the function when the button is clicked. To prevent memory leaks, use unsubscribe after you get the location:
buttonClicked(){
let sub = this.getCurrentLocation().subscribe(location => {
console.log(this.latitude, this.longitude)
sub.unsubscribe()
})
}
Let me know if you have more questions.