[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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,6 +15,7 @@ import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
@@ -121,7 +122,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
}
|
||||
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Integer, List<MarkerOptions>> {
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
|
||||
|
||||
private static final String TAG = "LoadBulkMarkerTask";
|
||||
private MapboxMap mMapboxMap;
|
||||
@@ -138,8 +139,8 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<MarkerOptions> doInBackground(Void... params) {
|
||||
List<MarkerOptions> markerOptions = new ArrayList<>(mAmount);
|
||||
protected List<BaseMarkerOptions> doInBackground(Void... params) {
|
||||
List<BaseMarkerOptions> markerOptions = new ArrayList<>(mAmount);
|
||||
try {
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
String json = GeoParseUtil.loadStringFromAssets(mAppContext, "points.geojson");
|
||||
@@ -170,7 +171,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<MarkerOptions> markerOptions) {
|
||||
protected void onPostExecute(List<BaseMarkerOptions> markerOptions) {
|
||||
super.onPostExecute(markerOptions);
|
||||
mMapboxMap.addMarkers(markerOptions);
|
||||
mProgressDialog.hide();
|
||||
|
||||
@@ -1,29 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.annotation;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
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.view.CustomMarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarker;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerOptions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyCallback {
|
||||
|
||||
private MapView mMapView;
|
||||
private Marker brazil, germany, china, us;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -46,37 +51,46 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
|
||||
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
List<BaseMarkerOptions> countries = new ArrayList<>();
|
||||
countries.add(new CountryMarkerOptions().abbrevName("ch").flagRes(R.drawable.ic_china).position(new LatLng(31.230416, 121.473701)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("us").flagRes(R.drawable.ic_us).position(new LatLng(38.907192, -77.036871)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("br").flagRes(R.drawable.ic_brazil).position(new LatLng(-15.798200, -47.922363)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("de").flagRes(R.drawable.ic_germany).position(new LatLng(52.520007, 13.404954)));
|
||||
mapboxMap.addMarkers(countries);
|
||||
|
||||
// adapt GL-marker to View-marker
|
||||
mapboxMap.setMarkerViewAdapter(new MapboxMap.MarkerViewAdapter() {
|
||||
@Nullable
|
||||
@Override
|
||||
public MarkerView getView(@NonNull Marker marker) {
|
||||
if (marker.equals(brazil)) {
|
||||
return createMarkerView("br", R.drawable.ic_brazil);
|
||||
} else if (marker.equals(germany)) {
|
||||
return createMarkerView("de", R.drawable.ic_germany);
|
||||
} else if (marker.equals(china)) {
|
||||
return createMarkerView("ch", R.drawable.ic_china);
|
||||
} else if (marker.equals(us)) {
|
||||
return createMarkerView("us", R.drawable.ic_us);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// add markers
|
||||
china = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(31.230416, 121.473701)));
|
||||
brazil = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-15.798200, -47.922363)));
|
||||
us = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(38.907192, -77.036871)));
|
||||
germany = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(52.520007, 13.404954)));
|
||||
mapboxMap.setMarkerViewAdapter(new CountryAdapter(this));
|
||||
}
|
||||
|
||||
private MarkerView createMarkerView(String countryAbbrev, @DrawableRes int countryIconRes) {
|
||||
CustomMarkerView customMarkerView = new CustomMarkerView(this);
|
||||
customMarkerView.setText(countryAbbrev);
|
||||
customMarkerView.setImage(countryIconRes);
|
||||
return customMarkerView;
|
||||
private static class CountryAdapter implements MapboxMap.MarkerViewAdapter<CountryMarker> {
|
||||
|
||||
private LayoutInflater inflater;
|
||||
|
||||
public CountryAdapter(@NonNull Context context) {
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View getView(@NonNull CountryMarker marker, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
ViewHolder viewHolder;
|
||||
if (convertView == null) {
|
||||
viewHolder = new ViewHolder();
|
||||
convertView = inflater.inflate(R.layout.view_custom_marker, parent, false);
|
||||
viewHolder.flag = (ImageView) convertView.findViewById(R.id.imageView);
|
||||
viewHolder.abbrev = (TextView) convertView.findViewById(R.id.textView);
|
||||
convertView.setTag(viewHolder);
|
||||
} else {
|
||||
viewHolder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
viewHolder.flag.setImageResource(marker.getFlagRes());
|
||||
viewHolder.abbrev.setText(marker.getAbbrevName());
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
ImageView flag;
|
||||
TextView abbrev;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,5 +133,4 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
|
||||
super.onLowMemory();
|
||||
mMapView.onLowMemory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mapbox.mapboxsdk.testapp.model.annotations;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
|
||||
public class CountryMarker extends Marker {
|
||||
|
||||
private String abbrevName;
|
||||
private int flagRes;
|
||||
|
||||
public CountryMarker(BaseMarkerOptions baseMarkerOptions, String abbrevName, int iconRes) {
|
||||
super(baseMarkerOptions);
|
||||
this.abbrevName = abbrevName;
|
||||
this.flagRes = iconRes;
|
||||
}
|
||||
|
||||
public String getAbbrevName() {
|
||||
return abbrevName;
|
||||
}
|
||||
|
||||
public int getFlagRes() {
|
||||
return flagRes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.mapbox.mapboxsdk.testapp.model.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
public class CountryMarkerOptions extends BaseMarkerOptions<CountryMarker, CountryMarkerOptions> {
|
||||
|
||||
private String abbrevName;
|
||||
private int flagRes;
|
||||
|
||||
public CountryMarkerOptions abbrevName(String name) {
|
||||
abbrevName = name;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CountryMarkerOptions flagRes(int imageRes) {
|
||||
flagRes = imageRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CountryMarkerOptions() {
|
||||
}
|
||||
|
||||
private CountryMarkerOptions(Parcel in) {
|
||||
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
|
||||
snippet(in.readString());
|
||||
String iconId = in.readString();
|
||||
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
|
||||
Icon icon = IconFactory.recreate(iconId, iconBitmap);
|
||||
icon(icon);
|
||||
title(in.readString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountryMarkerOptions getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountryMarker getMarker() {
|
||||
return new CountryMarker(this, abbrevName, flagRes);
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<CountryMarkerOptions> CREATOR
|
||||
= new Parcelable.Creator<CountryMarkerOptions>() {
|
||||
public CountryMarkerOptions createFromParcel(Parcel in) {
|
||||
return new CountryMarkerOptions(in);
|
||||
}
|
||||
|
||||
public CountryMarkerOptions[] newArray(int size) {
|
||||
return new CountryMarkerOptions[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
out.writeParcelable(position, flags);
|
||||
out.writeString(snippet);
|
||||
out.writeString(icon.getId());
|
||||
out.writeParcelable(icon.getBitmap(), flags);
|
||||
out.writeString(title);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.testapp.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.LayoutInflater;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
public class CustomMarkerView extends MarkerView {
|
||||
|
||||
private TextView textView;
|
||||
private ImageView imageView;
|
||||
|
||||
public CustomMarkerView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
LayoutInflater.from(context).inflate(R.layout.view_custom_marker, this);
|
||||
textView = (TextView) findViewById(R.id.textView);
|
||||
imageView = (ImageView) findViewById(R.id.imageView);
|
||||
}
|
||||
|
||||
public void setText(@NonNull String text) {
|
||||
textView.setText(text);
|
||||
}
|
||||
|
||||
public void setImage(@DrawableRes int drawableRes) {
|
||||
imageView.setImageResource(drawableRes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user