From 8f1a4262a75ef8ec0cb6a9d52c70a9e9aca719b4 Mon Sep 17 00:00:00 2001 From: Tobrun Van Nuland Date: Tue, 19 Jul 2016 14:01:03 +0200 Subject: [PATCH] [android] #352 - introduced addAnnotation and addAnnotations methods in MapboxMap. Exposed constructors of Marker, Polyline and Polygon. --- .../mapboxsdk/annotations/Annotation.java | 9 ++ .../annotations/AnnotationDefinition.java | 5 -- .../mapbox/mapboxsdk/annotations/Feature.java | 7 +- .../mapbox/mapboxsdk/annotations/Polygon.java | 2 +- .../mapboxsdk/annotations/Polyline.java | 2 +- .../mapbox/mapboxsdk/annotations/Shape.java | 6 +- .../mapboxsdk/annotations/package-info.java | 2 +- .../com/mapbox/mapboxsdk/maps/MapView.java | 1 + .../com/mapbox/mapboxsdk/maps/MapboxMap.java | 90 ++++++++++++++----- 9 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Annotation.java delete mode 100644 platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/AnnotationDefinition.java diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Annotation.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Annotation.java new file mode 100644 index 00000000000..7563551e39e --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Annotation.java @@ -0,0 +1,9 @@ +package com.mapbox.mapboxsdk.annotations; + +public interface Annotation { + + long getId(); + + void setId(long id); + +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/AnnotationDefinition.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/AnnotationDefinition.java deleted file mode 100644 index aac7f71dba6..00000000000 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/AnnotationDefinition.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mapbox.mapboxsdk.annotations; - -public interface AnnotationDefinition { - -} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Feature.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Feature.java index 994be34ea63..935d85bbb58 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Feature.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Feature.java @@ -15,11 +15,11 @@ * * @link com.mapbox.mapboxsdk.maps.MapboxMap#getVisibleFeatures(PointF)} * and related methods.Each feature object associates a shape with an identifier and - * attributes as specified by the source.Like ordinary AnnotationDefinition objects,some kinds of `Feature` + * attributes as specified by the source.Like ordinary Annotation objects,some kinds of `Feature` * objects can also be added to a map view using `-[MGLMapView addAnnotations:]` * and related methods. */ -public interface Feature extends AnnotationDefinition { +public interface Feature extends Annotation { /** * A long that uniquely identifies the feature in its containing @@ -67,4 +67,5 @@ public interface Feature extends AnnotationDefinition { * @param key the key associated to the attribute * @return the */ - Object getAttribute(String key); \ No newline at end of file + Object getAttribute(String key); +} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java index e4f7b5a94ca..109159dca21 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java @@ -15,7 +15,7 @@ public final class Polygon extends PointCollectionShape { private int fillColor = Color.BLACK; // default fillColor is black private int strokeColor = Color.BLACK; // default strokeColor is black - Polygon() { + public Polygon() { super(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java index d7a8437a4c4..aceff82156f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polyline.java @@ -10,7 +10,7 @@ public final class Polyline extends PointCollectionShape { private int color = Color.BLACK; // default color is black private float width = 10; // As specified by Google API Docs (in pixels) - Polyline() { + public Polyline() { super(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Shape.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Shape.java index 4f5c363ccf8..3f99fe01198 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Shape.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Shape.java @@ -6,14 +6,14 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; /** - * AnnotationDefinition is an overlay on top of a {@link MapView}, + * Annotation is an overlay on top of a {@link MapView}, * from which {@link Polygon}, {@link Polyline} and {@link Marker} are derived. *

* it manages attachment to a map and identification, but does not require * content to be placed at a geographical point. *

