1

I am trying to update user location using:

 private void addLocationIndicator(GeoCoordinates geoCoordinates,
                                      LocationIndicator.IndicatorStyle indicatorStyle, double orient) {
        LocationIndicator locationIndicator = new LocationIndicator();
        locationIndicator.setLocationIndicatorStyle(indicatorStyle);

        // A LocationIndicator is intended to mark the user's current location,
        // including a bearing direction.
        // For testing purposes, we create a Location object. Usually, you may want to get this from
        // a GPS sensor instead.
        Location location = new Location.Builder()
                .setCoordinates(geoCoordinates)
                .setTimestamp(new Date())
                .setBearingInDegrees(orient)
                .build();

        locationIndicator.updateLocation(location);
        // A LocationIndicator listens to the lifecycle of the map view,
        // therefore, for example, it will get destroyed when the map view gets destroyed.
        mapView.addLifecycleListener(locationIndicator);
    }

I have to remove the previous indicator whenever the location is updated. Is there any method to remove previous location indicators, as it is being stacked up upon the last indicators as the user updates its location?

Ujjwal
  • 11
  • 3

1 Answers1

0

This is happening because you are creating a new object of LocationIndicator every-time addLocationIndicator() is called. You should move the LocationIndicator locationIndicator declaration to class level instead of method level and only create the object once next time when method is called check if locationIndicator is not null then do not create a new object.

private LocationIndicator locationIndicator = null;
private void addLocationIndicator(GeoCoordinates geoCoordinates,
                                          LocationIndicator.IndicatorStyle indicatorStyle, double orient) {

    if(locationIndicator == null){ 
      locationIndicator = new LocationIndicator();
    }
    locationIndicator.setLocationIndicatorStyle(indicatorStyle);
            Location location = new Location.Builder()
                    .setCoordinates(geoCoordinates)
                    .setTimestamp(new Date())
                    .setBearingInDegrees(orient)
                    .build();
    
            locationIndicator.updateLocation(location);
            mapView.addLifecycleListener(locationIndicator);
        }
Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
  • For my use case, I need to create a location indicator object again and again. But once the new object is created, I no longer need the old one. I need to basically remove the "old object" of the location indicator, once the new object is created. – Ujjwal Jan 12 '22 at 13:37