Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

java - Getting the current GPS location on Android

I'm trying to get the user's current location via GPS capability,

Wrote a simple class that implements LocationListener

public class LocationManagerHelper implements LocationListener {

    private static double latitude;
    private static double longitude;

    @Override
    public void onLocationChanged(Location loc) {
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
    }

    @Override
    public void onProviderDisabled(String provider) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public static double getLatitude() {
        return latitude;
    }

    public static double getLongitude() {
        return longitude;
    }

}

and from a simple Action I'm accessing these longitude and latitude values

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /** create a TextView and write Hello World! */
    TextView tv = new TextView(this);

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

    LocationListener mlocListener = new LocationManagerHelper();

    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            mlocListener);

    if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                    + '
');
            tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                    + '
');
    } else {
        tv.setText("GPS is not turned on...");
    }

    /** set the content view to the TextView */
    setContentView(tv);

But it always returns 0.0 as the result.Couldn't figure out the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Location updates are in fact asynchronous. This means the API does not make your calling thread wait until a new location is available ; instead you register an observer object with a specific method (callback) that gets called whenever a new location gets computed.

In the Android LocationManager API, the observer is a LocationListener object, and the main callback for location updates is onLocationChanged()

Here is a diagram trying to explain this point (hope this helps rather than confuse you!)

Sequence diagram

So from your current code :

  • Declare mlocListener as a member of your Activity subclass
  • Add log outputs (Logcat lines) in your LocationListener implementation
  • Keep the rest of the code as is.
  • Add the right permissions in the manifest (FINE_LOCATION is needed for GPS) if not done so yet.
  • Try to have the phone connected to Internet and near a window, in order to get a quite fast GPS fix (should be ~30s).

Then launch the app and watch what happens in the logcat. you will see that status changes and location updates are not immediate after the initial request, thus explaining why your textview always shows (0.0,0.0).

More: http://developer.android.com/guide/topics/location/obtaining-user-location.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...