简体   繁体   中英

Google maps removing previous marker location tracking

I am trying to display marker on user position and remove the previous one.I have tried something like this but its not working.

LocationListener locationListener = new LocationListener() {
     public void onLocationChanged(Location location) {
         Toast.makeText(getActivity(), 
         String.valueOf(location.getLatitude()),Toast.LENGTH_SHORT).show();

         Marker playermarker = null;
         if (playermarker!=null) {
              playermarker.remove();
              playermarker=null;
         }

         if (playermarker==null) {
              playermarker = googleMap.addMarker(new 
                    MarkerOptions().position(new 
                    LatLng(location.getLatitude(),location.getLongitude())));
         }
      }

      public void onStatusChanged(String provider, int status, Bundle extras) {}

      public void onProviderEnabled(String provider) {}

      public void onProviderDisabled(String provider) {}
};

Use this

  onLocationChanged{

       if (currLocationMarker != null) {
                currLocationMarker.remove();
            }

            latLng = new LatLng(location.getLatitude(), location.getLongitude());

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position.......");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            currLocationMarker = mMap.addMarker(markerOptions);
}

Custom Marker select image form drawable folder

.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM