简体   繁体   中英

Android GPS Always Returns Same Location

I'm trying to get the user's location within Android, the code below works however it always returns the same location no matter what I do. I've tested out in the middle of an empty parking lot to ensure the GPS is locked on, and it is. Google maps also shows my location correctly. Is there something wrong with the code below?

public class LocationTestActivity extends Activity implements LocationListener
{   
private Location myLoc;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    output = (TextView) findViewById(R.id.output);
    myLong = (TextView) findViewById(R.id.longi);
    myLat = (TextView) findViewById(R.id.lat);
    myRefreshed = (TextView) findViewById(R.id.counter);

    mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) 
{
    timesChanged++;
    myRefreshed.setText("Refreshed: " + timesChanged);
    myLong.setText("Longitude: " + location.getLongitude());
    myLat.setText("Latitude: " + location.getLatitude());
}
}

I'd like to add that I can pass in a location using the emulator with no problem. I also removed the other needed methods required by LocationListener for clarity. Thanks for the help!

First make sure that onLocationChanged() is actually getting called. If your GPS has locked before then there is a chance that onLocationChanged won't get fired at all because the phone thinks that you haven't really moved. Therefore it is not a good practice to rely only on requestLocationUpdates() to get your current location.

In general here is what you should do to get your position:

  1. You should use getLastKnownLocation() first to try locating your current location based on last known position.

  2. Check if the location retrieved from step #1 is within reasonable time (not too old) by calling getTime() in the location and comparing with the current time

  3. If this last known position is considered old (I normally use 5-10 minutes depending on the context of the app) then you start requestLocationUpdates() with specific distance (the app should make assumption that the user has moved within the specified limit of last known position)

  4. Implement the onLocationChanged() as desired

Another note, I notice you are only using GPS, there is a lot of situation where GPS cannot lock and therefore never call onLocationChange(), your code should take account of that and checking Network based triangulation in the case onLocationChange() is not called within specified time

Try getting last location fix

Location loc =  mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

and give some time/distance between each location service request

 mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);

And I hope you have enough patience to wait until your device retrieves the location and onLocationChanged() is triggered;) Joking.

please check your manifest file whether have you added those permission or not.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

One more thing.Where did you call "removeUpdates()"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM