diff options
author | Tobrun <tobrun@mapbox.com> | 2016-04-06 12:32:50 +0200 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2016-04-07 21:52:34 +0200 |
commit | c8ee5dadaa8e3501337d30b699d59f54ddf2a32c (patch) | |
tree | 5ba8aa2a87954e853b83c3274959af8104cfb913 /platform/android | |
parent | 891685188b6bbda1a79ab93d4a16d36ce9177047 (diff) | |
download | qtlocation-mapboxgl-c8ee5dadaa8e3501337d30b699d59f54ddf2a32c.tar.gz |
[android] #3643 - improved android touch target when calling getAnnotationsInBounds
Diffstat (limited to 'platform/android')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java | 564 |
1 files changed, 286 insertions, 278 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 06f487acba..e0d444d79a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -23,7 +23,6 @@ import android.location.Location; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.CallSuper; import android.support.annotation.FloatRange; @@ -113,7 +112,10 @@ import java.util.concurrent.CopyOnWriteArrayList; public class MapView extends FrameLayout { private MapboxMap mMapboxMap; + private List<Icon> mIcons; + private int mAverageIconHeight; + private int mAverageIconWidth; private NativeMapView mNativeMapView; private CompassView mCompassView; @@ -893,10 +895,24 @@ public class MapView extends FrameLayout { Icon loadIconForMarker(Marker marker) { Icon icon = marker.getIcon(); + + // calculating average before adding + int iconSize = mIcons.size() + 1; + + // TODO replace former if case with anchor implementation, + // current workaround for having extra pixels is diving height by 2 if (icon == null) { icon = IconFactory.getInstance(getContext()).defaultMarker(); + Bitmap bitmap = icon.getBitmap(); + mAverageIconHeight = mAverageIconHeight + (bitmap.getHeight() / 2 - mAverageIconHeight) / iconSize; + mAverageIconWidth = mAverageIconHeight + (bitmap.getWidth() - mAverageIconHeight) / iconSize; marker.setIcon(icon); + } else { + Bitmap bitmap = icon.getBitmap(); + mAverageIconHeight = mAverageIconHeight + (bitmap.getHeight() - mAverageIconHeight) / iconSize; + mAverageIconWidth = mAverageIconHeight + (bitmap.getWidth() - mAverageIconHeight) / iconSize; } + if (!mIcons.contains(icon)) { mIcons.add(icon); loadIcon(icon); @@ -926,7 +942,6 @@ public class MapView extends FrameLayout { density = DisplayMetrics.DENSITY_DEFAULT; } float scale = density / DisplayMetrics.DENSITY_DEFAULT; - mNativeMapView.addAnnotationIcon( id, bitmap.getWidth(), @@ -1036,15 +1051,10 @@ public class MapView extends FrameLayout { } private List<Marker> getMarkersInBounds(@NonNull LatLngBounds bbox) { - if (mDestroyed) { + if (mDestroyed || bbox==null) { return new ArrayList<>(); } - if (bbox == null) { - Log.w(MapboxConstants.TAG, "bbox was null, so just returning null"); - return null; - } - // TODO: filter in JNI using C++ parameter to getAnnotationsInBounds long[] ids = mNativeMapView.getAnnotationsInBounds(bbox); @@ -1537,17 +1547,15 @@ public class MapView extends FrameLayout { @Override public boolean onSingleTapConfirmed(MotionEvent e) { - // Open / Close InfoWindow - PointF tapPoint = new PointF(e.getX(), e.getY()); - List<Marker> selectedMarkers = mMapboxMap.getSelectedMarkers(); - final float toleranceSides = 15 * mScreenDensity; - final float toleranceTop = 20 * mScreenDensity; - final float toleranceBottom = 5 * mScreenDensity; - - RectF tapRect = new RectF(tapPoint.x - toleranceSides, tapPoint.y + toleranceTop, - tapPoint.x + toleranceSides, tapPoint.y - toleranceBottom); + PointF tapPoint = new PointF(e.getX(), e.getY()); + float toleranceSides = 4 * mScreenDensity; + float toleranceTopBottom = 10 * mScreenDensity; + RectF tapRect = new RectF(tapPoint.x - mAverageIconWidth / 2 - toleranceSides, + tapPoint.y - mAverageIconHeight / 2 - toleranceTopBottom, + tapPoint.x + mAverageIconWidth / 2 + toleranceSides, + tapPoint.y + mAverageIconHeight / 2 + toleranceTopBottom); LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.include(fromScreenLocation(new PointF(tapRect.left, tapRect.bottom))); @@ -1916,7 +1924,7 @@ public class MapView extends FrameLayout { } // This class handles input events from the zoom control buttons -// Zoom controls allow single touch only devices to zoom in and out + // Zoom controls allow single touch only devices to zoom in and out private class OnZoomListener implements ZoomButtonsController.OnZoomListener { // Not used @@ -2463,9 +2471,9 @@ public class MapView extends FrameLayout { } } -// -// View utility methods -// + // + // View utility methods + // private void setWidgetGravity(@NonNull final View view, int gravity) { LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); @@ -2483,295 +2491,295 @@ public class MapView extends FrameLayout { view.setLayoutParams(layoutParams); } -private static class AttributionOnClickListener implements View.OnClickListener, DialogInterface.OnClickListener { + private static class AttributionOnClickListener implements View.OnClickListener, DialogInterface.OnClickListener { - private static final int ATTRIBUTION_INDEX_IMPROVE_THIS_MAP = 2; - private static final int ATTRIBUTION_INDEX_TELEMETRY_SETTINGS = 3; - private MapView mMapView; + private static final int ATTRIBUTION_INDEX_IMPROVE_THIS_MAP = 2; + private static final int ATTRIBUTION_INDEX_TELEMETRY_SETTINGS = 3; + private MapView mMapView; - public AttributionOnClickListener(MapView mapView) { - super(); - mMapView = mapView; - } - - // Called when someone presses the attribution icon - @Override - public void onClick(View v) { - Context context = v.getContext(); - String[] items = context.getResources().getStringArray(R.array.attribution_names); - AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AttributionAlertDialogStyle); - builder.setTitle(R.string.attributionsDialogTitle); - builder.setAdapter(new ArrayAdapter<>(context, R.layout.attribution_list_item, items), this); - builder.show(); - } - - // Called when someone selects an attribution, 'Improve this map' adds location data to the url - @Override - public void onClick(DialogInterface dialog, int which) { - final Context context = ((Dialog) dialog).getContext(); - if (which == ATTRIBUTION_INDEX_TELEMETRY_SETTINGS) { + public AttributionOnClickListener(MapView mapView) { + super(); + mMapView = mapView; + } - int array = R.array.attribution_telemetry_options; - if (MapboxEventManager.getMapboxEventManager().isTelemetryEnabled()) { - array = R.array.attribution_telemetry_options_already_participating; - } - String[] items = context.getResources().getStringArray(array); + // Called when someone presses the attribution icon + @Override + public void onClick(View v) { + Context context = v.getContext(); + String[] items = context.getResources().getStringArray(R.array.attribution_names); AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AttributionAlertDialogStyle); - builder.setTitle(R.string.attributionTelemetryTitle); - LayoutInflater factory = LayoutInflater.from(context); - View content = factory.inflate(R.layout.attribution_telemetry_view, null); + builder.setTitle(R.string.attributionsDialogTitle); + builder.setAdapter(new ArrayAdapter<>(context, R.layout.attribution_list_item, items), this); + builder.show(); + } - ListView lv = (ListView) content.findViewById(R.id.telemetryOptionsList); - lv.setAdapter(new ArrayAdapter<String>(context, R.layout.attribution_list_item, items)); - lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); + // Called when someone selects an attribution, 'Improve this map' adds location data to the url + @Override + public void onClick(DialogInterface dialog, int which) { + final Context context = ((Dialog) dialog).getContext(); + if (which == ATTRIBUTION_INDEX_TELEMETRY_SETTINGS) { - builder.setView(content); - final AlertDialog telemDialog = builder.show(); - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - switch (position) { - case 0: - String url = context.getResources().getStringArray(R.array.attribution_links)[3]; - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - context.startActivity(intent); - telemDialog.cancel(); - return; - case 1: - MapboxEventManager.getMapboxEventManager().setTelemetryEnabled(false); - telemDialog.cancel(); - return; - case 2: - MapboxEventManager.getMapboxEventManager().setTelemetryEnabled(true); - telemDialog.cancel(); - return; - } + int array = R.array.attribution_telemetry_options; + if (MapboxEventManager.getMapboxEventManager().isTelemetryEnabled()) { + array = R.array.attribution_telemetry_options_already_participating; } - }); - return; - } - String url = context.getResources().getStringArray(R.array.attribution_links)[which]; - if (which == ATTRIBUTION_INDEX_IMPROVE_THIS_MAP) { - LatLng latLng = mMapView.getMapboxMap().getCameraPosition().target; - url = String.format(url, latLng.getLongitude(), latLng.getLatitude(), (int) mMapView.getZoom()); + String[] items = context.getResources().getStringArray(array); + AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AttributionAlertDialogStyle); + builder.setTitle(R.string.attributionTelemetryTitle); + LayoutInflater factory = LayoutInflater.from(context); + View content = factory.inflate(R.layout.attribution_telemetry_view, null); + + ListView lv = (ListView) content.findViewById(R.id.telemetryOptionsList); + lv.setAdapter(new ArrayAdapter<String>(context, R.layout.attribution_list_item, items)); + lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); + + builder.setView(content); + final AlertDialog telemDialog = builder.show(); + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + switch (position) { + case 0: + String url = context.getResources().getStringArray(R.array.attribution_links)[3]; + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + context.startActivity(intent); + telemDialog.cancel(); + return; + case 1: + MapboxEventManager.getMapboxEventManager().setTelemetryEnabled(false); + telemDialog.cancel(); + return; + case 2: + MapboxEventManager.getMapboxEventManager().setTelemetryEnabled(true); + telemDialog.cancel(); + return; + } + } + }); + return; + } + String url = context.getResources().getStringArray(R.array.attribution_links)[which]; + if (which == ATTRIBUTION_INDEX_IMPROVE_THIS_MAP) { + LatLng latLng = mMapView.getMapboxMap().getCameraPosition().target; + url = String.format(url, latLng.getLongitude(), latLng.getLatitude(), (int) mMapView.getZoom()); + } + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + context.startActivity(intent); } - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - context.startActivity(intent); } -} -private static class ZoomInvalidator implements Runnable { + private static class ZoomInvalidator implements Runnable { - private MapboxMap mapboxMap; + private MapboxMap mapboxMap; - public ZoomInvalidator(MapboxMap mapboxMap) { - this.mapboxMap = mapboxMap; - } + public ZoomInvalidator(MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + } - @Override - public void run() { - // invalidate camera position - mapboxMap.getCameraPosition(); + @Override + public void run() { + // invalidate camera position + mapboxMap.getCameraPosition(); + } } -} -/** - * Definition of a map change event. - * - * @see MapView.OnMapChangedListener#onMapChanged(int) - */ -@IntDef({REGION_WILL_CHANGE, - REGION_WILL_CHANGE_ANIMATED, - REGION_IS_CHANGING, - REGION_DID_CHANGE, - REGION_DID_CHANGE_ANIMATED, - WILL_START_LOADING_MAP, - DID_FINISH_LOADING_MAP, - DID_FAIL_LOADING_MAP, - WILL_START_RENDERING_FRAME, - DID_FINISH_RENDERING_FRAME, - DID_FINISH_RENDERING_FRAME_FULLY_RENDERED, - WILL_START_RENDERING_MAP, - DID_FINISH_RENDERING_MAP, - DID_FINISH_RENDERING_MAP_FULLY_RENDERED -}) -@Retention(RetentionPolicy.SOURCE) -public @interface MapChange { -} + /** + * Definition of a map change event. + * + * @see MapView.OnMapChangedListener#onMapChanged(int) + */ + @IntDef({REGION_WILL_CHANGE, + REGION_WILL_CHANGE_ANIMATED, + REGION_IS_CHANGING, + REGION_DID_CHANGE, + REGION_DID_CHANGE_ANIMATED, + WILL_START_LOADING_MAP, + DID_FINISH_LOADING_MAP, + DID_FAIL_LOADING_MAP, + WILL_START_RENDERING_FRAME, + DID_FINISH_RENDERING_FRAME, + DID_FINISH_RENDERING_FRAME_FULLY_RENDERED, + WILL_START_RENDERING_MAP, + DID_FINISH_RENDERING_MAP, + DID_FINISH_RENDERING_MAP_FULLY_RENDERED + }) + @Retention(RetentionPolicy.SOURCE) + public @interface MapChange { + } -/** - * <p> - * This {@link MapChange} is triggered whenever the currently displayed map region is about to changing - * without an animation. - * </p> - * <p> - * This event is followed by a series of {@link MapView#REGION_IS_CHANGING} and ends - * with {@link MapView#REGION_DID_CHANGE}. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int REGION_WILL_CHANGE = 0; + /** + * <p> + * This {@link MapChange} is triggered whenever the currently displayed map region is about to changing + * without an animation. + * </p> + * <p> + * This event is followed by a series of {@link MapView#REGION_IS_CHANGING} and ends + * with {@link MapView#REGION_DID_CHANGE}. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int REGION_WILL_CHANGE = 0; -/** - * <p> - * This {@link MapChange} is triggered whenever the currently displayed map region is about to changing - * with an animation. - * </p> - * <p> - * This event is followed by a series of {@link MapView#REGION_IS_CHANGING} and ends - * with {@link MapView#REGION_DID_CHANGE_ANIMATED}. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int REGION_WILL_CHANGE_ANIMATED = 1; + /** + * <p> + * This {@link MapChange} is triggered whenever the currently displayed map region is about to changing + * with an animation. + * </p> + * <p> + * This event is followed by a series of {@link MapView#REGION_IS_CHANGING} and ends + * with {@link MapView#REGION_DID_CHANGE_ANIMATED}. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int REGION_WILL_CHANGE_ANIMATED = 1; -/** - * <p> - * This {@link MapChange} is triggered whenever the currently displayed map region is changing. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int REGION_IS_CHANGING = 2; + /** + * <p> + * This {@link MapChange} is triggered whenever the currently displayed map region is changing. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int REGION_IS_CHANGING = 2; -/** - * <p> - * This {@link MapChange} is triggered whenever the currently displayed map region finished changing - * without an animation. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int REGION_DID_CHANGE = 3; + /** + * <p> + * This {@link MapChange} is triggered whenever the currently displayed map region finished changing + * without an animation. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int REGION_DID_CHANGE = 3; -/** - * <p> - * This {@link MapChange} is triggered whenever the currently displayed map region finished changing - * with an animation. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int REGION_DID_CHANGE_ANIMATED = 4; + /** + * <p> + * This {@link MapChange} is triggered whenever the currently displayed map region finished changing + * with an animation. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int REGION_DID_CHANGE_ANIMATED = 4; -/** - * <p> - * This {@link MapChange} is triggered when the map is about to start loading a new map style. - * </p> - * <p> - * This event is followed by {@link MapView#DID_FINISH_LOADING_MAP} or - * {@link MapView#DID_FAIL_LOADING_MAP}. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int WILL_START_LOADING_MAP = 5; + /** + * <p> + * This {@link MapChange} is triggered when the map is about to start loading a new map style. + * </p> + * <p> + * This event is followed by {@link MapView#DID_FINISH_LOADING_MAP} or + * {@link MapView#DID_FAIL_LOADING_MAP}. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int WILL_START_LOADING_MAP = 5; -/** - * <p> - * This {@link MapChange} is triggered when the map has successfully loaded a new map style. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FINISH_LOADING_MAP = 6; + /** + * <p> + * This {@link MapChange} is triggered when the map has successfully loaded a new map style. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FINISH_LOADING_MAP = 6; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * <p> - * This event is triggered when the map has failed to load a new map style. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FAIL_LOADING_MAP = 7; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * <p> + * This event is triggered when the map has failed to load a new map style. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FAIL_LOADING_MAP = 7; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int WILL_START_RENDERING_FRAME = 8; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int WILL_START_RENDERING_FRAME = 8; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FINISH_RENDERING_FRAME = 9; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FINISH_RENDERING_FRAME = 9; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FINISH_RENDERING_FRAME_FULLY_RENDERED = 10; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FINISH_RENDERING_FRAME_FULLY_RENDERED = 10; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int WILL_START_RENDERING_MAP = 11; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int WILL_START_RENDERING_MAP = 11; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FINISH_RENDERING_MAP = 12; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FINISH_RENDERING_MAP = 12; -/** - * <p> - * This {@link MapChange} is currently not implemented. - * </p> - * - * @see MapView.OnMapChangedListener - */ -public static final int DID_FINISH_RENDERING_MAP_FULLY_RENDERED = 13; + /** + * <p> + * This {@link MapChange} is currently not implemented. + * </p> + * + * @see MapView.OnMapChangedListener + */ + public static final int DID_FINISH_RENDERING_MAP_FULLY_RENDERED = 13; -/** - * Interface definition for a callback to be invoked when the displayed map view changes. - * - * @see MapView#addOnMapChangedListener(OnMapChangedListener) - * @see MapView.MapChange - */ -public interface OnMapChangedListener { /** - * Called when the displayed map view changes. + * Interface definition for a callback to be invoked when the displayed map view changes. * - * @param change Type of map change event, one of {@link #REGION_WILL_CHANGE}, - * {@link #REGION_WILL_CHANGE_ANIMATED}, - * {@link #REGION_IS_CHANGING}, - * {@link #REGION_DID_CHANGE}, - * {@link #REGION_DID_CHANGE_ANIMATED}, - * {@link #WILL_START_LOADING_MAP}, - * {@link #DID_FAIL_LOADING_MAP}, - * {@link #DID_FINISH_LOADING_MAP}, - * {@link #WILL_START_RENDERING_FRAME}, - * {@link #DID_FINISH_RENDERING_FRAME}, - * {@link #DID_FINISH_RENDERING_FRAME_FULLY_RENDERED}, - * {@link #WILL_START_RENDERING_MAP}, - * {@link #DID_FINISH_RENDERING_MAP}, - * {@link #DID_FINISH_RENDERING_MAP_FULLY_RENDERED}. + * @see MapView#addOnMapChangedListener(OnMapChangedListener) + * @see MapView.MapChange */ - void onMapChanged(@MapChange int change); -} + public interface OnMapChangedListener { + /** + * Called when the displayed map view changes. + * + * @param change Type of map change event, one of {@link #REGION_WILL_CHANGE}, + * {@link #REGION_WILL_CHANGE_ANIMATED}, + * {@link #REGION_IS_CHANGING}, + * {@link #REGION_DID_CHANGE}, + * {@link #REGION_DID_CHANGE_ANIMATED}, + * {@link #WILL_START_LOADING_MAP}, + * {@link #DID_FAIL_LOADING_MAP}, + * {@link #DID_FINISH_LOADING_MAP}, + * {@link #WILL_START_RENDERING_FRAME}, + * {@link #DID_FINISH_RENDERING_FRAME}, + * {@link #DID_FINISH_RENDERING_FRAME_FULLY_RENDERED}, + * {@link #WILL_START_RENDERING_MAP}, + * {@link #DID_FINISH_RENDERING_MAP}, + * {@link #DID_FINISH_RENDERING_MAP_FULLY_RENDERED}. + */ + void onMapChanged(@MapChange int change); + } } |