1

我正在尝试使用以下方法更新用户位置:

 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);
    }

每当更新位置时,我都必须删除以前的指示符。是否有任何方法可以删除以前的位置指示符,因为当用户更新其位置时,它会叠加在最后一个指示符上?

4

1 回答 1

0

发生这种情况是因为您正在创建一个LocationIndicator每次addLocationIndicator()调用的新对象。您应该将LocationIndicator locationIndicator声明移至类级别而不是方法级别,并且仅在下次调用方法时创建一次对象,检查是否locationIndicator不为空,然后不要创建新对象。

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);
        }
于 2022-01-12T11:14:28.613 回答