0

I just started learning android.I am developing one small application in which i am using google map so, i used google maps api v2.

Now when i am running my application i am able to get the map and i am finding my location and adding one marker at that point but when i am trying to remove that marker i am unable to remove it.I am using clear() method. Here is my code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(isGooglePlayAvailable())
    {

        criteria=new Criteria();

        setContentView(R.layout.mapdemo);

        getGoogleMap();

        getUserLocation();

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

        getAddress(lat,lang);

        drawMarker(lat,lang);

    }

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);
        lat=location.getLatitude();
        lang=location.getLongitude();
    }
}

@Override
public void onLocationChanged(Location location) {

    drawMarker(lat, lang);

}

private void drawMarker(double lattitude,double longitude){

    google_map.clear();

    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);
    google_map.addMarker(new MarkerOptions().position(currentPosition).snippet("Address:" + addresses.get(0).getAddressLine(0) + "City:"+ addresses.get(0).getAddressLine(1)+"Country:"+addresses.get(0).getAddressLine(2)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)).title("ME"));
}

I also referred the following link Clear markers from Google Map in Android

Here i not posted full code because everything works fine what i wanted to do is i wated to remove current marker and add new marker that must be added using drawmarker() function.

Community
  • 1
  • 1
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68

0 Answers0