Garbit
Garbit

Reputation: 6046

Android - RemoveUpdates not removing listener

I've been trying to remove location updates from my location manager however the GPS symbol continuously shows even after removeupdates has been called. I've ensured the correct listener variable name has been passed and the code is executed for the listener update. I only want to find the user's location once.

Thanks

public class MapActivity extends FragmentActivity implements OnInfoWindowClickListener, OnCameraChangeListener {

    Map<Marker, Integer> markers = new HashMap<Marker, Integer>();
    VenueManager venueManager;
    GoogleMap map;
    long last_map_refresh = 0;
    Button search_btn;
    CameraPosition previous_position = null;
    private LocationManager locationManager;
    private static final long MIN_TIME = 400;
    private static final float MIN_DISTANCE = 1000;
    private ProgressDialog loading;
    private Button add_venue;
    SharedPreferences sharedPreferences;
    private LatLng user_position = null;
    private NetworkManager network_manager;
    private Boolean first_map_render = false;
    private LocationListener location_listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

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

        if( !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MapActivity.this);
            builder.setTitle("GPS not enabled");  // GPS not found
            builder.setMessage("This application requires you to enable GPS location settings. We recommend that you enable 'Use wireless networks' and 'Use GPS satellites'. Would you like to enable this setting now?"); // Want to enable?
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                    MapActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent home_intent = new Intent(MapActivity.this, HomeActivity.class);
                    startActivity(home_intent);
                    Toast.makeText(MapActivity.this, "You will not be able to use the map features of this application until you enable gps settings", Toast.LENGTH_LONG).show();
                    finish();
                }
            });

            builder.create().show();
            return;
        }
        else
        {
            network_manager = new NetworkManager();

            location_listener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16);
                    map.animateCamera(cameraUpdate);
                    draw_map(new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).build());
                    user_position = latLng;

                    locationManager.removeUpdates(location_listener);
             };

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, location_listener);
}
}

Upvotes: 0

Views: 515

Answers (1)

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

First of all when using LocationManager.NETWORK_PROVIDER you should not see the GPS symbol at all.

I see you are using Google Maps Android API v2, so my guess is outside of the code you put here you have

map.setMyLocationEnabled(true);

which shows blue dot and continuously updates it while showing Activity with MapFragment/MapView.

Upvotes: 2

Related Questions