Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Emulator having issues with Geolocation

I have been using Android and the 2.2 emulator for a bit now and just started running into an issue. My geolocation is not being set in the Emulator. At first I thought it was an issue with PhoneGap 0.92 but after reverting to .91 and opening an older project the same holds true. In DDMS my coordinates are set and I 'send' them. I also telnet and issue the geo fix command and I get a OK response. It seems all is well but for some reason the emulator still has no clue.

I found some older posts re: setting the time zone. I did that. Still no luck. I even use the browser and go to html5demos.com/geo to see if it can find me and it does not.

This used to work, not sure what happened since then. I updated my version of Java 1.6.0.22. Android SDK is 2.2, API 8, revision 2.

Any tips or tricks would be appreciated. I am stuck at this point.

My Phonegap/JS call:

var getLocation = function() {
  var suc = function(p){
        alert(p.coords.latitude + " " + p.coords.longitude);
  };
 var fail = function(error){  
    alert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
  };
  navigator.geolocation.getCurrentPosition(suc,fail);
}

The 'fail' keeps happening. Error code 2. The last location provider was disabled.

--UPDATE Still having the same issue. I found an older mapping application I used back in the summer, it was 2.1. So I recompiled and ran that on a 2.1 emulator and that is also not working. The funny thing is in eclipse when i am in DDMS and send coordinates to 2.1 I 'see' a confirmation that it has been sent in the emulator but with 2.2 I do not.

The big difference is I am behind a proxy now (new job) where before I was not. I guess I could try at home...not sure if that matters.

like image 506
jdruid Avatar asked Nov 12 '10 21:11

jdruid


3 Answers

You have to put enableHighAccuracy into getCurrentPosition, for example:

navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true });

Next, you have to telnet into the emulator, and geo fix the location(longitude, latitude) you want:

kelvin@locahost:~$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix 101.689453 3.074695
OK
quit
Connection closed by foreign host.

Then run your PhoneGap app.

like image 52
kelvinfix Avatar answered Oct 18 '22 22:10

kelvinfix


The only way I have found to make this work is to telnet to the emulator and issue a "geo fix longitude latitude" after I have triggered the code which I am testing. Something like this emulates GPS coordinates for Trondheim, Norway (the port of the emulator is usually 5554, but this can vary with your settings):

kristian@bergman:~$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix 10.40 63.40
OK
quit
Connection closed by foreign host.

More information about the emulation can be found here

like image 30
Kristian Avatar answered Oct 18 '22 21:10

Kristian


I had the same problem. The solution is at the end of http://docs.phonegap.com/phonegap_geolocation_geolocation.md.html#Geolocation (scroll down):

"Android Quirks

The Android 2.x simulators will not return a geolocation result unless the enableHighAccuracy option is set to true.

{ enableHighAccuracy: true }"

So you have to set enableHighAccuracy at your navigator.geolocation.getCurrentPosition and your navigator.geolocation.watchPosition calls.

like image 36
mrbigbunbury Avatar answered Oct 18 '22 20:10

mrbigbunbury