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
273 views
in Technique[技术] by (71.8m points)

java - Location returned is null when provider is gps?

I am trying to retrieve the my current location coordinates in the following piece of code

LocationManager locationManager= (LocationManager) getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

I get the location coordinates when the provider is "network".

When I change the settings in the location and security options to enable "Use GPS satellites" option the provider changes to "gps". In this case the location returned is null.

What could be the cause of this and how can it be rectified?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It takes a while for the GPS provider to get a fix, especially if no previous fix was made. The getLastKnownLocation is not a blocking call that will wait untill an actual live GPS fix is established (that fix can take up to 1 min, depending on various reasons).

Also, make sure you're outside for obvious reasons).

Instead of simply calling the getLastKnownLocation for the GPS provider, you first need to request location updates from it.

The following article explains the principle very well: http://developer.android.com/guide/topics/location/obtaining-user-location.html

It's a must read for anyone doing anything with the location provider on Android.

the typical flow is:

  1. Start application.
  2. Start listening for updates from desired location providers.
  3. Maintain a "current best estimate" of location by filtering out new, but less accurate fixes.
  4. Stop listening for location updates.
  5. Take advantage of the last best location estimate.

Looking at the code below, we’re initializing a MyLocationListener (to track the phone’s position), and retrieving a reference to the LocationManager. We’re requesting location updates from the locationmanager. We’re using a minDistance (minimum distance interval for notifications) of 10 meters, and a minTime (the minimum time interval for notifications) of 35 seconds.

LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

When implementing this, you'll probably notice that the MyLocationListener receives more updates than you expected (given the minTime and minDistance parameters). The following article at http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ can help you understand why.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

56.9k users

...