Fleshing out Android Annotation API, working on removing annotations through JNI.

This commit is contained in:
Nicholas Hallahan
2015-07-14 10:45:29 -07:00
parent f710e9607a
commit 1ac1ba0e18
13 changed files with 381 additions and 6 deletions

View File

@@ -0,0 +1,42 @@
package com.mapbox.mapboxgl.annotations;
import com.mapbox.mapboxgl.views.MapView;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public abstract class Annotation {
/**
* The annotation id
*
* Internal C++ id is stored as unsigned int.
*/
protected Long id;
private MapView mapView;
public Annotation() {}
public Annotation(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void remove() {
if (mapView == null) return;
mapView.removeAnnotation(this);
}
public void setId(Long id) {
this.id = id;
}
public void setMapView(MapView mapView) {
this.mapView = mapView;
}
}

View File

@@ -0,0 +1,8 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class Circle {
}

View File

@@ -0,0 +1,9 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class CircleOptions {
}

View File

@@ -0,0 +1,98 @@
package com.mapbox.mapboxgl.annotations;
import com.mapbox.mapboxgl.geometry.LatLng;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class Marker extends Annotation {
/**
* Member fields are without access qualifiers
* so that they can be directly set by MarkerOptions
* from within the module.
*/
float alpha;
float anchorU;
float anchorV;
boolean draggable;
boolean flat;
float infoWindowAnchorU;
float infoWindowAnchorV;
LatLng position;
float rotation;
String snippet;
String title;
boolean visible;
private boolean infoWindowShown = false;
public Marker() {
}
public boolean equals(Object other) {
double lat = position.getLatitude();
double lng = position.getLongitude();
// TODO Implement equals
return false;
}
public float getAlpha() {
return alpha;
}
/**
* NOTE: Google Maps Android API uses String for IDs.
*
* Internal C++ id is stored as unsigned int.
*
* @return the annotation id
*/
public long getId() {
return id;
}
public LatLng getPosition() {
return position;
}
public float getRotation() {
return rotation;
}
public String getSnippet() {
return snippet;
}
public String getTitle() {
return title;
}
// Method in Google Maps Android API
// public int hashCode()
public void hideInfoWindow() {
//TODO hideInfoWindow
infoWindowShown = true;
}
public boolean isDraggable() {
return draggable;
}
public boolean isFlat() {
return flat;
}
public boolean isInfoWindowShown () {
return infoWindowShown;
}
public boolean isVisible() {
return visible;
}
}

View File

@@ -0,0 +1,134 @@
package com.mapbox.mapboxgl.annotations;
import com.mapbox.mapboxgl.geometry.LatLng;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class MarkerOptions {
private Marker marker;
public MarkerOptions() {
marker = new Marker();
}
public MarkerOptions alpha(float alpha) {
marker.alpha = alpha;
return this;
}
public MarkerOptions anchor(float u, float v) {
marker.anchorU = u;
marker.anchorV = v;
return this;
}
public MarkerOptions draggable(boolean draggable) {
marker.draggable = draggable;
return this;
}
public MarkerOptions flat(boolean flat) {
marker.flat = flat;
return this;
}
public float getAlpha() {
return marker.alpha;
}
public float getAnchorU() {
return marker.anchorU;
}
public float getAnchorV() {
return marker.anchorV;
}
// TODO: Implement this method of Google Maps Android API
// public BitmapDescriptor getIcon () {
//
// }
public float getInfoWindowAnchorU() {
return marker.infoWindowAnchorU;
}
public float getInfoWindowAnchorV() {
return marker.infoWindowAnchorV;
}
public Marker getMarker() {
return marker;
}
public LatLng getPosition() {
return marker.position;
}
public float getRotation() {
return marker.rotation;
}
public String getSnippet() {
return marker.snippet;
}
public String getTitle() {
return marker.title;
}
// TODO: Implement this method of Google Maps Android API
// public MarkerOptions icon(BitmapDescriptor icon) {
//
// }
public MarkerOptions infoWindowAnchor(float u, float v) {
marker.infoWindowAnchorU = u;
marker.infoWindowAnchorV = v;
return this;
}
public boolean isDraggable() {
return marker.draggable;
}
public boolean isFlat() {
return marker.flat;
}
public boolean isVisible() {
return marker.visible;
}
public MarkerOptions position(LatLng position) {
marker.position = position;
return this;
}
public MarkerOptions rotation(float rotation) {
marker.rotation = rotation;
return this;
}
public MarkerOptions snippet(String snippet) {
marker.snippet = snippet;
return this;
}
public MarkerOptions title(String title) {
marker.title = title;
return this;
}
public MarkerOptions visible(boolean visible) {
marker.visible = visible;
return this;
}
// TODO: Implement this method of Google Maps Android API
// public void writeToParcel (Parcel out, int flags)
}

View File

@@ -0,0 +1,9 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class Polygon {
}

View File

@@ -0,0 +1,9 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class PolygonOptions {
}

View File

@@ -0,0 +1,9 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class Polyline {
}

View File

@@ -0,0 +1,9 @@
package com.mapbox.mapboxgl.annotations;
/**
* Created by Nicholas Hallahan on 7/13/15.
* nick@theoutpost.io
*/
public class PolylineOptions {
}

View File

@@ -29,6 +29,9 @@ import android.widget.ZoomButtonsController;
import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector;
import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector;
import com.mapbox.mapboxgl.annotations.Annotation;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
@@ -62,6 +65,11 @@ public class MapView extends SurfaceView {
private static final String STATE_CLASSES = "classes";
private static final String STATE_DEFAULT_TRANSITION_DURATION = "defaultTransitionDuration";
/**
* Every annotation that has been added to the map.
*/
private List<Annotation> annotations = new ArrayList<>();
//
// Instance members
//
@@ -204,10 +212,21 @@ public class MapView extends SurfaceView {
}
}
public void addMarker(LatLng marker) {
mNativeMapView.addMarker(marker);
public Marker addMarker(MarkerOptions markerOptions) {
Marker marker = markerOptions.getMarker();
Long markerId = mNativeMapView.addMarker(marker);
marker.setId(markerId); // the annotation needs to know its id
marker.setMapView(this); // the annotation needs to know which map view it is in
annotations.add(marker);
return marker;
}
public void removeAnnotation(Annotation annotation) {
long id = annotation.getId();
mNativeMapView.removeAnnotation(id);
}
//
// Property methods
//

View File

@@ -3,6 +3,7 @@ package com.mapbox.mapboxgl.views;
import android.graphics.PointF;
import android.view.Surface;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
import com.mapbox.mapboxgl.geometry.ProjectedMeters;
@@ -213,8 +214,13 @@ class NativeMapView {
nativeSetLatLng(mNativeMapViewPtr, latLng, duration);
}
public void addMarker(LatLng latLng) {
nativeAddMarker(mNativeMapViewPtr, latLng);
public long addMarker(Marker marker) {
LatLng latLng = marker.getPosition();
return nativeAddMarker(mNativeMapViewPtr, latLng);
}
public void removeAnnotation(long id) {
// nativeRemoveAnnotation(mNativeMapViewPtr, id);
}
public LatLng getLatLng() {
@@ -451,6 +457,8 @@ class NativeMapView {
private native long nativeAddMarker(long nativeMapViewPtr, LatLng latLng);
private native void nativeRemoveAnnotation(long nativeMapViewPtr, long id);
private native LatLng nativeGetLatLng(long nativeMapViewPtr);
private native void nativeResetPosition(long nativeMapViewPtr);

View File

@@ -23,6 +23,8 @@ import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.views.MapView;
import com.mapzen.android.lost.api.LocationListener;
@@ -80,6 +82,8 @@ public class MainActivity extends ActionBarActivity {
// Used for markers
private boolean mIsMarkersOn = false;
private Marker marker;
//
// Lifecycle events
//
@@ -272,14 +276,22 @@ public class MainActivity extends ActionBarActivity {
} else {
if (mIsMarkersOn) {
mIsMarkersOn = false;
removeMarkers();
}
}
}
private void addMarkers() {
LatLng backLot = new LatLng(38.649441, -121.369064);
mMapFragment.getMap().addMarker(backLot);
MapView map = mMapFragment.getMap();
marker = map.addMarker(new MarkerOptions()
.position(backLot)
.title("Back Lot")
.snippet("The back lot behind my house"));
}
private void removeMarkers() {
marker.remove();
}
// This class forwards location updates to updateLocation()