I am trying to use Geolocation but I get the below error
TypeError: Object(…) is not a function
at Geolocation.getCurrentPosition
Ionic Version 3.20.0
cordova-plugin-geolocation 4.0.1 “Geolocation”
my code
import { Geolocation } from ‘@ionic-native/geolocation/ngx’;
mylocation(){
this.geolocation.getCurrentPosition().then((resp) => {
this.lat = resp.coords.latitude;
this.longt =resp.coords.longitude;
}).catch((error) => {
console.log(‘Error getting location’, error);
});
}
any idea what I can do
Thank you
1 Like
if you found solution tell me please , i have the same problem
Hi,
If you wants please follow and understand then apply given coding.
platform.ready().then(() => {
this.geolocation.getCurrentPosition({enableHighAccuracy: true}).then((resp) => {
this.lat= resp.coords.latitude;
this.lng= resp.coords.longitude;
console.log(this.lat);
console.log(this.lng);
this.reverseGeocoding();
}).catch((error) => {
console.log(‘Error getting location’, error);
});
});
Then converting lat and lng for given coding.
reverseGeocoding(){
this.platform.ready().then(() => {
let options: NativeGeocoderOptions = {
useLocale: false,
maxResults: 5
};
this.nativeGeocoder.reverseGeocode(this.lat,this.lng,options)
.then((result: NativeGeocoderReverseResult) =>{
console.log(JSON.stringify(result[0]));
this.data.addrss = result[0].subLocality +","+ result[0].locality +","+
result[0].subAdministrativeArea +","+ result[0].administrativeArea +","+
result[0].postalCode +","+ result[0].countryName;
console.log(this.data.addrss);
}).catch((error: any) => console.log(error));
});
}
Please properly understand and then apply above coding for geolocation.
Thank you!
This is ionic 3 and Angular Js 5.
Hi,
Sorry for the delay response, Please use two plugins 1.Geo coder 2.Geolocation.
1 link is https://ionicframework.com/docs/native/native-geocoder
2 link is https://ionicframework.com/docs/native/geolocation
install two plugins then use above coding properly it will working…
Thank you! If you got a solution ,click the solution but bcoz users find what is solution.
Hi! Im facing the same issue. I’ve already made some changes like update rxjs to 6.0 version, put /ngx in native-geocoder and Geolocation package imports. But nothing worked.
@karthikaivel I’ve also tried your code above but it throws the same error.
Moreover, I tested only the code for getCurrentPosition in platform.ready, without to call reverseGeocoding function, but I still get the first error
“Uncaught (in promise): TypeError: Object(…) is not a function
TypeError: Object(…) is not a function”
Found the fix. At least the above worked for me.
I checked my depedencies in package.json file. Not all of my ionic-native plugins were in the same version. (5.2.0), so I update each of them with the command
npm install --save @ionic-native@(plugin_name)@latest
Also I update rxjs and added rxjs-compat with the above command
npm i rxjs@^6.0 rxjs-compat
Finally, geolocation must be imported with the above way
import { Geolocation } from '@ionic-native/geolocation/ngx';
Hope it helps. getCurrentPosition doesn’t throw any error
1 Like
If working with ionic 3, use these to install the geolocation plugin:
ionic cordova plugin add cordova-plugin-geolocation
npm install --save @ionic-native/geolocation@4
5 Likes
i tried to arrange the code, because I think the problem in the code
constructor(public navCtrl: NavController,private geolocation: Geolocation) {
navigator.geolocation.getCurrentPosition(this.onSuccess, this.onError);
}
onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
onSuccess(position) {
let element = document.getElementById('map');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
console.log(element)
}
also, you can see the tutorial here
Hi,
Sorry for that,But properly work for me.
Thank you!
“npm install --save @ionic-native/geolocation@4” also worked for me.