Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native ionic 2 geolocation is not working on android device

I am using the native Ionic 2 Geolocation plugin and it does not work as expected. First, I add the plugin.

ionic plugin add cordova-plugin-geolocation

I then import the plugin and issue a call to get the current position.

import { Geolocation } from 'ionic-native';
import { Geoposition, GeolocationOptions } from 'ionic-native/dist/plugins/geolocation';

export class MyComponent {
 protected getLocation():Promise<Geoposition> {
  let options:GeolocationOptions = {
   maximumAge: 0, timeout: 5000, enableHighAccuracy: false
  };
  return Geolocation.getCurrentPosition(options)
   .catch(error => { console.error(error.message) };
 }
}

I've also checked my AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I noticed that the timeout is always exceeded. If I leave it to the default (timeout is set to infinity by default), then around 20 minutes later, I do actually get a geo position. Of course, 20 minutes is not ideal.

I also tried using the navigator as follows. But I get the exact same results.

navigator.geolocation.getCurrentPosition(
 position => {
  console.info('using navigator');
  console.info(position.coords.latitude);
  console.info(position.coords.longitude);
 },
 error => {
  console.warn('using navigator');
  console.warn(error.code);
  console.warn(error.message);
 },
 options
);

When testing on Chrome, the code works as expected; the only difference is that I get a popup asking if I want to allow the page to access my location.

I saw a lot of people have the same problem on SO with Ionic too, but that is with version 1. I wanted to ask this question in the context of Ionic 2.

By the way, I've also updated Cordova and the version is showing up as 6.0.2.

npm update -g cordova

Version information

  • Ionic 2.0.0-beta.30
  • Cordova 6.0.2
  • Gulp CLI 1.2.1 and Local 3.9.1
like image 284
Jane Wayne Avatar asked Jun 22 '16 03:06

Jane Wayne


People also ask

How do I ask for location permissions in ionic?

If location is switched off, you can use switchToLocationSettings() to open the device location settings page to allow user to enable location services, or you can use the cordova-plugin-request-location-accuracy to request the desired location accuracy without needing the user to manually do this on the location ...


3 Answers

Use this code:

if (navigator.geolocation) {
      var options = {
        enableHighAccuracy: true
      };

      navigator.geolocation.getCurrentPosition(position=> {
        console.info('using navigator');
        console.info(position.coords.latitude);
        console.info(position.coords.longitude);
      }, error => {
        console.log(error);
      }, options);
    }

Remember to import Geolocation:

import { Geolocation } from 'ionic-native';

And add plugin:

ionic plugin add cordova-plugin-geolocation
like image 97
Felipe Fernandes Avatar answered Oct 21 '22 02:10

Felipe Fernandes


Probably I found a solution. The problem was with mine GPS module. Eventually it stopped find my location at any site. I reloaded my phone and now everything works.

So at first check if you can determine your location on site such maps.google.com. If it can't determine your location, then the problem is with your phone. Try to reload it or reflash it.

like image 1
likerRr Avatar answered Oct 21 '22 03:10

likerRr


Install both cordova dependencies

cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist

Add this permissions to AndroidMainfest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

Please keep in mind, when running ionic cordova run android -l geolocation will throw this unsecure error. Test it on ur browser or do a simple run with

ionic cordova run android

It solved the problem for me, hope it helps someone else.

like image 1
Jaime Yule Avatar answered Oct 21 '22 01:10

Jaime Yule