Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Fix info window placement on programatic marker select
Browse files Browse the repository at this point in the history
Fixes #5348
  • Loading branch information
ivovandongen committed Jun 17, 2016
1 parent 2591147 commit a2dbec8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
Expand Down Expand Up @@ -376,11 +377,7 @@ public void onClick(final View v) {
}

if (!clickHandled) {
// InfoWindow offset
int infoWindowOffsetX = (int) ((adaptedView.getWidth() * marker.getInfoWindowAnchorU()) - marker.getOffsetX());
int infoWindowOffsetY = (int) ((adaptedView.getHeight() * marker.getInfoWindowAnchorV()) - marker.getOffsetY());
marker.setTopOffsetPixels(infoWindowOffsetY);
marker.setRightOffsetPixels(infoWindowOffsetX);
ensureInfoWindowOffset(marker);
select(marker, v, adapter);
}
}
Expand All @@ -397,6 +394,44 @@ public void onClick(final View v) {
}
}

//TODO: This whole method is a stopgap for: https://github.com/mapbox/mapbox-gl-native/issues/5384
public void ensureInfoWindowOffset(MarkerView marker) {
View view = null;
if (markerViewMap.containsKey(marker)) {
view = markerViewMap.get(marker);
} else {
for (final MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) {
if (adapter.getMarkerClass().equals(marker.getClass())) {
View convertView = (View) adapter.getViewReusePool().acquire();
view = adapter.getView(marker, convertView, mapView);
break;
}
}
}

if (view != null) {
//Ensure the marker's view is measured first
if (view.getMeasuredWidth() == 0) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
}

// update position on map
if (marker.getOffsetX() == -1) {
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
int x = (int) (marker.getAnchorU() * view.getMeasuredWidth());
int y = (int) (marker.getAnchorV() * view.getMeasuredHeight());
marker.setOffsetX(x);
marker.setOffsetY(y);
}

// InfoWindow offset
int infoWindowOffsetX = (int) ((view.getMeasuredWidth() * marker.getInfoWindowAnchorU()) - marker.getOffsetX());
int infoWindowOffsetY = (int) ((view.getMeasuredHeight() * marker.getInfoWindowAnchorV()) - marker.getOffsetY());
marker.setTopOffsetPixels(infoWindowOffsetY);
marker.setRightOffsetPixels(infoWindowOffsetX);
}
}

/**
* Default MarkerViewAdapter used for base class of MarkerView to adapt a MarkerView to an ImageView
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,11 @@ public void selectMarker(@NonNull Marker marker) {
}

if (!handledDefaultClick) {

if (marker instanceof MarkerView) {
mMarkerViewManager.ensureInfoWindowOffset((MarkerView) marker);
}

if (isInfoWindowValidForMarker(marker) || getInfoWindowAdapter() != null) {
mInfoWindows.add(marker.showInfoWindow(this, mMapView));
}
Expand Down

0 comments on commit a2dbec8

Please sign in to comment.