title: How to remove old location marker for new location in google map, Android Studio?
body: <p>This code is work but i had a problem with marker, when i run my app it does shows marker but when i walk to change my location, the old location marker still appear on app and my latest location appear too, how can i remove the old location marker?</p>
  
  <p>Here my Activity.java</p>
  
  <pre><code>package stmikbumigoramataram.intro;
  
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.location.Address;
  import android.location.Geocoder;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.app.FragmentActivity;
  import android.os.Bundle;
  import android.util.Log;
  
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.CameraPosition;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.Marker;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import java.io.IOException;
  import java.util.List;
  
  public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  
  private GoogleMap mMap;
  private LocationManager locationManager;
  private LocationListener locationListener;
  Marker currLocationMarker;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
              .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
  
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
  
      locationListener = new LocationListener() {
          @Override
          public void onLocationChanged(Location location) {
              double latitude = location.getLatitude();
              double longitude = location.getLongitude();
  
             if(currLocationMarker != null){
                 currLocationMarker.remove();
             }
  
              LatLng latLng = new LatLng(latitude, longitude);
              Geocoder geocoder = new Geocoder(getApplicationContext());
              try {
                  List&lt;Address&gt; addressList = geocoder.getFromLocation(latitude, longitude, 1);
                  String str = addressList.get(0).getLocality()+", ";
                  str += addressList.get(0).getCountryName();
                  currLocationMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(str));
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
              CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latLng.latitude, latLng.longitude)).zoom(15).build();
              mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
          }
  
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
  
          }
  
          @Override
          public void onProviderEnabled(String provider) {
  
          }
  
          @Override
          public void onProviderDisabled(String provider) {
  
          }
      };
  
  
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
  
  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
  
  }
  
  protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(locationListener);
      Log.i("onPause...","paused");
  }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ot14K.png" rel="nofollow noreferrer">You can see result of my code here</a></p>