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

2 Answers 2

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
           }
        });
Sign up to request clarification or add additional context in comments.

6 Comments

I already have <uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name = "android.permission.ACCESS_MOCK_LOCATION"/> <uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION"/>
public boolean runOnFirstFix(java.lang.Runnable runnable) Please u give me an example of above.
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 }
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.
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.