0

I am developing small application in android to learn and as my project of final year.

In this i found user location and put a marker in a google map.

But now i am unable to remove it.

I already tried Clear() method and remove() method.

here is my code.

getUserLocation();

Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang,
                Toast.LENGTH_LONG).show();

getAddress(lat, lang);

drawMarker(lat, lang);

This is the code i written inside oncreate() method.

Now here is the function code.

private void getUserLocation() {

    location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (location_manager != null) {
        provider = location_manager.getBestProvider(criteria, true);
        location = location_manager.getLastKnownLocation(provider);
        location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (location != null) {
            lat = location.getLatitude();
            lang = location.getLongitude();
        } else {

            Toast.makeText(Map.this, "Unable to idntify location",
                    Toast.LENGTH_LONG).show();

            lat = -22.00000;
            lang = -33.0000;

        }
    }
}

@Override
public void onLocationChanged(Location location) {

    mlocationMarker.remove();
    google_map.clear();
    drawMarker(lat, lang);

}

private void drawMarker(double lattitude, double longitude) {

    google_map.setMyLocationEnabled(true);

    LatLng latlng = new LatLng(lattitude, longitude);

    google_map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    google_map.animateCamera(CameraUpdateFactory.zoomTo(15));

    LatLng currentPosition = new LatLng(lattitude, longitude);
    mlocation = new MarkerOptions();
    mlocation.position(currentPosition);
    mlocation.snippet("Address:" + addresses.get(0).getAddressLine(0)
            + "City:" + addresses.get(0).getAddressLine(1) + "Country:"
            + addresses.get(0).getAddressLine(2));
    mlocation.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mlocation.title("ME");
    mlocationMarker = google_map.addMarker(mlocation);
}

Here i am able to find out location and put marker but in onlocationChanged() its not working i don't know how to solve it.

Here is my request for updates line.

location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Kara
  • 6,115
  • 16
  • 50
  • 57
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
  • Please check the link same question with accepted answer... [1]: http://stackoverflow.com/questions/3696415/remove-all-overlays – Srinivasan Sep 24 '13 at 11:43

1 Answers1

0

It looks to me like your code should work. I would try adding some logging to verify that onLocationChanged, is actually being called.

On a side note, you could consider just updating the position of the marker, instead of removing it and adding a new one. See the docs for details.

rusmus
  • 1,665
  • 11
  • 18