How to get Latitude and Longitude from IONIC

How can i get Latitude and Longitude from IONIC Cordova.

I am using following plugin for getting Lat/Lng.

cordova-plugin-geolocation 2.1.0 “Geolocation”

And

    var options = { timeout: 3000, enableHighAccuracy: true  }; 
 
    document.addEventListener('deviceready', function () { 
        navigator.geolocation.getCurrentPosition(onSuccess, onError, options); 
    }); 
                 
    function onSuccess(position) { 
       
        latvalue = position.coords.latitude; 
        lngvalue = position.coords.longitude; 
        timestamp = position.timestamp; 

        alert(latvalue);
       
    } 
     
    function onError(error)  
    {
      alert(error);
   }

You already have it in latvalue and lngvalue,

but you can also use Geolocation from ionic-native.

import { Geolocation } from 'ionic-native';


Geolocation.getCurrentPosition().then((resp) => {
 // resp.coords.latitude
 // resp.coords.longitude
})

Hi mvrc,

will you provide any script for this. It will help to me.
Thanks.

I am using following code, but some time i got Lat/Lng on some android device and sometime not.

var options = { timeout: 3000, enableHighAccuracy: true };

document.addEventListener(‘deviceready’, function () {
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
});

function onSuccess(position) {

latvalue = position.coords.latitude;
lngvalue = position.coords.longitude;
timestamp = position.timestamp;

alert(latvalue);

}

function onError(error)
{
alert(error);
}

Oh yes, i have the same problem.
You just have to store latvalue and lngvalue and only read this variables.
So when you have an error, latvalue and lngvalue remains the same.

However, some phones have gps with bad precision, you should try with this options :
var options = { timeout: 3000, enableHighAccuracy: false };