Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #352 - introduced addAnnotation and addAnnotations methods …
Browse files Browse the repository at this point in the history
…in MapboxMap. Exposed constructors of Marker, Polyline and Polygon.
  • Loading branch information
tobrun committed Jul 26, 2016
1 parent ddc9245 commit 8f1a426
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mapbox.mapboxsdk.annotations;

public interface Annotation {

long getId();

void setId(long id);

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,4 +67,5 @@ public interface Feature extends AnnotationDefinition {
* @param key the key associated to the attribute
* @return the
*/
Object getAttribute(String key);
Object getAttribute(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* it manages attachment to a map and identification, but does not require
* content to be placed at a geographical point.
* </p>
*/
public abstract class Shape implements AnnotationDefinition, Comparable<Shape> {
public abstract class Shape implements Annotation, Comparable<Shape> {

/**
* <p>
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -652,13 +652,66 @@ void setTilt(double tilt) {
mMapView.setTilt(tilt);
}

/**
* Adds an annotation to the map view.
* <p>
* {@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.
* </p>
*
* @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.
* <p>
* 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.
* </p>
*
* @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<Annotation> annotations) {
for (Annotation a : annotations) {
addAnnotation(a);
}
}

/**
* Adds a marker to this map.
* <p>
* 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.
* </p>
*
* @param markerOptions A marker options object that defines how to render the marker.
* @return The {@code Marker} that was added to the map.
Expand All @@ -670,11 +723,11 @@ public Marker addMarker(@NonNull MarkerOptions markerOptions) {
}

/**
* <p>
* Adds a marker to this map.
* </p>
* <p>
* 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.
* </p>
*
* @param markerOptions A marker options object that defines how to render the marker.
* @return The {@code Marker} that was added to the map.
Expand Down Expand Up @@ -778,10 +831,6 @@ public void updateMarker(@NonNull Marker updatedMarker) {
}
}

public void addAnnotations(List<AnnotationDefinition>annotations){
// TODO implementation
}

/**
* Adds a polyline to this map.
*
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -1668,7 +1718,7 @@ public void snapshot(@NonNull SnapshotReadyCallback callback) {
// featuresAt
//

public List<Feature> getVisibleFeatures(PointF pointF){
public List<Feature> getVisibleFeatures(PointF pointF) {
return new ArrayList<>();
}

Expand Down

0 comments on commit 8f1a426

Please sign in to comment.