Ionic Native Geolocation not working on Android

I have the following code using Ionic native geolocation :

import { Geolocation } from 'ionic-native'; this.platform.ready().then(() => { alert('loadMap about to getCurrentPosition'); Geolocation.getCurrentPosition(options).then((position) => { alert('loadMap getCurrentPosition'); let latLng: google.maps.LatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); bound.extend(latLng); this.load(bound); }); }); 

When I run it as ionic serve in the browser, or I create it and run it on iOS Simulator ( Xcode ), it works. However, when I build it for Android and try to run it, the first warning will be triggered, but not the second.

This means only for Android , Geolocation.getCurrentPosition... does not work.

I have another page that can display a map through this.map = new google.maps.Map(htmlElement, mapOptions); , so it seems like the problem is getting the current position. When I install the application, I get this message:

 Allow AppName to access the device location? DENY ALLOW 

Which I click on Allow .

Does anyone know what I'm doing wrong, or if there are some missing steps during the installation and build of Android?

thanks

+5
source share
3 answers

Good, so I struggled with this for a while. Sometimes it works, sometimes it doesn’t. Try adding the following options to it:

 let options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 3600}; Geolocation.getCurrentPosition(options).then((resp) => { 
+3
source

The following steps did the trick for me:

Install the last two ionic-cli plugins:

 sudo npm install -g ionic@latest npm install --save-dev --save-exact @ionic/ cli-plugin-cordova@latest npm install --save-dev --save-exact @ionic/ cli-plugin-ionic-angular@latest 

Then install the geolocation plugin:

 npm install @ionic-native/geolocation --save ionic cordova plugin add cordova-plugin-geolocation 

Then delete the "/ node_modules" folder and clear the npm cache:

 rm -rf node_modules/ npm cache clean --force 

And finally reinstall the packages:

 npm install 

Hope this helps someone :)

+1
source

 let options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 3600}; 

this is like starting a geolocation, but cannot catch lat and lng return {}

0
source

Source: https://habr.com/ru/post/1265237/


All Articles