Cordova Geolocation plugin

Anyone using Geolocation plugin as of now in your projects?
Cant seem to get co-ordinates from the mobile device but can get it on broswer. Can someone please help.

I use Geolocalisation plugin in an Android App, the GPS coordinates provided by the mobile are deprecated and are now given by the browser. But the result is identical to the user.

This plugin is based on the W3C Geolocation API Specification.

Can you show me an example implementation. I am trying a lot but I am not able to make it work in the app. Please can you show me your AndroidManifest.xml and config.xml and how are you implementing your code?

In order to get a postition I use watchPosition instead of getCurrentPosition. I set a timer to stop watching after a few seconds. I keep the result that’s the most accurate. I found that I get more precise results this way, even if i have to wait a couple of seconds more to get a position this way.

Example below

  watchTries=0;

  lastAccuracy = 10000.01;
  appdata.state.lastLat = "Watching High Accuracy";
  appdata.state.lastLng =  "Watching High Accuracy";

   watchId = window.navigator.geolocation.watchPosition(
    function(position){
        watchTries++;
        console.log("Got a watch try!");

        if(position.coords.accuracy<lastAccuracy){
          lastAccuracy=position.coords.accuracy;
          lastPosition.coords.latitude = position.coords.latitude;
          lastPosition.coords.longitude = position.coords.longitude;
          lastPosition.coords.accuracy = position.coords.accuracy;
          lastPosition.coords.altitude = position.coords.altitude;
          lastPosition.coords.speed = position.coords.speed;
          lastPosition.coords.heading = position.coords.heading;

        }
    }, function(){
      //error
    }, {
      maximumAge: 250,
      enableHighAccuracy: true
    }
  );

  window.setTimeout( function () {
      window.navigator.geolocation.clearWatch( watchId );
      console.log("sending after Watch tries: "+watchTries );
      if(watchTries>0){


         onGeoSuccess(lastPosition); // do what you want in this function with the location you got!! 


      }

      $localstorage.countLocations(gotTotalCallback);

      },
      25000 //stop checking after XX seconds
  );
  }

In “AndroidManifest.xml” you must have:

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

I think “navigator.geolocation.watchPosition…” is good enough

I tried everything. I cant seem to capture location. What is wrong?

Got to the root of it finally!

I had disabled location services explicitly in my phone. :frowning:

Hi there…

Can u send me the full code ??