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!
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?
-
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.
-
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.
-
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).
-
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
-
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.
-
Same problem, even on a phisycal device, Titanium 1.5.1 a GApis 2.2 and 2.3
-
same problem Titanium 2.0 GApis 2.2 and 2.3
-
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.