Skip to main content

Symbol listener

Not compatible with the Maps SDK v10
This plugin is not compatible with the Maps SDK v10 or higher. The Maps SDK v10 and higher comes with annotation functionality built in. See the Markers and annotations guide.

This example uses the Annotation plugin to add a symbol to a map and add listeners to the symbol.

You must install the plugin before running the application.

Note

This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res directory. For example, see res/values/activity_strings.xml for R.string.* references used in this example.

activity_annotation_plugin_symbol_listener
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="60.169091"
mapbox:mapbox_cameraTargetLng="24.939876"
mapbox:mapbox_cameraZoom="12" />

</FrameLayout>
SymbolListenerActivity.java
package com.mapbox.mapboxandroiddemo.examples.plugins;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolDragListener;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolLongClickListener;
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager;
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;
import com.mapbox.mapboxsdk.plugins.annotation.Symbol;
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolClickListener;

/**
* Change symbol icon by pressing on icon
*/
public class SymbolListenerActivity extends AppCompatActivity implements
OnMapReadyCallback {

private MapView mapView;
private static final String MAKI_ICON_CAFE = "cafe-15";
private static final String MAKI_ICON_HARBOR = "harbor-15";
private static final String MAKI_ICON_AIRPORT = "airport-15";
private SymbolManager symbolManager;
private Symbol symbol;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_annotation_plugin_symbol_listener);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {

// Set up a SymbolManager instance
symbolManager = new SymbolManager(mapView, mapboxMap, style);

symbolManager.setIconAllowOverlap(true);
symbolManager.setTextAllowOverlap(true);

// Add symbol at specified lat/lon
symbol = symbolManager.create(new SymbolOptions()
.withLatLng(new LatLng(60.169091, 24.939876))
.withIconImage(MAKI_ICON_HARBOR)
.withIconSize(2.0f)
.withDraggable(true));

// Add click listener and change the symbol to a cafe icon on click
symbolManager.addClickListener(new OnSymbolClickListener() {
@Override
public boolean onAnnotationClick(Symbol symbol) {
Toast.makeText(SymbolListenerActivity.this,
getString(R.string.clicked_symbol_toast), Toast.LENGTH_SHORT).show();
symbol.setIconImage(MAKI_ICON_CAFE);
symbolManager.update(symbol);
return true;
}
});

// Add long click listener and change the symbol to an airport icon on long click
symbolManager.addLongClickListener((new OnSymbolLongClickListener() {
@Override
public boolean onAnnotationLongClick(Symbol symbol) {
Toast.makeText(SymbolListenerActivity.this,
getString(R.string.long_clicked_symbol_toast), Toast.LENGTH_SHORT).show();
symbol.setIconImage(MAKI_ICON_AIRPORT);
symbolManager.update(symbol);
return true;
}
}));

symbolManager.addDragListener(new OnSymbolDragListener() {
@Override
// Left empty on purpose
public void onAnnotationDragStarted(Symbol annotation) {
}

@Override
// Left empty on purpose
public void onAnnotationDrag(Symbol symbol) {
}

@Override
// Left empty on purpose
public void onAnnotationDragFinished(Symbol annotation) {
}
});
Toast.makeText(SymbolListenerActivity.this,
getString(R.string.symbol_listener_instruction_toast), Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Was this example helpful?