Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android can't retrieve my current position

Hi everybody,

I'm using Titanium SDK 1.5.1 for both iPhone and Android development.

I have an app that loads a map with my current position perfectly on iPhone. But, for android, nothing is showing but an error from the Geolocation API.

I added my Android Map API key and other properties in the tiapp.xml file:

<property name="ti.android.debug" type="bool">false</property>
    <property name="ti.android.google.map.api.key.development">xxxxxxxxxxxxxxxxxxx</property>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.INTERNET"/>
            <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
            <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    </android>

Then, in my js file, I added the two lines:

Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.distanceFilter = 10;

And, for the call, here is the code:

Titanium.Geolocation.getCurrentPosition(function(e){

    if (e.error)
    {
        alert('Error while retrieving your position!');
        return;
    }
var longitude = e.coords.longitude;
var latitude = e.coords.latitude;
....

On my device, the app returns the alert above even if I allow Android to access to my position (in Android Settings).

So what is the problem?

Thanks by advance!

— asked January 17th 2011 by Thierry Marc
  • android map geolocation
1 Comment
  • Same problem, even on a phisycal device, Titanium 1.5.1 a GApis 2.2 and 2.3

    — commented February 11th 2011 by Martino Massalini

9 Answers

  • Hi, I join this conversation for the same problem.

    I'm on android APIs 2.3 and I want to use geolocation on emulator. Maps work, I can display mapViews and annotations, but I cannot retrieve my currrent position, Titanium.Geolocation.getCurrentPosition() gives me back error "location is currently unavailable. code 2"

    I tried methods showed here and here, but nothing works

    any help?

    — answered January 26th 2011 by Emanuele Gaspari Castelletti
    permalink
    0 Comments
  • Well I figured out to run the geolocation functionality by turning the app on my device (a 2.1 update android version. The emulator is really bad.) AND by using a nighty build here:
    http://developer.appcelerator.com/guides/en/continuous_builds.html

    @Emanuele Gaspari Castelletti: I can't mark this answer as solved but don't hesitate to post "questions/pastebin codes" if you still have issues.

    Regards.

    — answered January 26th 2011 by Thierry Marc
    permalink
    1 Comment
    • I am having issues with Android 2.1 API Emulator and a Handset with the exact same issues here. I get an initial error when first calling Titanium.Geolocation.getCurrentPosition() I've tried the geo fix approach and although I could see the GPS icon show in the toolbar, it did not lookup the GPS value. Am trawling the community/web for answers or solutions. I am running 1.2.2 Titanium Developer on Mac OSX 10.6.6 - the gps lookup works on iPhone btw

      — commented March 18th 2011 by Ketan Majmudar
  • Thank you for replying.

    I tried using the last continuos build: Jan 25 2011 19:43 ra056f7f7
    but didn't work.
    From the comments it seems they worked hard precisely on the Geolocation module.

    I post my code, but it's quite straightforward:

    window = Titanium.UI.currentWindow;
    
    
    function setCurrentLocation(annotation) {
      Titanium.Geolocation.getCurrentPosition(function(e){
    
        if(e.error){
          alert(e.error);    // returns "location is currently unavailable. code 2"
          return;
        }
    
        annotation.latitude  = e.coords.latitude;
        annotation.longitude = e.coords.longitude;
    
      });
      return annotation;
    }
    
    if(Titanium.Geolocation.locationServicesEnabled==true){
    
      Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
      //Titanium.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS;
    
      Titanium.Geolocation.distanceFilter = 10;
    
      var current_location_annotation = Titanium.Map.createAnnotation({
        title:      'current location',
        pincolor:    Titanium.Map.ANNOTATION_RED,
        animate:    true,
      });
      setCurrentLocation(current_location_annotation);
    
      var mapview = Titanium.Map.createView({
        mapType: Titanium.Map.STANDARD_TYPE,
        animate:true,
        region: { latitude:45.319844,
                  longitude:10.832831,
                  latitudeDelta:0.1,
                  longitudeDelta:0.1},
        regionFit:true,
        userLocation:true,
        visible: true,
        annotations:[current_location_annotation]
      });    window.add(mapview);
    
    } else {
    
      noGeoLocationAlert=Titanium.UI.createAlertDialog({
      title:'Error',
        message:'geolocation not available.'
      });
      noGeoLocationAlert.show();
    
    }
    

    Moreover, neighter the app called "Navigation" does work on android emulator. It stucks in "waiting for location" by gps and the emulator doesn't respond. I don't think is a Titanium problem, because "Navigation" should work in this case. I cannot try on a phisycal device by usb, because I don't have one, sorry.

    — answered January 26th 2011 by Emanuele Gaspari Castelletti
    permalink
    0 Comments
  • If I understand your question correctly it's working on your device but not the simulator? Have you set a location for the GPS (via the geo fix command).

    — answered January 26th 2011 by Frank A
    permalink
    0 Comments
  • No, I don't have a phisycal device to test on. I'm working only on the emulator.

    yes, I tried:

    geo fix <latitude> <longitude>
    

    and also setting the location with the ddms, but without results

    — answered January 26th 2011 by Emanuele Gaspari Castelletti
    permalink
    0 Comments
  • I've got the same problem on the simulator and the device.

    I'm using Google Maps APIs 2.2, Titanium 1.2.2 and a beta version of mobile sdk.

    — answered February 3rd 2011 by Morvan Loïc
    permalink
    0 Comments
  • Same problem, even on a phisycal device, Titanium 1.5.1 a GApis 2.2 and 2.3

    — answered February 11th 2011 by Martino Massalini
    permalink
    0 Comments
  • same problem Titanium 2.0 GApis 2.2 and 2.3

    — answered July 21st 2012 by eugene brown
    permalink
    0 Comments
  • Had same problem on my no contract/pay-as-you-go Android device. Was working fine then suddenly stopped.

    Error code = 6, "Location services currently unavailable"

    Checked, Setting > Location & security > Use wireless networks - checked , Use GPS satellites - checked
    no changes there.

    Having read that Android uses a cached position when Titanium.Geolocation.getCurrentPosition is called, I opened Maps from the home screen hoping to cache a position. Received an Alert, clicked Settings and discovered my WI-FI settings had changed.

    Not sure why it had changed but after trying all other solutions/links this resolved my issue.

    — answered December 12th 2012 by K. E.
    permalink
    1 Comment
    • Keylo -

      Have you figured this out. I'm unable to get my location as well

      — commented February 5th 2013 by matt s
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.