Juan
Juan

Reputation: 111

How to update gps location frequently?

I am writing an application that shows your location in a google map... so far, so good... It does shows my location correctly

The problem is that this gps is not updated if I am moving (or it does, but only after some time)

Does anybody know how to do this in a way like the native google maps (for android) does? This is, if you click on 'my location' it shows a flashing blue point that changes as you move...

This is my code:

//Initializing the listeners        
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, gpsLocationListener);



//and now, in both 'networkLocationListener' and 'gpsLocationListener'
//I overwrite the method 'onLocationChanged':

@Override
public void onLocationChanged(Location location) {
    Log.i("test", "New network location: "+location.getLatitude()+
                  " , "+location.getLongitude());
    myLongitude= location.getLongitude();
    myLatitude= location.getLatitude(); 
}

//and the variables myLongitude and myLatitude are the only ones that I need to display my
//position in the map...

Does anybody knows if I am missing something?

Upvotes: 4

Views: 7212

Answers (2)

skynet
skynet

Reputation: 9908

The method call you are using is requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), and you're telling it to only give you updates every 35000ms or every 35 seconds. Try lowering that number to whatever suits you. A lower number will mean more battery usage though.

If you want an indicator on a MapView, look into MyLocationOverlay.

Upvotes: 5

Luke
Luke

Reputation: 658

You wouldn't want to leave it like this forever, but it might be worth setting the minTime and minDistance values to zero if you haven't given that a shot yet.

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);

If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

-requestLocationUpdates Reference

Upvotes: 1

Related Questions