summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java503
1 files changed, 252 insertions, 251 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
index b33d489da2..34d2c31139 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
@@ -31,270 +31,271 @@ import java.lang.ref.WeakReference;
*/
public class InfoWindow {
- private WeakReference<Marker> mBoundMarker;
- private WeakReference<MapboxMap> mMapboxMap;
- protected WeakReference<View> mView;
-
- private float mMarkerHeightOffset;
- private float mMarkerWidthOffset;
- private float mViewWidthOffset;
- private PointF mCoordinates;
- private boolean mIsVisible;
-
- @LayoutRes
- private int mLayoutRes;
-
- InfoWindow(MapView mapView, int layoutResId, MapboxMap mapboxMap) {
- mLayoutRes = layoutResId;
- View view = LayoutInflater.from(mapView.getContext()).inflate(layoutResId, mapView, false);
- initialize(view, mapboxMap);
- }
+ private WeakReference<Marker> boundMarker;
+ private WeakReference<MapboxMap> mapboxMap;
+ protected WeakReference<View> view;
+
+ private float markerHeightOffset;
+ private float markerWidthOffset;
+ private float viewWidthOffset;
+ private PointF coordinates;
+ private boolean isVisible;
+
+ @LayoutRes
+ private int layoutRes;
+
+ InfoWindow(MapView mapView, int layoutResId, MapboxMap mapboxMap) {
+ layoutRes = layoutResId;
+ View view = LayoutInflater.from(mapView.getContext()).inflate(layoutResId, mapView, false);
+ initialize(view, mapboxMap);
+ }
+
+ InfoWindow(View view, MapboxMap mapboxMap) {
+ initialize(view, mapboxMap);
+ }
+
+ private void initialize(View view, MapboxMap mapboxMap) {
+ this.mapboxMap = new WeakReference<>(mapboxMap);
+ isVisible = false;
+ this.view = new WeakReference<>(view);
+
+ view.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ MapboxMap mapboxMap = InfoWindow.this.mapboxMap.get();
+ if (mapboxMap != null) {
+ MapboxMap.OnInfoWindowClickListener onInfoWindowClickListener = mapboxMap.getOnInfoWindowClickListener();
+ boolean handledDefaultClick = false;
+ if (onInfoWindowClickListener != null) {
+ handledDefaultClick = onInfoWindowClickListener.onInfoWindowClick(getBoundMarker());
+ }
+
+ if (!handledDefaultClick) {
+ // default behavior: close it when clicking on the tooltip:
+ close();
+ }
+ }
+ }
+ });
+
+ view.setOnLongClickListener(new View.OnLongClickListener() {
+ @Override
+ public boolean onLongClick(View v) {
+ MapboxMap mapboxMap = InfoWindow.this.mapboxMap.get();
+ if (mapboxMap != null) {
+ MapboxMap.OnInfoWindowLongClickListener listener = mapboxMap.getOnInfoWindowLongClickListener();
+ if (listener != null) {
+ listener.onInfoWindowLongClick(getBoundMarker());
+ }
+ }
+ return true;
+ }
+ });
+ }
+
+
+ /**
+ * Open the info window at the specified position.
+ *
+ * @param boundMarker The marker on which is hooked the view.
+ * @param position to place the window on the map.
+ * @param offsetX The offset of the view to the position, in pixels. This allows to offset
+ * the view from the object position.
+ * @param offsetY The offset of the view to the position, in pixels. This allows to offset
+ * the view from the object position.
+ * @return this {@link InfoWindow}.
+ */
+ InfoWindow open(MapView mapView, Marker boundMarker, LatLng position, int offsetX, int offsetY) {
+ setBoundMarker(boundMarker);
+
+ MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
+ MapView.LayoutParams.WRAP_CONTENT);
+
+ MapboxMap mapboxMap = this.mapboxMap.get();
+ View view = this.view.get();
+ if (view != null && mapboxMap != null) {
+ view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
+
+ // Calculate y-offset for update method
+ markerHeightOffset = -view.getMeasuredHeight() + offsetY;
+ markerWidthOffset = -offsetX;
+
+ // Calculate default Android x,y coordinate
+ coordinates = mapboxMap.getProjection().toScreenLocation(position);
+ float x = coordinates.x - (view.getMeasuredWidth() / 2) + offsetX;
+ float y = coordinates.y - view.getMeasuredHeight() + offsetY;
+
+ if (view instanceof InfoWindowView) {
+ // only apply repositioning/margin for InfoWindowView
+ Resources resources = mapView.getContext().getResources();
+
+ // get right/left popup window
+ float rightSideInfowWindow = x + view.getMeasuredWidth();
+ float leftSideInfoWindow = x;
+
+ // get right/left map view
+ final float mapRight = mapView.getRight();
+ final float mapLeft = mapView.getLeft();
+
+ float marginHorizontal = resources.getDimension(R.dimen.mapbox_infowindow_margin);
+ float tipViewOffset = resources.getDimension(R.dimen.mapbox_infowindow_tipview_width) / 2;
+ float tipViewMarginLeft = view.getMeasuredWidth() / 2 - tipViewOffset;
+
+ boolean outOfBoundsLeft = false;
+ boolean outOfBoundsRight = false;
+
+ // only optimise margins if view is inside current viewport
+ if (coordinates.x >= 0 && coordinates.x <= mapView.getWidth()
+ && coordinates.y >= 0 && coordinates.y <= mapView.getHeight()) {
+
+ // if out of bounds right
+ if (rightSideInfowWindow > mapRight) {
+ outOfBoundsRight = true;
+ x -= rightSideInfowWindow - mapRight;
+ tipViewMarginLeft += rightSideInfowWindow - mapRight + tipViewOffset;
+ rightSideInfowWindow = x + view.getMeasuredWidth();
+ }
+
+ // fit screen left
+ if (leftSideInfoWindow < mapLeft) {
+ outOfBoundsLeft = true;
+ x += mapLeft - leftSideInfoWindow;
+ tipViewMarginLeft -= mapLeft - leftSideInfoWindow + tipViewOffset;
+ leftSideInfoWindow = x;
+ }
+
+ // Add margin right
+ if (outOfBoundsRight && mapRight - rightSideInfowWindow < marginHorizontal) {
+ x -= marginHorizontal - (mapRight - rightSideInfowWindow);
+ tipViewMarginLeft += marginHorizontal - (mapRight - rightSideInfowWindow) - tipViewOffset;
+ leftSideInfoWindow = x;
+ }
+
+ // Add margin left
+ if (outOfBoundsLeft && leftSideInfoWindow - mapLeft < marginHorizontal) {
+ x += marginHorizontal - (leftSideInfoWindow - mapLeft);
+ tipViewMarginLeft -= (marginHorizontal - (leftSideInfoWindow - mapLeft)) - tipViewOffset;
+ }
+ }
- InfoWindow(View view, MapboxMap mapboxMap) {
- initialize(view, mapboxMap);
- }
+ // Adjust tipView
+ InfoWindowView infoWindowView = (InfoWindowView) view;
+ infoWindowView.setTipViewMarginLeft((int) tipViewMarginLeft);
+ }
- private void initialize(View view, MapboxMap mapboxMap) {
- mMapboxMap = new WeakReference<>(mapboxMap);
- mIsVisible = false;
- mView = new WeakReference<>(view);
-
- view.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- MapboxMap mapboxMap = mMapboxMap.get();
- if (mapboxMap != null) {
- MapboxMap.OnInfoWindowClickListener onInfoWindowClickListener = mapboxMap.getOnInfoWindowClickListener();
- boolean handledDefaultClick = false;
- if (onInfoWindowClickListener != null) {
- handledDefaultClick = onInfoWindowClickListener.onInfoWindowClick(getBoundMarker());
- }
-
- if (!handledDefaultClick) {
- // default behavior: close it when clicking on the tooltip:
- close();
- }
- }
- }
- });
-
- view.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- MapboxMap mapboxMap = mMapboxMap.get();
- if (mapboxMap != null) {
- MapboxMap.OnInfoWindowLongClickListener listener = mapboxMap.getOnInfoWindowLongClickListener();
- if (listener != null) {
- listener.onInfoWindowLongClick(getBoundMarker());
- }
- }
- return true;
- }
- });
- }
+ // set anchor popupwindowview
+ view.setX(x);
+ view.setY(y);
+ // Calculate x-offset for update method
+ viewWidthOffset = x - coordinates.x - offsetX;
- /**
- * Open the info window at the specified position.
- *
- * @param boundMarker The marker on which is hooked the view.
- * @param position to place the window on the map.
- * @param offsetX The offset of the view to the position, in pixels. This allows to offset
- * the view from the object position.
- * @param offsetY The offset of the view to the position, in pixels. This allows to offset
- * the view from the object position.
- * @return this {@link InfoWindow}.
- */
- InfoWindow open(MapView mapView, Marker boundMarker, LatLng position, int offsetX, int offsetY) {
- setBoundMarker(boundMarker);
-
- MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT);
-
- MapboxMap mapboxMap = mMapboxMap.get();
- View view = mView.get();
- if (view != null && mapboxMap != null) {
- view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
-
- // Calculate y-offset for update method
- mMarkerHeightOffset = -view.getMeasuredHeight() + offsetY;
- mMarkerWidthOffset = -offsetX;
-
- // Calculate default Android x,y coordinate
- mCoordinates = mapboxMap.getProjection().toScreenLocation(position);
- float x = mCoordinates.x - (view.getMeasuredWidth() / 2) + offsetX;
- float y = mCoordinates.y - view.getMeasuredHeight() + offsetY;
-
- if (view instanceof InfoWindowView) {
- // only apply repositioning/margin for InfoWindowView
- Resources resources = mapView.getContext().getResources();
-
- // get right/left popup window
- float rightSideInfowWindow = x + view.getMeasuredWidth();
- float leftSideInfoWindow = x;
-
- // get right/left map view
- final float mapRight = mapView.getRight();
- final float mapLeft = mapView.getLeft();
-
- float marginHorizontal = resources.getDimension(R.dimen.mapbox_infowindow_margin);
- float tipViewOffset = resources.getDimension(R.dimen.mapbox_infowindow_tipview_width) / 2;
- float tipViewMarginLeft = view.getMeasuredWidth() / 2 - tipViewOffset;
-
- boolean outOfBoundsLeft = false;
- boolean outOfBoundsRight = false;
-
- // only optimise margins if view is inside current viewport
- if (mCoordinates.x >= 0 && mCoordinates.x <= mapView.getWidth()
- && mCoordinates.y >= 0 && mCoordinates.y <= mapView.getHeight()) {
-
- // if out of bounds right
- if (rightSideInfowWindow > mapRight) {
- outOfBoundsRight = true;
- x -= rightSideInfowWindow - mapRight;
- tipViewMarginLeft += rightSideInfowWindow - mapRight + tipViewOffset;
- rightSideInfowWindow = x + view.getMeasuredWidth();
- }
-
- // fit screen left
- if (leftSideInfoWindow < mapLeft) {
- outOfBoundsLeft = true;
- x += mapLeft - leftSideInfoWindow;
- tipViewMarginLeft -= mapLeft - leftSideInfoWindow + tipViewOffset;
- leftSideInfoWindow = x;
- }
-
- // Add margin right
- if (outOfBoundsRight && mapRight - rightSideInfowWindow < marginHorizontal) {
- x -= marginHorizontal - (mapRight - rightSideInfowWindow);
- tipViewMarginLeft += marginHorizontal - (mapRight - rightSideInfowWindow) - tipViewOffset;
- leftSideInfoWindow = x;
- }
-
- // Add margin left
- if (outOfBoundsLeft && leftSideInfoWindow - mapLeft < marginHorizontal) {
- x += marginHorizontal - (leftSideInfoWindow - mapLeft);
- tipViewMarginLeft -= (marginHorizontal - (leftSideInfoWindow - mapLeft)) - tipViewOffset;
- }
- }
-
- // Adjust tipView
- InfoWindowView infoWindowView = (InfoWindowView) view;
- infoWindowView.setTipViewMarginLeft((int) tipViewMarginLeft);
- }
-
- // set anchor popupwindowview
- view.setX(x);
- view.setY(y);
-
- // Calculate x-offset for update method
- mViewWidthOffset = x - mCoordinates.x - offsetX;
-
- close(); //if it was already opened
- mapView.addView(view, lp);
- mIsVisible = true;
- }
- return this;
+ close(); //if it was already opened
+ mapView.addView(view, lp);
+ isVisible = true;
}
-
- /**
- * Close this {@link InfoWindow} if it is visible, otherwise calling this will do nothing.
- *
- * @return This {@link InfoWindow}
- */
- InfoWindow close() {
- MapboxMap mapboxMap = mMapboxMap.get();
- if (mIsVisible && mapboxMap != null) {
- mIsVisible = false;
- View view = mView.get();
- if (view != null && view.getParent() != null) {
- ((ViewGroup) view.getParent()).removeView(view);
- }
-
- Marker marker = getBoundMarker();
- MapboxMap.OnInfoWindowCloseListener listener = mapboxMap.getOnInfoWindowCloseListener();
- if (listener != null) {
- listener.onInfoWindowClose(marker);
- }
-
- setBoundMarker(null);
- }
- return this;
+ return this;
+ }
+
+ /**
+ * Close this {@link InfoWindow} if it is visible, otherwise calling this will do nothing.
+ *
+ * @return This {@link InfoWindow}
+ */
+ InfoWindow close() {
+ MapboxMap mapboxMap = this.mapboxMap.get();
+ if (isVisible && mapboxMap != null) {
+ isVisible = false;
+ View view = this.view.get();
+ if (view != null && view.getParent() != null) {
+ ((ViewGroup) view.getParent()).removeView(view);
+ }
+
+ Marker marker = getBoundMarker();
+ MapboxMap.OnInfoWindowCloseListener listener = mapboxMap.getOnInfoWindowCloseListener();
+ if (listener != null) {
+ listener.onInfoWindowClose(marker);
+ }
+
+ setBoundMarker(null);
}
-
- /**
- * Constructs the view that is displayed when the InfoWindow opens. This retrieves data from
- * overlayItem and shows it in the tooltip.
- *
- * @param overlayItem the tapped overlay item
- */
- void adaptDefaultMarker(Marker overlayItem, MapboxMap mapboxMap, MapView mapView) {
- View view = mView.get();
- if (view == null) {
- view = LayoutInflater.from(mapView.getContext()).inflate(mLayoutRes, mapView, false);
- initialize(view, mapboxMap);
- }
- mMapboxMap = new WeakReference<>(mapboxMap);
- String title = overlayItem.getTitle();
- TextView titleTextView = ((TextView) view.findViewById(R.id.infowindow_title));
- if (!TextUtils.isEmpty(title)) {
- titleTextView.setText(title);
- titleTextView.setVisibility(View.VISIBLE);
- } else {
- titleTextView.setVisibility(View.GONE);
- }
-
- String snippet = overlayItem.getSnippet();
- TextView snippetTextView = ((TextView) view.findViewById(R.id.infowindow_description));
- if (!TextUtils.isEmpty(snippet)) {
- snippetTextView.setText(snippet);
- snippetTextView.setVisibility(View.VISIBLE);
- } else {
- snippetTextView.setVisibility(View.GONE);
- }
+ return this;
+ }
+
+ /**
+ * Constructs the view that is displayed when the InfoWindow opens. This retrieves data from
+ * overlayItem and shows it in the tooltip.
+ *
+ * @param overlayItem the tapped overlay item
+ */
+ void adaptDefaultMarker(Marker overlayItem, MapboxMap mapboxMap, MapView mapView) {
+ View view = this.view.get();
+ if (view == null) {
+ view = LayoutInflater.from(mapView.getContext()).inflate(layoutRes, mapView, false);
+ initialize(view, mapboxMap);
}
-
- InfoWindow setBoundMarker(Marker boundMarker) {
- mBoundMarker = new WeakReference<>(boundMarker);
- return this;
+ this.mapboxMap = new WeakReference<>(mapboxMap);
+ String title = overlayItem.getTitle();
+ TextView titleTextView = ((TextView) view.findViewById(R.id.infowindow_title));
+ if (!TextUtils.isEmpty(title)) {
+ titleTextView.setText(title);
+ titleTextView.setVisibility(View.VISIBLE);
+ } else {
+ titleTextView.setVisibility(View.GONE);
}
- Marker getBoundMarker() {
- if (mBoundMarker == null) {
- return null;
- }
- return mBoundMarker.get();
+ String snippet = overlayItem.getSnippet();
+ TextView snippetTextView = ((TextView) view.findViewById(R.id.infowindow_description));
+ if (!TextUtils.isEmpty(snippet)) {
+ snippetTextView.setText(snippet);
+ snippetTextView.setVisibility(View.VISIBLE);
+ } else {
+ snippetTextView.setVisibility(View.GONE);
}
+ }
- /**
- * Will result in getting this {@link InfoWindow} and updating the view being displayed.
- */
- public void update() {
- MapboxMap mapboxMap = mMapboxMap.get();
- Marker marker = mBoundMarker.get();
- View view = mView.get();
- if (mapboxMap != null && marker != null && view != null) {
- mCoordinates = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
-
- if (view instanceof InfoWindowView) {
- view.setX(mCoordinates.x + mViewWidthOffset - mMarkerWidthOffset);
- } else {
- view.setX(mCoordinates.x - (view.getMeasuredWidth() / 2) - mMarkerWidthOffset);
- }
- view.setY(mCoordinates.y + mMarkerHeightOffset);
- }
- }
+ InfoWindow setBoundMarker(Marker boundMarker) {
+ this.boundMarker = new WeakReference<>(boundMarker);
+ return this;
+ }
- /**
- * Retrieve this {@link InfoWindow}'s current view being used.
- *
- * @return This {@link InfoWindow}'s current View.
- */
- public View getView() {
- return mView != null ? mView.get() : null;
+ Marker getBoundMarker() {
+ if (boundMarker == null) {
+ return null;
}
-
- boolean isVisible() {
- return mIsVisible;
+ return boundMarker.get();
+ }
+
+ /**
+ * Will result in getting this {@link InfoWindow} and updating the view being displayed.
+ */
+ public void update() {
+ MapboxMap mapboxMap = this.mapboxMap.get();
+ Marker marker = boundMarker.get();
+ View view = this.view.get();
+ if (mapboxMap != null && marker != null && view != null) {
+ coordinates = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
+
+ if (view instanceof InfoWindowView) {
+ view.setX(coordinates.x + viewWidthOffset - markerWidthOffset);
+ } else {
+ view.setX(coordinates.x - (view.getMeasuredWidth() / 2) - markerWidthOffset);
+ }
+ view.setY(coordinates.y + markerHeightOffset);
}
+ }
+
+ /**
+ * Retrieve this {@link InfoWindow}'s current view being used.
+ *
+ * @return This {@link InfoWindow}'s current View.
+ */
+ public View getView() {
+ return view != null ? view.get() : null;
+ }
+
+ boolean isVisible() {
+ return isVisible;
+ }
}