Pergunta

I need my Actual position in GPS just once. In this code, onLocationChanged is never called, I don't understand why. As I call it:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

It should run every 0 sec and every 0 meters. How can I change the code so that it works?

public class MapActivity extends FragmentActivity implements
        LocationListener {

    private final String TAG = getClass().getSimpleName();
    private GoogleMap mMap;
    private Location loc;
    private Context ctx;
    private radius;
    LocationManager mLocationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        ctx = MapActivity.this;
        radius = getRadius();

        mMap = initMap();

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {
            loc = location;
            Log.v("Location Changed", location.getLatitude() + " and "
                    + location.getLongitude());
            mLocationManager.removeUpdates(this);
myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
        myPlaces.execute();
            Log.e(TAG, "location : " + loc);
        }
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

EDIT : I moved the call to loc :

myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
        myPlaces.execute();
            Log.e(TAG, "location : " + loc);

inside the onLocationChange. This way, I don't get nullpointerException anymore, but I can wait more than 20 sec to get location. It is not so good... If anybody have an idea of how to get the fix quickly!

Foi útil?

Solução

What I did is add 2 listener, not only gps, but also network before onCreate :

Found the code here : Location Manager's requestLocationUpdates called only once

private final LocationListener gpsLocationListener =new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener =
                                                    new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras){
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

and activate them in CallBack methods :

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            0, 0, gpsLocationListener);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}

And then it worked well! I think just GPS is not enough, because fix can last a lot to come !

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top