Reputation: 67
I'm stuck with this problem. I'm not able to get the coordinates using Ionic Geolocation. Always return is "{}" .Anybody can help me? This is my code.
import { Geolocation, GeolocationOptions } from '@ionic-native/geolocation';
constructor(
private geolocation: Geolocation,
public platform: Platform,
) {}
this.platform.ready().then(()=>{
this.geolocation.getCurrentPosition(options)
.then((position) => {
console.log('Geolocation successful', JSON.stringify( position));
this.app.alertMsg( JSON.stringify( position) );
// let query = '?lat=' + position.coords.latitude + '&lng=' + position.coords.longitude;
}).catch((error) => {
console.log('Error getting location', JSON.stringify( error));
});
})
Upvotes: 1
Views: 919
Reputation: 1031
Please take a look at this. Cordova Geolocation plugin returning empty position object on Android by Flock Dawson
Apparently, the getCurrentPosition() function returns a 'special' object in Android, which evaluates to {} when using JSON.stringify(). If I outputted the raw return object to the console, it turned out it wasn't empty at all.
And according to https://ionicframework.com/docs/native/geolocation/
you can get the latitude and longitude, etc. by
this.app.alertMsg(position.coords.longitude + ' ' + position.coords.latitude);
Upvotes: 4