Add InfoWindowAdapter, had to move some parts in InfoWindow + Marker
InfoWindowAdapter example in sample app
This commit is contained in:
@@ -157,11 +157,24 @@ public class Marker extends Annotation {
|
||||
return;
|
||||
}
|
||||
|
||||
getInfoWindow().adaptDefaultMarker(this);
|
||||
getInfoWindow().open(this, getPosition(), (int) anchorU, (int) anchorV);
|
||||
getInfoWindow().setBoundMarker(this);
|
||||
infoWindowShown = true;
|
||||
}
|
||||
|
||||
public void showInfoWindow(View view){
|
||||
|
||||
if (!isVisible() || mapView == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
infoWindow = new InfoWindow(view, mapView);
|
||||
infoWindow.open(this, getPosition(), (int) anchorU, (int) anchorV);
|
||||
infoWindow.setBoundMarker(this);
|
||||
infoWindowShown = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use to set a custom OnTouchListener for the InfoWindow.
|
||||
* By default the InfoWindow will close on touch.
|
||||
|
||||
@@ -81,7 +81,6 @@ public final class InfoWindow {
|
||||
* @return this infowindow
|
||||
*/
|
||||
public InfoWindow open(Marker object, LatLng position, int offsetX, int offsetY) {
|
||||
onOpen(object);
|
||||
MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT);
|
||||
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
|
||||
|
||||
@@ -141,7 +140,7 @@ public final class InfoWindow {
|
||||
*
|
||||
* @param overlayItem the tapped overlay item
|
||||
*/
|
||||
public void onOpen(Marker overlayItem) {
|
||||
public void adaptDefaultMarker(Marker overlayItem) {
|
||||
String title = overlayItem.getTitle();
|
||||
((TextView) mView.findViewById(mTitleId /*R.id.title*/)).setText(title);
|
||||
String snippet = overlayItem.getSnippet();
|
||||
|
||||
@@ -192,6 +192,7 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
private List<Annotation> mAnnotations = new ArrayList<>();
|
||||
private List<Annotation> mAnnotationsNearLastTap = new ArrayList<>();
|
||||
private Annotation mSelectedAnnotation = null;
|
||||
private InfoWindowAdapter mInfoWindowAdapter;
|
||||
|
||||
// Used for the Mapbox Logo
|
||||
private ImageView mLogoView;
|
||||
@@ -395,6 +396,19 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
void onMapChanged(MapChange change);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when an {@link InfoWindow} will be shown.
|
||||
*/
|
||||
public interface InfoWindowAdapter {
|
||||
/**
|
||||
* Called when the user clicks on a marker.
|
||||
*
|
||||
* @param marker The marker the user clicked on.
|
||||
* @return View to be shown as a {@link InfoWindow}
|
||||
*/
|
||||
View getInfoWindow(Marker marker);
|
||||
}
|
||||
|
||||
//
|
||||
// Constructors
|
||||
//
|
||||
@@ -1503,12 +1517,20 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
deselectAnnotation();
|
||||
|
||||
Marker marker = (Marker) annotation;
|
||||
boolean handledClick = false;
|
||||
boolean handledDefaultClick = false;
|
||||
if (onMarkerClickListener != null) {
|
||||
handledClick = onMarkerClickListener.onMarkerClick(marker);
|
||||
// end developer has provided a custom click listener
|
||||
handledDefaultClick = onMarkerClickListener.onMarkerClick(marker);
|
||||
}
|
||||
|
||||
if (!handledClick) {
|
||||
if(mInfoWindowAdapter!=null){
|
||||
// end developer is using a custom InfoWindowAdapter
|
||||
View content = mInfoWindowAdapter.getInfoWindow(marker);
|
||||
if(content != null){
|
||||
marker.showInfoWindow(content);
|
||||
}
|
||||
}else if (!handledDefaultClick) {
|
||||
// default behaviour
|
||||
marker.showInfoWindow();
|
||||
}
|
||||
|
||||
@@ -2331,7 +2353,7 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
//
|
||||
|
||||
/**
|
||||
* Add an OnMapChangedListner
|
||||
* Add an OnMapChangedListener
|
||||
*
|
||||
* @param listener Listener to add
|
||||
*/
|
||||
@@ -2341,6 +2363,15 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an InfoWindowAdapter
|
||||
*
|
||||
* @param infoWindowAdapter to set
|
||||
*/
|
||||
public void setInfoWindowAdapter(@NonNull InfoWindowAdapter infoWindowAdapter){
|
||||
mInfoWindowAdapter = infoWindowAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an OnMapChangedListener
|
||||
*
|
||||
@@ -2659,4 +2690,4 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user