Reeves62
Reeves62

Reputation: 149

How to remove a symbol from a map

I am currently creating a map which updates based on users selection and displays 5 location closest to them. This works however when the user changes their selection the map updates and displays the 5 NEW locations as well as the 5 OLD locations.

I am not sure how to remove the old symbols.

public void displayResults(ArrayList allLocation) {

    SymbolManager sm = new SymbolManager(mapView,map,styleMap);

    sm.deleteAll();
    SymList.clear();


        sm.setIconAllowOverlap(true);
        sm.setIconIgnorePlacement(true);

    int count = 1;

    for (LocationDetails a : allLocation
    ) {
        // gets the distance from user to Location
        double LocationLat = Double.parseDouble(a.getLatitude());
        double LocationLng = Double.parseDouble(a.getLongitude());
        float[] disResult = new float[1];
        Location.distanceBetween(lat, lng, LocationLat, LocationLng, disResult);
        results.append(count + ": " + a.getName() + " " + "\n");
        distanceResults.append(Math.round(disResult[0]) + "m" + "\n");

        SymbolOptions symbolOptions = new SymbolOptions()
                .withLatLng(new LatLng(LocationLat, LocationLng))
                .withIconImage("marker-11")
                .withTextField(""+count)
                .withIconColor("black")
                .withIconSize(2.5f);

        SymList.add(symbolOptions);

        count++;
    }

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
            .include(SymList.get(0).getLatLng())
            .include(SymList.get(1).getLatLng())
            .include(SymList.get(2).getLatLng())
            .include(SymList.get(3).getLatLng())
            .include(SymList.get(4).getLatLng())
            .build();

    map.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 2000);

    for(SymbolOptions a : SymList){
        sm.create(a);
    }

    SymList.clear();
}

Upvotes: 0

Views: 1280

Answers (2)

Adriana Babakanian
Adriana Babakanian

Reputation: 1299

If your use case only requires showing about five markers on the map at a time, it might be easier to use native sources and SymbolLayers rather than relying on the abstraction provided by the SymbolManager.

For example, this icon updates based on API response Android demo shows how to add a GeoJSON source and corresponding layer to the map, then update said source to get a different visual result. Basically all of the logic you will need is encapsulated here, but your GeoJSON will be a FeatureCollection of multiple (namely, 5) features rather than just one point.

So, you can set up your symbols similarly to how it's done in the linked example:

private void initSpaceStationSymbolLayer(@NonNull Style style) {
  style.addImage("space-station-icon-id",
  BitmapFactory.decodeResource(
  this.getResources(), R.drawable.iss));

  style.addSource(new GeoJsonSource("source-id"));

  style.addLayer(new SymbolLayer("layer-id", "source-id").withProperties(
    iconImage("space-station-icon-id"),
    iconIgnorePlacement(true),
    iconAllowOverlap(true),
    iconSize(.7f)
  ));
}

, and then update the source's GeoJSON to the new locations closest to the user's position, similar to the updateMarkerPostion method:

private void updateMarkerPosition(LatLng position) {
  // This method is where we update the marker position once we have new coordinates. First we
  // check if this is the first time we are executing this handler, the best way to  do this is
  // check if marker is null;
  if (map.getStyle() != null) {
    GeoJsonSource spaceStationSource = map.getStyle().getSourceAs("source-id");
    if (spaceStationSource != null) {
      spaceStationSource.setGeoJson(FeatureCollection.fromFeature(
      Feature.fromGeometry(Point.fromLngLat(position.getLongitude(), position.getLatitude()))));
    }
  }

  // Lastly, animate the camera to the new position so the user
  // wont have to search for the marker and then return.
  map.animateCamera(CameraUpdateFactory.newLatLng(position));
}

A few modifications will need to be made, of course, but this option might be more direct for your implementation specifically.

Upvotes: 1

Belluzzo Matteo
Belluzzo Matteo

Reputation: 36

I have been using mapbox for 3 months. After hours of research I discovered that on Android the only way to remove a Symbol or any element on the map was to reload all the elements from scratch. Unfortunately, there is currently no method to remove a single element. So I suggest you create a container class in which to save your items.

Upvotes: 1

Related Questions