Search code examples
geolocationionic-frameworkcordova-pluginsvisual-studio-cordova

Geolocation Using Ionic Cordova Returning Incorrect Lat/Long Values


I am facing issue using Geolocation pluggin with Ionic and Cordova framework.

Using Visual Studio Community 2015, with Cordova CLI version :4.3.0 and added the org.apache.cordova.geolocation pluggin to VSC-2015

My controller.js

.controller('MapCtrl', function ($scope) {  
function onSuccess(position) {            
        console.log(position.timestamp);
        console.log(position.coords.latitude + " " + position.coords.longitude);
    }
    function onError(error) {
        alert('code: ' + error.code + '\n' +
              'message: ' + error.message + '\n');
    }
    navigator.geolocation.getCurrentPosition(onSuccess, onError);


 })

Added the google maps into the index.html

<script src="https://maps.googleapis.com/maps/api/js?sensor=true&v=3.exp"></script> 

Added a Map to my Map.html file

<div id="map" data-tap-disabled="true" map> </div>

The problem is I always get a fixed value of Lat/Long ie Lat = 43.465187 and Long = -80.522372

This is not my correct Geolocation in terms of Lat/Long.I need my current Geolocation in terms of Lat/Long

Please help me identity the fault.

Also I am using Ripple -Nexus (Galaxy) on Browser.

Any help is appreciated... Thanks,


Solution

  • Download the Latest version of GeoLocation pluggin from https://github.com/apache/cordova-plugin-geolocation

    Then inject the $cordovaGeolocation into the controller. Code is as below:

    .controller('HomeCtrl', function($scope,$cordovaGeolocation) {
    $scope.getLocation = function() {      
      var posOptions = {timeout: 10000, enableHighAccuracy: false};
        $cordovaGeolocation
          .getCurrentPosition(posOptions)
          .then(function (position) {
            //these are your your lat long values  
            var lat  = position.coords.latitude
            var long = position.coords.longitude                        
            }, function(err) {
            // error           
           });
          }
        })
    

    Note: The device need to be connected to internet or wifi and also need to keep the GPS location ON in the device for getting the correct Lat/Long Values.