Geolocation

This topic is not a problem. It is a solution for using geolocation.

I used the Cordova plug-in for geolocation in my app. Sinecure a couple of days getCurrentLocation does’t work anymore. I read from Cordova that their plugin is deprecated. I removed the plugin and changed my code to use Web Geolocation. Now my works fine as before.

Where did you see this? I’ve been using it fine. Instead of getCurrentLocation, I’m using watchPosition and seems to be working fine.

I don’t see anywhere on their documentation that it has been depreciated. I’d love to know what the case is since I’m working on an app that rely’s on geolocation.

Hi everyone,

I try to develop an Ionic app with android platform using ngCordova geolocation. I just want to get the user location (latitude and longitude). So a simple geolocation Ionic app based on the Ionic seed tabs app ! But, it doesn’t work.

Of course, I’ve installed

  • the JAVA SDK,
  • ant,
  • Android SDK for Eclipse,
  • Ionic,
  • cordova

and then initial Ionic tabs app worked fine. So, I’ve install ngCordova with bower and I had some line of Javascript in order to get the location of the user

  • In my index.html file, I’ve had a reference to lib/ngCordova/dist/ng-cordova.js before cordova.js,
  • In my controller, in the $ionicPlatform.ready function, I call a own service which call $cordovaGeolocation.getCurrentPosition in order to get the position

But, when I emulate the app with ionic emulate android I’ve got a black screen in then android emulator.

How can I debug this please. I’ve no information from android emulator, event if i use $log.debug or console. What is the best way to debug an ionic app for android platform ? You know what I mean a way with a debugger, with breakpoints, … Is there a solution to do this ?

Thank you in advance for your help !

I’ve found the solution… I can test my app with the command line “ionic serve” + the “c” character on the keyboard

All is going well in this beautiful world !

Hi, can paste your location code here or codepen/plunker?
Im trying to save it to the factory but unable to do so.

I simple use: window.navigator.geolocation.getCurrentPosition(function (location) in a function.
So ngCordova uses the same.
In my opinion this has nothing to do with end up your app in a black screen.
You can sent your code to me. I’ll try to make it working.
By the way: the best way to debug an Android app is using a real device connected to your desktop. If you use a device with Android 4.4.2 you even can use webdevtools in chrome to debug like you do in chrome.
After 3 years developing apps I learned its hard (almost impossible) to debug without a real device.

My app is woorking well now, thank you.

An alternative way to debug is to use hte “ionic serve” command with Firefox and to install the Geolocater plugin. With Geolocater, you can define several geographical points, select one of them and retrieve the selected geographic point in your app.

@pcr Hey, just looking at the post, you seem pretty experienced with the $cordovaGeolocation plugin. If you get a chance would you be able to take a quick look at this? Cordova, $geolocation and geocoding in Ionic

Cheers in advance!

Prettig experienced? Well I loodjes in my app how I implement watchGeo.
I still use a solution from before WWII:

watchGeoId = navigator.geolocation.watchPosition(handlerSuccess, handlerError, {
frequency: 300,
enableHighAccurancy: true
});

In the handlerSuccess callback you put the code to handle position.

I see ngCordova now uses promises on geoLocation. In the next few days I’ll dive in the ngCordiva approach.

1 Like

That would be awesome because using navigator.geolocation.watchPosition like so:

var handlerSuccess = function(position) {
   var lat         = position.coords.latitude,
   long            = position.coords.longitude,
   currentLocation = new google.maps.LatLng(lat, long);
   currentPosMarker.setPosition(currentLocation);

   console.log('new position:' + position);
};

var handlerError = function(error) {
  console.log('Error w/ watchPosition: ' + JSON.stringify(error));
};

var watchGeoId = navigator.geolocation.watchPosition(handlerSuccess, handlerError, {
   frequency: 3000,
   enableHighAccurancy: true
});

Is working fine so I’d love to know how to get it to work with ngCordova.

1 Like

Did you get it to work in ngCordova?

I got it to work in ngCordova, try tweaking the frequency and timeout options, In my case i increased the figures…

Now I remember why I use still navigator.geolocation.watchposition instead of ngCordova. Because the navigator option also workshop in a browser. NgCordova doesn’t work in the browser. I hope the ngCordova team soon will bring good working mocks with good instructies how to test/develop ngCordova functions in a browser, because developers used to develop with a desktop browser.

Hi, geolocation is working here (ios with ionic and ngcordova) but what about user permissions? If the user says no to the first alert (“would you like to allow access the geolocation…” ) , how can I request again the permissions?

1 Like

If you’re working on a mobile application using Cordova 4.0 + and you’re using Google Maps do not forget to install Cordova Whitelist plugin. As of Cordova 4.0 all external resources permissions can be done only through this plugin, which includesGoogle Maps.

I wrote an article on this topic, you will also find a working example there: Using Cordova Geoloacation API with Google Maps in Ionic Framework

@folador Best practice: Before popping the system modal requesting permission, present another modal with messaging about why you’re asking for permission to access location (e.g. “We can only show you the best deals if we have your current location”). That modal should also have options “Cancel” or “I agree”. Then, if the user taps “I agree”, then show them the system modal asking for location and they can once again tap “Yes”.

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);

}

Why not using ngCordova? NgCordova is available from the Ionic homepage. Look at the very end of the page voor links.

Verzonden vanaf Samsung-tablet

-------- Oorspronkelijk bericht --------
Van: Rranjan ionicframework@discoursemail.com
Datum: 30-09-2016 13:36 (GMT+01:00)
Aan: piet.crombach@home.nl
Onderwerp: [Ionic] [tutorials] Geolocation

rranjan 

September 30
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);
}

Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

is it possible to include in a form/field the lon/lat next to the address on an ordering form ?
would make it easier for delivery as then the driver app gets the lon/lat instead of the whole address from the backend using FCM push

can someone help