[android] #3276 - WIP Android View Annotations, showing an Android SDK view on the screen

This commit is contained in:
Tobrun
2016-04-21 10:50:01 +02:00
parent ef257c257d
commit 028ef7a323
15 changed files with 391 additions and 1 deletions

View File

@@ -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.
*/

View File

@@ -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);
}
}

View File

@@ -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();
}

View File

@@ -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<Annotation> mAnnotations;
private List<Marker> mSelectedMarkers;
private List<MarkerView> mMarkerViews;
private List<InfoWindow> 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);
}
/**
* <p>
* 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<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.
*
@@ -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<MarkerView> 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.
*