diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java index 8a6ff519a..9b7e7eb1a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java @@ -16,6 +16,8 @@ import com.mapbox.mapboxsdk.maps.MapView; */ public class Marker extends Annotation { + private MarkerView markerView; + private LatLng position; private String snippet; private Icon icon; @@ -78,6 +80,10 @@ public class Marker extends Annotation { if (map != null) { map.updateMarker(this); } + + if(markerView!=null){ + markerView.setLatLng(position); + } } void setSnippet(String snippet) { @@ -141,6 +147,19 @@ 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. */ diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java new file mode 100644 index 000000000..3e9d12034 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java @@ -0,0 +1,69 @@ +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); + } +} 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 be4819064..960b478f9 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 @@ -65,6 +65,7 @@ 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; @@ -1319,6 +1320,11 @@ public class MapView extends FrameLayout { mCompassView.update(getDirection()); mMyLocationView.update(); + + for (MarkerView view : mMapboxMap.getMarkerViews()) { + view.update(); + } + for (InfoWindow infoWindow : mMapboxMap.getInfoWindows()) { infoWindow.update(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 0e763a998..b12371b3a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -20,6 +20,7 @@ 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; @@ -58,9 +59,13 @@ public class MapboxMap { private CameraPosition mCameraPosition; private boolean mInvalidCameraPosition; private LongSparseArray mAnnotations; + private List mSelectedMarkers; + private List mMarkerViews; private List mInfoWindows; + private MapboxMap.InfoWindowAdapter mInfoWindowAdapter; + private MapboxMap.MarkerViewAdapter mMarkerViewAdapter; private boolean mMyLocationEnabled; private boolean mAllowConcurrentMultipleInfoWindows; @@ -89,6 +94,7 @@ public class MapboxMap { mProjection = new Projection(mapView); mAnnotations = new LongSparseArray<>(); mSelectedMarkers = new ArrayList<>(); + mMarkerViews = new ArrayList<>(); mInfoWindows = new ArrayList<>(); } @@ -613,6 +619,12 @@ public class MapboxMap { // Annotations // + public void addMarkerView(MarkerView markerView){ + markerView.setProjection(mProjection); + mMarkerViews.add(markerView); + mMapView.addView(markerView); + } + /** *

* Adds a marker to this map. @@ -643,6 +655,14 @@ 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); @@ -1001,6 +1021,18 @@ public class MapboxMap { return markers; } + @Nullable + public MarkerView getMarkerView(long id) { + MarkerView markerView = null; + List 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. * @@ -1133,6 +1165,10 @@ public class MapboxMap { return marker; } + public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter){ + mMarkerViewAdapter = markerViewAdapter; + } + // // InfoWindow // @@ -1188,6 +1224,11 @@ public class MapboxMap { return mInfoWindows; } + // used by MapView + List getMarkerViews(){ + return mMarkerViews; + } + private boolean isInfoWindowValidForMarker(@NonNull Marker marker) { return !TextUtils.isEmpty(marker.getTitle()) || !TextUtils.isEmpty(marker.getSnippet()); } @@ -1715,6 +1756,12 @@ public class MapboxMap { View getInfoWindow(@NonNull Marker marker); } + public interface MarkerViewAdapter { + + @Nullable + MarkerView getView(@NonNull Marker marker); + } + /** * Interface definition for a callback to be invoked when the the My Location view changes location. * diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 541164384..d31915f21 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -253,6 +253,16 @@ android:value="@string/category_maplayout" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml new file mode 100644 index 000000000..08caf1df6 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml @@ -0,0 +1,19 @@ + + + + + + + + \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml index 9b316d0c3..120bf49fc 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml @@ -4,6 +4,7 @@ + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 835b1479f..840427af0 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -17,7 +17,8 @@ Polyline Polygon Press Map For Marker - + x + Standard InfoWindow example Custom InfoWindow Adapter @@ -76,6 +77,7 @@ Scroll with pixels in x,y direction Example to make a snapshot of the map 2 maps in a view hierarchy + x Concurrent Open InfoWindowsr Dismiss location tracking on gesture