Skip to content

Commit

Permalink
mapbox#2283 - Supporting custom InfoWindow touch listeners via Marker…
Browse files Browse the repository at this point in the history
….setInfoWindowOnTouchListener(). Added Test to Custom Annotations in TestApp's MainActivity.
  • Loading branch information
bleege authored and incanus committed Sep 11, 2015
1 parent c453e7d commit f201aed
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxgl.annotations;

import android.graphics.Point;
import android.view.View;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.views.R;

Expand Down Expand Up @@ -145,13 +146,32 @@ public void showInfoWindow() {
return;
}

getInfoWindow().open(this, getPosition(), (int) anchorU, (int) anchorV);
getInfoWindow().setBoundMarker(this);
infoWindowShown = true;
}

/**
* Use to set a custom OnTouchListener for the InfoWindow.
* By default the InfoWindow will close on touch.
* @param listener Custom OnTouchListener
*/
public void setInfoWindowOnTouchListener(View.OnTouchListener listener) {
if (listener == null) {
return;
}
getInfoWindow().setOnTouchListener(listener);
}

/**
* Common internal InfoWindow initialization method
* @return InfoWindow for Marker
*/
private InfoWindow getInfoWindow() {
if (infoWindow == null) {
infoWindow = new InfoWindow(R.layout.infowindow, mapView);
}

infoWindow.open(this, getPosition(), (int)anchorU, (int)anchorV);
infoWindow.setBoundMarker(this);
infoWindowShown = true;
return infoWindow;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
Expand All @@ -30,7 +27,6 @@
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ScaleGestureDetectorCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.ScaleGestureDetector;
Expand Down Expand Up @@ -484,13 +480,19 @@ public List<Polygon> addPolygons(List<PolygonOptions> polygonOptions) {
private void removeAnnotationsWithId(long annotationId){
for (Iterator<Annotation> iterator = mAnnotations.iterator(); iterator.hasNext();) {
Annotation annotation = iterator.next();
if (annotation instanceof Marker) {
((Marker)annotation).hideInfoWindow();
}
if (annotation.getId() == annotationId) {
iterator.remove();
}
}
}

public void removeAnnotation(Annotation annotation) {
if (annotation instanceof Marker) {
((Marker)annotation).hideInfoWindow();
}
long id = annotation.getId();
mNativeMapView.removeAnnotation(id);
mAnnotations.remove(annotation);
Expand All @@ -504,8 +506,12 @@ public void removeAnnotation(long annotationId) {
public void removeAnnotations() {
long[] ids = new long[mAnnotations.size()];
for(int i = 0; i < mAnnotations.size(); i++) {
long id = mAnnotations.get(i).getId();
Annotation annotation = mAnnotations.get(i);
long id = annotation.getId();
ids[i] = id;
if (annotation instanceof Marker) {
((Marker)annotation).hideInfoWindow();
}
}
mNativeMapView.removeAnnotations(ids);
mAnnotations.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
import com.mapbox.mapboxgl.annotations.PolygonOptions;
import com.mapbox.mapboxgl.annotations.PolylineOptions;
Expand Down Expand Up @@ -395,10 +397,18 @@ private void toggleAnnotations(boolean enableAnnotations) {

private void addMarkers() {
LatLng backLot = new LatLng(38.649441, -121.369064);
mMapView.addMarker(new MarkerOptions()
final Marker marker = mMapView.addMarker(new MarkerOptions()
.position(backLot)
.title("Back Lot")
.snippet("The back lot behind my house"));
marker.setInfoWindowOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getApplicationContext(), "Custom Info Touch Listener!!", Toast.LENGTH_SHORT).show();
marker.hideInfoWindow();
return true;
}
});

LatLng cheeseRoom = new LatLng(38.531577,-122.010646);
mMapView.addMarker(new MarkerOptions()
Expand Down

0 comments on commit f201aed

Please sign in to comment.