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

Commit

Permalink
[android] #3573 - Marker clicks stop working after orientation change
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jan 18, 2016
1 parent 2dae355 commit 74a9c50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mapbox.mapboxsdk.annotations;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.PointF;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -31,18 +31,12 @@ public class InfoWindow {
private boolean mIsVisible;
protected WeakReference<View> mView;

static int mTitleId = 0;
static int mDescriptionId = 0;
static int mSubDescriptionId = 0;
static int mImageId = 0;
@LayoutRes
private int mLayoutRes;

InfoWindow(int layoutResId, MapView mapView) {
mLayoutRes = layoutResId;
View view = LayoutInflater.from(mapView.getContext()).inflate(layoutResId, mapView, false);

if (mTitleId == 0) {
setResIds(mapView.getContext());
}

initialize(view, mapView);
}

Expand Down Expand Up @@ -86,7 +80,7 @@ public boolean onTouch(View v, MotionEvent e) {
* This allows to offset the view from the object position.
* @return this infowindow
*/
InfoWindow open(Marker boundMarker, LatLng position, int offsetX, int offsetY) {
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);
Expand All @@ -99,21 +93,21 @@ InfoWindow open(Marker boundMarker, LatLng position, int offsetX, int offsetY) {
mMarkerHeightOffset = -view.getMeasuredHeight() + offsetY;

// Calculate default Android x,y coordinate
mCoordinates = mMapView.get().toScreenLocation(position);
mCoordinates = mapView.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 = mMapView.get().getContext().getResources();
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 = mMapView.get().getRight();
final float mapLeft = mMapView.get().getLeft();
final float mapRight = mapView.getRight();
final float mapLeft = mapView.getLeft();

float marginHorizontal = resources.getDimension(R.dimen.infowindow_margin);
float tipViewOffset = resources.getDimension(R.dimen.infowindow_tipview_width) / 2;
Expand Down Expand Up @@ -164,7 +158,7 @@ InfoWindow open(Marker boundMarker, LatLng position, int offsetX, int offsetY) {
mViewWidthOffset = x - mCoordinates.x - offsetX;

close(); //if it was already opened
mMapView.get().addView(view, lp);
mapView.addView(view, lp);
mIsVisible = true;
}
return this;
Expand Down Expand Up @@ -196,25 +190,15 @@ InfoWindow close() {
*/
void adaptDefaultMarker(Marker overlayItem, MapView mapView) {
View view = mView.get();
if (view != null) {
mMapView = new WeakReference<>(mapView);
overlayItem.setMapView(mapView);
String title = overlayItem.getTitle();
((TextView) view.findViewById(mTitleId /*R.id.title*/)).setText(title);
String snippet = overlayItem.getSnippet();
((TextView) view.findViewById(mDescriptionId /*R.id.description*/)).setText(snippet);
}
/*
//handle sub-description, hiding or showing the text view:
TextView subDescText = (TextView) mView.findViewById(mSubDescriptionId);
String subDesc = overlayItem.getSubDescription();
if ("".equals(subDesc)) {
subDescText.setVisibility(View.GONE);
} else {
subDescText.setText(subDesc);
subDescText.setVisibility(View.VISIBLE);
if (view == null) {
view = LayoutInflater.from(mapView.getContext()).inflate(mLayoutRes, mapView, false);
initialize(view, mapView);
}
*/
mMapView = new WeakReference<>(mapView);
String title = overlayItem.getTitle();
((TextView) view.findViewById(R.id.infowindow_title)).setText(title);
String snippet = overlayItem.getSnippet();
((TextView) view.findViewById(R.id.infowindow_description)).setText(snippet);
}

private void onClose() {
Expand All @@ -233,22 +217,6 @@ Marker getBoundMarker() {
return mBoundMarker.get();
}

/**
* Given a context, set the resource ids for the layout
* of the InfoWindow.
*
* @param context the apps Context
*/
private static void setResIds(Context context) {
String packageName = context.getPackageName(); //get application package name
mTitleId = context.getResources().getIdentifier("id/infowindow_title", null, packageName);
mDescriptionId =
context.getResources().getIdentifier("id/infowindow_description", null, packageName);
mSubDescriptionId = context.getResources()
.getIdentifier("id/infowindow_subdescription", null, packageName);
mImageId = context.getResources().getIdentifier("id/infowindow_image", null, packageName);
}

public void update() {
MapView mapView = mMapView.get();
Marker marker = mBoundMarker.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ public InfoWindow showInfoWindow(@NonNull MapView mapView) {
View content = infoWindowAdapter.getInfoWindow(this);
if (content != null) {
infoWindow = new InfoWindow(content, getMapView());
showInfoWindow(infoWindow);
showInfoWindow(infoWindow, mapView);
return infoWindow;
}
}

getInfoWindow().adaptDefaultMarker(this, mapView);
return showInfoWindow(getInfoWindow());
return showInfoWindow(getInfoWindow(), mapView);
}

private InfoWindow showInfoWindow(InfoWindow iw) {
iw.open(this, getPosition(), 0, topOffsetPixels);
private InfoWindow showInfoWindow(InfoWindow iw, MapView mapView) {
iw.open(mapView, this, getPosition(), 0, topOffsetPixels);
infoWindowShown = true;
return iw;
}
Expand Down

0 comments on commit 74a9c50

Please sign in to comment.