[android] #3276 - android view annotations adapter approach
This commit is contained in:
@@ -16,8 +16,6 @@ import com.mapbox.mapboxsdk.maps.MapView;
|
||||
*/
|
||||
public class Marker extends Annotation {
|
||||
|
||||
private MarkerView markerView;
|
||||
|
||||
private LatLng position;
|
||||
private String snippet;
|
||||
private Icon icon;
|
||||
@@ -80,10 +78,6 @@ public class Marker extends Annotation {
|
||||
if (map != null) {
|
||||
map.updateMarker(this);
|
||||
}
|
||||
|
||||
if(markerView!=null){
|
||||
markerView.setLatLng(position);
|
||||
}
|
||||
}
|
||||
|
||||
void setSnippet(String snippet) {
|
||||
@@ -147,19 +141,6 @@ public class Marker extends Annotation {
|
||||
return infoWindow;
|
||||
}
|
||||
|
||||
public MarkerView getMarkerView() {
|
||||
return markerView;
|
||||
}
|
||||
|
||||
void setMarkerView(MarkerView markerView) {
|
||||
MapboxMap map = getMapboxMap();
|
||||
if (map != null) {
|
||||
this.markerView = markerView;
|
||||
this.markerView.setLatLng(position);
|
||||
map.addMarkerView(markerView);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not use this method. Used internally by the SDK.
|
||||
*/
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.annotations;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PointF;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.Projection;
|
||||
|
||||
public class MarkerView extends FrameLayout {
|
||||
|
||||
private LatLng latLng;
|
||||
private Projection projection;
|
||||
private Marker marker;
|
||||
|
||||
private float widthOffset;
|
||||
private float heightOffset;
|
||||
|
||||
public MarkerView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public MarkerView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public MarkerView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context){
|
||||
setLayoutParams(new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
}
|
||||
|
||||
public void setProjection(Projection projection) {
|
||||
this.projection = projection;
|
||||
}
|
||||
|
||||
public void setLatLng(LatLng latLng) {
|
||||
this.latLng = latLng;
|
||||
}
|
||||
|
||||
public void setMarker(Marker marker) {
|
||||
this.marker = marker;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
widthOffset = getMeasuredWidth() / 2;
|
||||
heightOffset = getMeasuredHeight() / 2;
|
||||
|
||||
Log.v(MapboxConstants.TAG,"Measure "+widthOffset + heightOffset);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
PointF point = projection.toScreenLocation(latLng);
|
||||
setX(point.x - widthOffset);
|
||||
setY(point.y - heightOffset);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ 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;
|
||||
@@ -31,6 +32,7 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.util.LongSparseArray;
|
||||
import android.support.v4.view.GestureDetectorCompat;
|
||||
import android.support.v4.view.ScaleGestureDetectorCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
@@ -65,7 +67,6 @@ import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.InfoWindow;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.annotations.Polygon;
|
||||
import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
@@ -86,6 +87,7 @@ import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
import com.mapbox.mapboxsdk.telemetry.MapboxEvent;
|
||||
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
|
||||
import com.mapbox.mapboxsdk.utils.ColorUtils;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -109,7 +111,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
* </p>
|
||||
* <strong>Warning:</strong> Please note that you are responsible for getting permission to use the map data,
|
||||
* and for ensuring your use adheres to the relevant terms of use.
|
||||
*
|
||||
*/
|
||||
public class MapView extends FrameLayout {
|
||||
|
||||
@@ -244,6 +245,8 @@ public class MapView extends FrameLayout {
|
||||
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) {
|
||||
mMapboxMap.getUiSettings().setZoomControlsEnabled(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void setInitialState(MapboxMapOptions options) {
|
||||
@@ -446,13 +449,8 @@ public class MapView extends FrameLayout {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
} else if (change == REGION_IS_CHANGING) {
|
||||
LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds;
|
||||
long[] ids = mNativeMapView.getAnnotationsInBounds(bounds);
|
||||
Log.v(MapboxConstants.TAG, "Region is changing ane we are seeing: "+ids.length+ " point annotations");
|
||||
// for (long id : ids) {
|
||||
// Log.v(MapboxConstants.TAG, "Marker: "+id);
|
||||
// }
|
||||
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE) {
|
||||
new MarkerInBoundsTask().execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -466,6 +464,60 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
private class MarkerInBoundsTask extends AsyncTask<Void, Void, Void>{
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds;
|
||||
long[] ids = mNativeMapView.getAnnotationsInBounds(bounds);
|
||||
LongSparseArray<View> markerViews = mMapboxMap.getMarkerViews();
|
||||
|
||||
MapboxMap.MarkerViewAdapter adapter = mMapboxMap.getMarkerViewAdapter();
|
||||
|
||||
boolean found;
|
||||
long key;
|
||||
|
||||
// introduce new markers
|
||||
for (long id : ids) {
|
||||
found = false;
|
||||
for (int i = 0; i < markerViews.size(); i++) {
|
||||
key = markerViews.keyAt(i);
|
||||
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
Log.v(MapboxConstants.TAG, "Adding " + id);
|
||||
// if(adapter!=null) {
|
||||
// mMapboxMap.addMarkerView(id, adapter.getView((Marker) mMapboxMap.getAnnotation(id), null, MapView.this));
|
||||
// }
|
||||
markerViews.append(id, null);
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Already added " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// clean up out of bound markers
|
||||
for (int i = 0; i < markerViews.size(); i++) {
|
||||
found = false;
|
||||
key = markerViews.keyAt(i);
|
||||
for (long id : ids) {
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
Log.v(MapboxConstants.TAG, "Removing " + key);
|
||||
markerViews.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
Log.v(MapboxConstants.TAG, "Amount of annotations: " + markerViews.size());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* You must call this method from the parent's {@link android.app.Activity#onSaveInstanceState(Bundle)}
|
||||
* or {@link android.app.Fragment#onSaveInstanceState(Bundle)}.
|
||||
@@ -842,6 +894,11 @@ public class MapView extends FrameLayout {
|
||||
//
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* DEPRECATED @see MapboxAccountManager#start(String)
|
||||
* </p>
|
||||
* <p>
|
||||
* <p>
|
||||
* Sets the current Mapbox access token used to load map styles and tiles.
|
||||
* <p>
|
||||
* You must set a valid access token before you call {@link MapView#onCreate(Bundle)}
|
||||
@@ -867,8 +924,13 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* DEPRECATED @see MapboxAccountManager#getAccessToken()
|
||||
* </p>
|
||||
* <p>
|
||||
* Returns the current Mapbox access token used to load map styles and tiles.
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @return The current Mapbox access token.
|
||||
* @deprecated As of release 4.1.0, replaced by {@link MapboxAccountManager#getAccessToken()}
|
||||
*/
|
||||
@@ -1282,6 +1344,10 @@ public class MapView extends FrameLayout {
|
||||
private class SurfaceTextureListener implements TextureView.SurfaceTextureListener {
|
||||
|
||||
private Surface mSurface;
|
||||
private View mViewHolder;
|
||||
|
||||
private static final int VIEW_MARKERS_POOL_SIZE = 20;
|
||||
|
||||
|
||||
// Called when the native surface texture has been created
|
||||
// Must do all EGL/GL ES initialization here
|
||||
@@ -1289,7 +1355,6 @@ public class MapView extends FrameLayout {
|
||||
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
||||
mNativeMapView.createSurface(mSurface = new Surface(surface));
|
||||
mNativeMapView.resizeFramebuffer(width, height);
|
||||
|
||||
mHasSurface = true;
|
||||
}
|
||||
|
||||
@@ -1330,8 +1395,15 @@ public class MapView extends FrameLayout {
|
||||
mCompassView.update(getDirection());
|
||||
mMyLocationView.update();
|
||||
|
||||
for (MarkerView view : mMapboxMap.getMarkerViews()) {
|
||||
view.update();
|
||||
LongSparseArray<View> viewMarkers = mMapboxMap.getMarkerViews();
|
||||
for (int i = 0; i < viewMarkers.size(); i++) {
|
||||
mViewHolder = viewMarkers.valueAt(i);
|
||||
if (mViewHolder != null) {
|
||||
Marker marker = (Marker) mMapboxMap.getAnnotation(viewMarkers.keyAt(i));
|
||||
PointF point = mMapboxMap.getProjection().toScreenLocation(marker.getPosition());
|
||||
mViewHolder.setX(point.x - (mViewHolder.getMeasuredWidth() / 2));
|
||||
mViewHolder.setY(point.y - (mViewHolder.getMeasuredHeight() / 2));
|
||||
}
|
||||
}
|
||||
|
||||
for (InfoWindow infoWindow : mMapboxMap.getInfoWindows()) {
|
||||
@@ -2667,7 +2739,7 @@ public class MapView extends FrameLayout {
|
||||
private String mStyle;
|
||||
private boolean mDefaultStyle;
|
||||
|
||||
StyleInitializer(@NonNull Context context) {
|
||||
StyleInitializer(@NonNull Context context) {
|
||||
mStyle = Style.getMapboxStreetsUrl(context.getResources().getInteger(R.integer.style_version));
|
||||
mDefaultStyle = true;
|
||||
}
|
||||
|
||||
@@ -14,13 +14,15 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapbox.mapboxsdk.MapboxAccountManager;
|
||||
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Annotation;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.InfoWindow;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.annotations.Polygon;
|
||||
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
@@ -36,6 +38,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.layers.CustomLayer;
|
||||
import com.mapbox.mapboxsdk.location.LocationListener;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -61,7 +64,7 @@ public class MapboxMap {
|
||||
private LongSparseArray<Annotation> mAnnotations;
|
||||
|
||||
private List<Marker> mSelectedMarkers;
|
||||
private List<MarkerView> mMarkerViews;
|
||||
private LongSparseArray<View> mMarkerViews;
|
||||
private List<InfoWindow> mInfoWindows;
|
||||
|
||||
private MapboxMap.InfoWindowAdapter mInfoWindowAdapter;
|
||||
@@ -93,8 +96,8 @@ public class MapboxMap {
|
||||
mTrackingSettings = new TrackingSettings(mMapView, mUiSettings);
|
||||
mProjection = new Projection(mapView);
|
||||
mAnnotations = new LongSparseArray<>();
|
||||
mMarkerViews = new LongSparseArray<>();
|
||||
mSelectedMarkers = new ArrayList<>();
|
||||
mMarkerViews = new ArrayList<>();
|
||||
mInfoWindows = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -590,8 +593,12 @@ public class MapboxMap {
|
||||
//
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* DEPRECATED @see MapboxAccountManager#start(String)
|
||||
* </p>
|
||||
* <p>
|
||||
* Sets the current Mapbox access token used to load map styles and tiles.
|
||||
*
|
||||
* </p>
|
||||
* @param accessToken Your public Mapbox access token.
|
||||
* @see MapView#setAccessToken(String)
|
||||
* @deprecated As of release 4.1.0, replaced by {@link com.mapbox.mapboxsdk.MapboxAccountManager#start(Context, String)}
|
||||
@@ -603,7 +610,12 @@ public class MapboxMap {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* DEPRECATED @see MapboxAccountManager#getAccessToken()
|
||||
* </p>
|
||||
* <p>
|
||||
* Returns the current Mapbox access token used to load map styles and tiles.
|
||||
* </p>
|
||||
*
|
||||
* @return The current Mapbox access token.
|
||||
* @deprecated As of release 4.1.0, replaced by {@link MapboxAccountManager#getAccessToken()}
|
||||
@@ -619,9 +631,8 @@ public class MapboxMap {
|
||||
// Annotations
|
||||
//
|
||||
|
||||
public void addMarkerView(MarkerView markerView){
|
||||
markerView.setProjection(mProjection);
|
||||
mMarkerViews.add(markerView);
|
||||
public void addMarkerView(long key, View markerView) {
|
||||
mMarkerViews.append(key, markerView);
|
||||
mMapView.addView(markerView);
|
||||
}
|
||||
|
||||
@@ -655,14 +666,6 @@ public class MapboxMap {
|
||||
@NonNull
|
||||
public Marker addMarker(@NonNull BaseMarkerOptions markerOptions) {
|
||||
Marker marker = prepareMarker(markerOptions);
|
||||
|
||||
if(mMarkerViewAdapter!=null){
|
||||
MarkerView view = mMarkerViewAdapter.getView(marker);
|
||||
if(view!=null) {
|
||||
mMarkerViews.add(view);
|
||||
}
|
||||
}
|
||||
|
||||
long id = mMapView.addMarker(marker);
|
||||
marker.setMapboxMap(this);
|
||||
marker.setId(id);
|
||||
@@ -682,11 +685,11 @@ public class MapboxMap {
|
||||
*/
|
||||
@UiThread
|
||||
@NonNull
|
||||
public List<Marker> addMarkers(@NonNull List<MarkerOptions> markerOptionsList) {
|
||||
public List<Marker> addMarkers(@NonNull List<BaseMarkerOptions> markerOptionsList) {
|
||||
int count = markerOptionsList.size();
|
||||
List<Marker> markers = new ArrayList<>(count);
|
||||
if (count > 0) {
|
||||
MarkerOptions markerOptions;
|
||||
BaseMarkerOptions markerOptions;
|
||||
Marker marker;
|
||||
for (int i = 0; i < count; i++) {
|
||||
markerOptions = markerOptionsList.get(i);
|
||||
@@ -1021,18 +1024,6 @@ public class MapboxMap {
|
||||
return markers;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public MarkerView getMarkerView(long id) {
|
||||
MarkerView markerView = null;
|
||||
List<Marker> markers = getMarkers();
|
||||
for (Marker m : markers) {
|
||||
if (m.getId() == id) {
|
||||
markerView = m.getMarkerView();
|
||||
}
|
||||
}
|
||||
return markerView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all the polygons on the map.
|
||||
*
|
||||
@@ -1165,10 +1156,14 @@ public class MapboxMap {
|
||||
return marker;
|
||||
}
|
||||
|
||||
public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter){
|
||||
public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) {
|
||||
mMarkerViewAdapter = markerViewAdapter;
|
||||
}
|
||||
|
||||
public MarkerViewAdapter getMarkerViewAdapter() {
|
||||
return mMarkerViewAdapter;
|
||||
}
|
||||
|
||||
//
|
||||
// InfoWindow
|
||||
//
|
||||
@@ -1225,7 +1220,7 @@ public class MapboxMap {
|
||||
}
|
||||
|
||||
// used by MapView
|
||||
List<MarkerView> getMarkerViews(){
|
||||
LongSparseArray<View> getMarkerViews() {
|
||||
return mMarkerViews;
|
||||
}
|
||||
|
||||
@@ -1240,12 +1235,13 @@ public class MapboxMap {
|
||||
/**
|
||||
* Sets the distance from the edges of the map view’s frame to the edges of the map
|
||||
* view’s logical viewport.
|
||||
*
|
||||
* <p>
|
||||
* When the value of this property is equal to {0,0,0,0}, viewport
|
||||
* properties such as `centerCoordinate` assume a viewport that matches the map
|
||||
* view’s frame. Otherwise, those properties are inset, excluding part of the
|
||||
* frame from the viewport. For instance, if the only the top edge is inset, the
|
||||
* map center is effectively shifted downward.
|
||||
* </p>
|
||||
*
|
||||
* @param left The left margin in pixels.
|
||||
* @param top The top margin in pixels.
|
||||
@@ -1756,10 +1752,10 @@ public class MapboxMap {
|
||||
View getInfoWindow(@NonNull Marker marker);
|
||||
}
|
||||
|
||||
public interface MarkerViewAdapter {
|
||||
public interface MarkerViewAdapter<U extends Marker> {
|
||||
|
||||
@Nullable
|
||||
MarkerView getView(@NonNull Marker marker);
|
||||
View getView(@NonNull U marker, @Nullable View convertView, @NonNull ViewGroup parent);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user