-
Notifications
You must be signed in to change notification settings - Fork 75
Listen and handle map events
As you can see in the list Map Listener methods there are various events what MapView can provide to the application: general view manipulation, clicking on objects and marker labels, and even drawing events (which will be useful for custom drawing like GPS animation).
- Create Map Listener Class. Here we define minimal MapEventListener.java. It has two additions: it shows Toast messages for clicks to a marker, and it takes Activity in constructor to be able to show the Toasts.
package com.nutiteq.hellomap;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.widget.Toast;
import com.nutiteq.geometry.VectorElement;
import com.nutiteq.projections.EPSG3857;
import com.nutiteq.ui.DefaultLabel;
import com.nutiteq.ui.MapListener;
public class MapEventListener extends MapListener {
private Activity activity;
// activity is often useful to handle click events
public MapEventListener(Activity activity) {
this.activity = activity;
}
// Vector element (touch) handlers
@Override
public void onLabelClicked(VectorElement vectorElement, boolean longClick) {
Toast.makeText(activity, "onLabelClicked "+((DefaultLabel) vectorElement.getLabel()).getTitle()+" longClick: "+longClick, Toast.LENGTH_SHORT).show();
}
@Override
public void onVectorElementClicked(VectorElement vectorElement, boolean longClick) {
Toast.makeText(activity, "onVectorElementClicked "+((DefaultLabel) vectorElement.getLabel()).getTitle()+" longClick: "+longClick, Toast.LENGTH_SHORT).show();
}
// Map View manipulation handlers
@Override
public void onMapClicked(final float x, final float y, final boolean longClick) {
Toast.makeText(activity, "onMapClicked "+(new EPSG3857()).toWgs84(x, y).x+" "+(new EPSG3857()).toWgs84(x, y).y+" longClick: "+longClick, Toast.LENGTH_SHORT).show();
}
@Override
public void onMapMoved() {
}
// Progress indication handlers
@Override
public void onBackgroundTasksStarted() {
// This method is called when mapping library is performing relatively long lasting operations.
// This is good place to show some progress indicator.
// NOTE: in order to make title progress bar work, place
// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
// just before setContentView call in your Activity's onCreate method.
activity.setProgressBarIndeterminateVisibility(true);
}
@Override
public void onBackgroundTasksFinished() {
// This method is called when mapping library has finished all long lasting operations.
activity.setProgressBarIndeterminateVisibility(false);
}
}
- Configure MapView to send events to the MapListener. Following goes to onCreate() of your Map Activity, where other MapView configurations are done:
MapEventListener mapListener = new MapEventListener(this);
mapView.getOptions().setMapListener(mapListener);
And now try clicking and long-clicking to your marker or somewhere else on map to see when and which events are called.
Probably you want to know which Marker (or other VectorElement) was clicked to have specific handling, like loading data from database, open new activity etc. For this Markers and others have userData Object as constructor parameter and public field: there you can add (and later retrieve from the field) any Object with all the additional application-specific Marker data, or just a object id.