*/ -public abstract class Shape implements AnnotationDefinition, Comparable { +public abstract class Shape implements Annotation, Comparable { /** *

@@ -35,6 +35,7 @@ protected Shape() { * This ID is unique for a MapView instance and is suitable for associating your own extra * data with. */ + @Override public long getId() { return id; } @@ -49,6 +50,7 @@ public void remove() { /** * Do not use this method. Used internally by the SDK. */ + @Override public void setId(long id) { this.id = id; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/package-info.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/package-info.java index 2f0a621cb6c..1e2dc542fa8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/package-info.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/package-info.java @@ -1,4 +1,4 @@ /** - * Contains the Mapbox Maps Android AnnotationDefinition API classes. + * Contains the Mapbox Maps Android Annotation API classes. */ package com.mapbox.mapboxsdk.annotations; 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 b95c7dbdd79..57363437d57 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 @@ -61,6 +61,7 @@ import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; import com.mapbox.mapboxsdk.MapboxAccountManager; import com.mapbox.mapboxsdk.R; +import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.Shape; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; 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 6702c63f2dc..b16594ca470 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 @@ -17,7 +17,7 @@ import android.view.ViewGroup; import com.mapbox.mapboxsdk.MapboxAccountManager; -import com.mapbox.mapboxsdk.annotations.AnnotationDefinition; +import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.Feature; import com.mapbox.mapboxsdk.annotations.Shape; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; @@ -652,13 +652,66 @@ void setTilt(double tilt) { mMapView.setTilt(tilt); } + /** + * Adds an annotation to the map view. + *

+ * {@link com.mapbox.mapboxsdk.annotations.MultiPolyline}, {@link com.mapbox.mapboxsdk.annotations.MultiPolygon} + * and {@link com.mapbox.mapboxsdk.annotations.ShapeCollection} objects cannot be added to the map view at this time. + * Nor can {@link Shape} objects that are not instances of {@link Polyline} or {@link Polygon) or {@link Marker}. Any + * multipoint, multipolyline, multipolygon, or shape collection object that is specified is silently ignored. + *

+ * + * @param annotation The annotation object to add to the receiver. This object + * must conform to the `Annotation` protocol. The map view retains the + * annotation object. + */ + public Annotation addAnnotation(@NonNull Annotation annotation) { + + // validate if annotation is already added + if (annotation.getId() != -1) { + removeAnnotation(annotation); + } + + if (annotation instanceof Marker) { + long id = mMapView.addMarker((Marker) annotation); + annotation.setId(id); + } else if (annotation instanceof Polygon) { + long id = mMapView.addPolygon((Polygon) annotation); + annotation.setId(id); + } else if (annotation instanceof Polyline) { + long id = mMapView.addPolyline((Polyline) annotation); + annotation.setId(id); + } else { + Log.v(MapboxConstants.TAG, "Unsupported annotation type for addAnnotation"); + } + return annotation; + } /** + * Adds an array of annotations to the map view. *

- * Adds a marker to this map. + * {@link com.mapbox.mapboxsdk.annotations.MultiPolyline}, {@link com.mapbox.mapboxsdk.annotations.MultiPolygon} + * and {@link com.mapbox.mapboxsdk.annotations.ShapeCollection} objects cannot be added to the map view at this time. + * Nor can {@link Shape} objects that are not instances of {@link Polyline} or {@link Polygon) or {@link Marker}. Any + * multipoint, multipolyline, multipolygon, or shape collection object that is specified is silently ignored. *

+ * + * @param annotations An List of annotation objects. Each object in the list + * must conform to the `Annotation` protocol. The map view retains each + * individual annotation object. + */ + public void addAnnotations(List annotations) { + for (Annotation a : annotations) { + addAnnotation(a); + } + } + + /** + * Adds a marker to this map. + *

* The marker's icon is rendered on the map at the location {@code Marker.position}. * If {@code Marker.title} is defined, the map shows an info box with the marker's title and snippet. + *

* * @param markerOptions A marker options object that defines how to render the marker. * @return The {@code Marker} that was added to the map. @@ -670,11 +723,11 @@ public Marker addMarker(@NonNull MarkerOptions markerOptions) { } /** - *

* Adds a marker to this map. - *

+ *

* The marker's icon is rendered on the map at the location {@code Marker.position}. * If {@code Marker.title} is defined, the map shows an info box with the marker's title and snippet. + *

* * @param markerOptions A marker options object that defines how to render the marker. * @return The {@code Marker} that was added to the map. @@ -778,10 +831,6 @@ public void updateMarker(@NonNull Marker updatedMarker) { } } - public void addAnnotations(Listannotations){ - // TODO implementation - } - /** * Adds a polyline to this map. * @@ -953,17 +1002,19 @@ public void removePolygon(@NonNull Polygon polygon) { * @param annotation The annotation object to remove. */ @UiThread - public void removeAnnotation(@NonNull Shape annotation) { - if (annotation instanceof Marker) { - Marker marker = (Marker) annotation; - marker.hideInfoWindow(); - if (marker instanceof MarkerView) { - mMarkerViewManager.removeMarkerView((MarkerView) marker); + public void removeAnnotation(Annotation annotation) { + if (annotation != null) { + if (annotation instanceof Marker) { + Marker marker = (Marker) annotation; + marker.hideInfoWindow(); + if (marker instanceof MarkerView) { + mMarkerViewManager.removeMarkerView((MarkerView) marker); + } } + long id = annotation.getId(); + mMapView.removeAnnotation(id); + mAnnotations.remove(id); } - long id = annotation.getId(); - mMapView.removeAnnotation(id); - mAnnotations.remove(id); } /** @@ -973,8 +1024,7 @@ public void removeAnnotation(@NonNull Shape annotation) { */ @UiThread public void removeAnnotation(long id) { - mMapView.removeAnnotation(id); - mAnnotations.remove(id); + removeAnnotation(getAnnotation(id)); } /** @@ -1668,7 +1718,7 @@ public void snapshot(@NonNull SnapshotReadyCallback callback) { // featuresAt // - public List getVisibleFeatures(PointF pointF){ + public List getVisibleFeatures(PointF pointF) { return new ArrayList<>(); }