[android] - getMarkerViews in rect should return in bound views (#7015)

* [android] - getMarkerViews in rectangle should only return in bound views.

* added example in test app for debugging

* Covert rectangle coming in to density dependant rectangle
This commit is contained in:
Tobrun
2016-11-14 12:08:03 +01:00
committed by GitHub
parent fa6d586417
commit f66ea0bef9
6 changed files with 193 additions and 7 deletions

View File

@@ -329,7 +329,14 @@ class AnnotationManager {
return selectedMarkers;
}
public List<Marker> getMarkersInRect(@NonNull RectF rect) {
public List<Marker> getMarkersInRect(@NonNull RectF rectangle) {
// convert Rectangle to be density depedent
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
@@ -350,7 +357,13 @@ class AnnotationManager {
return new ArrayList<>(annotations);
}
public List<MarkerView> getMarkerViewsInRect(@NonNull RectF rect) {
public List<MarkerView> getMarkerViewsInRect(@NonNull RectF rectangle) {
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
@@ -363,7 +376,7 @@ class AnnotationManager {
int count = annotationList.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
if (annotation instanceof MarkerView) {
if (annotation instanceof MarkerView && idsList.contains(annotation.getId())) {
annotations.add((MarkerView) annotation);
}
}

View File

@@ -1572,10 +1572,10 @@ public class MapView extends FrameLayout {
float toleranceSides = 4 * screenDensity;
float toleranceTopBottom = 10 * screenDensity;
RectF tapRect = new RectF((tapPoint.x - iconManager.getAverageIconWidth() / 2 - toleranceSides) / screenDensity,
(tapPoint.y - iconManager.getAverageIconHeight() / 2 - toleranceTopBottom) / screenDensity,
(tapPoint.x + iconManager.getAverageIconWidth() / 2 + toleranceSides) / screenDensity,
(tapPoint.y + iconManager.getAverageIconHeight() / 2 + toleranceTopBottom) / screenDensity);
RectF tapRect = new RectF(tapPoint.x - iconManager.getAverageIconWidth() / 2 - toleranceSides,
tapPoint.y - iconManager.getAverageIconHeight() / 2 - toleranceTopBottom,
tapPoint.x + iconManager.getAverageIconWidth() / 2 + toleranceSides,
tapPoint.y + iconManager.getAverageIconHeight() / 2 + toleranceTopBottom);
List<Marker> nearbyMarkers = annotationManager.getMarkersInRect(tapRect);
long newSelectedMarkerId = -1;

View File

@@ -453,6 +453,14 @@
android:name="@string/category"
android:value="@string/category_maplayout" />
</activity>
<activity
android:name=".activity.annotation.MarkerViewsInRectangleActivity"
android:description="@string/description_marker_view_rectangle"
android:label="@string/activity_marker_view_rectangle">
<meta-data
android:name="@string/category"
android:value="@string/category_annotation" />
</activity>
<!-- For Instrumentation tests -->
<activity android:name=".activity.style.RuntimeStyleTestActivity" />

View File

@@ -0,0 +1,135 @@
package com.mapbox.mapboxsdk.testapp.activity.annotation;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.feature.QueryRenderedFeaturesBoxCountActivity;
import java.util.List;
public class MarkerViewsInRectangleActivity extends AppCompatActivity implements OnMapReadyCallback,
View.OnClickListener {
private static final String TAG = QueryRenderedFeaturesBoxCountActivity.class.getSimpleName();
public MapView mapView;
private MapboxMap mapboxMap;
private View selectionBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker_view_in_rect);
setupActionBar();
selectionBox = findViewById(R.id.selection_box);
//Initialize map as normal
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
MarkerViewsInRectangleActivity.this.mapboxMap = mapboxMap;
selectionBox.setOnClickListener(this);
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.0907, 5.1214), 16));
mapboxMap.addMarker(new MarkerViewOptions().position(new LatLng(52.0907, 5.1214)));
}
@Override
public void onClick(View view) {
//Query
int top = selectionBox.getTop() - mapView.getTop();
int left = selectionBox.getLeft() - mapView.getLeft();
RectF box = new RectF(left, top, left + selectionBox.getWidth(), top + selectionBox.getHeight());
Log.i(TAG, String.format("Querying box %s", box));
List<MarkerView> markers = mapboxMap.getMarkerViewsInRect(box);
//Show count
Toast.makeText(
MarkerViewsInRectangleActivity.this,
String.format("%s markers inside box", markers.size()),
Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupActionBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
<FrameLayout
android:id="@+id/selection_box"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:alpha="0.3"
android:background="#1d72da"/>
</RelativeLayout>

View File

@@ -73,6 +73,7 @@
<string name="activity_navigation_drawer">Android SDK View integration</string>
<string name="activity_video_view">Video View</string>
<string name="activity_map_in_dialog">Dialog with map</string>
<string name="activity_marker_view_rectangle">Marker views in rectangle</string>
<!-- Description -->
<string name="description_user_location_tracking">Tracks the location of the user</string>
@@ -130,6 +131,7 @@
<string name="description_add_remove_markers">Based on zoom level</string>
<string name="description_style_file">Use a local file as the style</string>
<string name="description_map_in_dialog">Display a map inside a dialog fragment</string>
<string name="description_marker_view_rectangle">Marker Views within a rectangle</string>
<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>
<string name="menuitem_title_deselect_markers_on_tap">Deselect Markers On Tap</string>