2
protected void showCurrentLocation(){
 Location lc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 if(lc != null){
  String msg = String.format("Current Location \n Logitude: %1$s \n Latitude: %2$s",lc.getLongitude(),lc.getLatitude());
  Toast.makeText(Test1.this,msg,3000).show();
 }
 Toast.makeText(Test1.this,"location is null",3000).show();
} 

from code above, when I run on an Android phone, it can run but it only shows location as null. I don't know why it can't get location from getLastKnownLocation()

http://www.javacodegeeks.com/2010/09/android-location-based-services.html

This is source code which I got to try. Please help me. Thanks ka :))

Ps. I already have ACCESS_FINE_LOCATION,ACCESS_MOCK_LOCATION, and ACCESS_COARSE_LOCATION

Yoo
  • 1,421
  • 8
  • 20
  • 36

2 Answers2

1

Do your application have permissions for using GPS?

http://developer.android.com/reference/android/Manifest.permission.html

P.S: Here (http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null) is suggested the following - to use LocationListener

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
    //TODO:enter your code here
    }
}

Also you can use LocationOverlay:

 final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
        overlay.enableMyLocation();
        overlay.runOnFirstFix(new Runnable() {
           public void run() {
             //TODO: update some model, etc
           }
        });
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • I already have – Yoo Dec 27 '10 at 14:32
  • See this answer - http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null – Kiril Kirilov Dec 27 '10 at 14:35
  • public boolean runOnFirstFix(java.lang.Runnable runnable) Please u give me an example of above. – Yoo Dec 27 '10 at 14:44
  • Appreciate for your answer but i'm not understand private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { //TODO:enter your code here } – Yoo Dec 27 '10 at 14:59
  • 1
    Ok. You create new Listener object. The purpose of this object is to LISTEN. For some kind of events(location changed events in that case). When this event happens (location is changed), the onLocationChanged method is invoked. In the '//TODO' part you must insert your source, this part will be invoked every time the location is changed. – Kiril Kirilov Dec 27 '10 at 15:04
  • Thank you. Actually I have class which implements LocationListener and have a onLocationChanged(Location location) already. Could you pls go in a website which I posted it have all of I have thank you – Yoo Dec 27 '10 at 15:12
0

Daisy, I think what you are struggling with is the concept of getLastKnownLocation(). In the emulator, you manually send the mock location and there is indeed a last known location when you run your code. On your actual phone, there may not be a last known location, yet...

You're probably thinking "But doesnt my phone always know its location" and the answer is maybe but not always.

So as mentioned above, you really need to write your code to listen for location updates from a location manager