Frage

I created a program that displays a mapView, with the location of the phone, and the ability to send this location (latitude, longitude) through email. (to add some places to a database).

I'm using the LocationManager class and getLatitude / getLongitude methods to get the phone's location. My code looks like this:

double MyLongitude  = 0;
double MyLatitude   = 0;
Location location   = null;

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, new ShowLocation()); 

location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

MyLatitude  = location.getLatitude();
MyLongitude = location.getLongitude();

When test my application on the emulator, everything is OK: mapView displays the location I fixed (using telnet and geo fix command), and I can send the location through email. I get the same result on the phone of a friend; everything seems to work OK.

The problem is on my phone. I can display my location on the mapView (the little blue circle), but when I try to send my location through email, the Latitude and Longitude were both set to 0.

Here is the code to display my location on the mapView:

MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
myLocation.enableMyLocation();

I don't understand why my location appears to work when displayed, but I can't get the correct latitude and longitude, while it seems to work on emulator and my friend's phone.

Is this due to missing code or something like that? Is this a problem with my phone? I checked the permissions, and I already have the needed ones:

android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_MOCK_LOCATION

I also tried to test with LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER, but got the same result.

War es hilfreich?

Lösung

You can override onLoationChanged(Location l) on your MyLocationOverlay, which will get called whenever the overlay gets a new location to draw your position, instead of requesting location updates and getLastKnownLocation.

I am guessing that getLastKnownLocation is returning 0, 0 because there hasn't been a location fix yet.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top