diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptions.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptions.java new file mode 100644 index 0000000..1d273cc --- /dev/null +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptions.java @@ -0,0 +1,8 @@ +package com.mapbox.reactnativemapboxgl; + +import com.mapbox.mapboxsdk.annotations.Annotation; +import com.mapbox.mapboxsdk.maps.MapboxMap; + +public interface RNMGLAnnotationOptions { + public abstract Annotation addToMap(MapboxMap map); +} \ No newline at end of file diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLAnnotationFactory.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptionsFactory.java similarity index 69% rename from android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLAnnotationFactory.java rename to android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptionsFactory.java index 8c92fa8..290b6b6 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLAnnotationFactory.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/RNMGLAnnotationOptionsFactory.java @@ -6,19 +6,16 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; -import android.support.annotation.RequiresPermission; -import android.view.View; import com.facebook.react.bridge.ReadableMap; +import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; -import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; -import com.mapbox.mapboxsdk.annotations.Polygon; import com.mapbox.mapboxsdk.annotations.PolygonOptions; import com.mapbox.mapboxsdk.annotations.PolylineOptions; import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; import java.io.IOException; import java.io.InputStream; @@ -27,7 +24,60 @@ import java.net.URL; import java.util.HashMap; import java.util.Map; -public class ReactNativeMapboxGLAnnotationFactory { +class RNMGLMarkerOptions implements RNMGLAnnotationOptions { + protected MarkerOptions _options; + + public RNMGLMarkerOptions(MarkerOptions options) { + _options = options; + } + + @Override + public Annotation addToMap(MapboxMap map) { + return map.addMarker(_options); + } +} + +class RNMGLPolylineOptions implements RNMGLAnnotationOptions { + protected PolylineOptions _options; + + public RNMGLPolylineOptions(PolylineOptions options) { + _options = options; + } + + @Override + public Annotation addToMap(MapboxMap map) { + return map.addPolyline(_options); + } +} + +class RNMGLPolygonOptions implements RNMGLAnnotationOptions { + protected PolygonOptions _options; + + public RNMGLPolygonOptions(PolygonOptions options) { + _options = options; + } + + @Override + public Annotation addToMap(MapboxMap map) { + return map.addPolygon(_options); + } +} + +public class RNMGLAnnotationOptionsFactory { + + public static RNMGLAnnotationOptions annotationOptionsFromJS(ReadableMap annotation, Context context) { + String type = annotation.getString("type"); + + if (type.equals("point")) { + return markerOptionsFromJS(annotation, context); + } else if (type.equals("polyline")) { + return polylineOptionsFromJS(annotation); + } else if (type.equals("polygon")) { + return polygonOptionsFromJS(annotation); + } + + return null; + } static Drawable drawableFromDrawableName(Context context, String drawableName) { int resID = context.getResources().getIdentifier(drawableName, "drawable", context.getApplicationContext().getPackageName()); @@ -35,7 +85,7 @@ public class ReactNativeMapboxGLAnnotationFactory { return new BitmapDrawable(context.getResources(), bitmap); } - public static Drawable drawableFromUrl(Context context, String url) throws IOException { + static Drawable drawableFromUrl(Context context, String url) throws IOException { Bitmap x; HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); @@ -48,7 +98,7 @@ public class ReactNativeMapboxGLAnnotationFactory { static Map drawableCache = new HashMap(); - public static Drawable drawableFromPath(Context context, String path) throws IOException { + static Drawable drawableFromPath(Context context, String path) throws IOException { Drawable drawable = drawableCache.get(path); if (drawable != null) { return drawable; } @@ -62,7 +112,7 @@ public class ReactNativeMapboxGLAnnotationFactory { return drawable; } - public static MarkerOptions markerFromJS(ReadableMap annotation, View view) { + static RNMGLAnnotationOptions markerOptionsFromJS(ReadableMap annotation, Context context) { MarkerOptions marker = new MarkerOptions(); double latitude = annotation.getArray("coordinates").getDouble(0); @@ -84,12 +134,12 @@ public class ReactNativeMapboxGLAnnotationFactory { ReadableMap annotationImage = annotation.getMap("annotationImage"); String annotationURL = annotationImage.getString("url"); try { - Drawable image = drawableFromPath(view.getContext(), annotationURL); + Drawable image = drawableFromPath(context, annotationURL); - IconFactory iconFactory = IconFactory.getInstance(view.getContext()); + IconFactory iconFactory = IconFactory.getInstance(context); Icon icon; if (annotationImage.hasKey("height") && annotationImage.hasKey("width")) { - float scale = view.getResources().getDisplayMetrics().density; + float scale = context.getResources().getDisplayMetrics().density; int height = Math.round((float)annotationImage.getInt("height") * scale); int width = Math.round((float)annotationImage.getInt("width") * scale); icon = iconFactory.fromDrawable(image, width, height); @@ -102,10 +152,10 @@ public class ReactNativeMapboxGLAnnotationFactory { e.printStackTrace(); } } - return marker; + return new RNMGLMarkerOptions(marker); } - static PolylineOptions polylineFromJS(ReadableMap annotation) { + static RNMGLAnnotationOptions polylineOptionsFromJS(ReadableMap annotation) { PolylineOptions polyline = new PolylineOptions(); int coordSize = annotation.getArray("coordinates").size(); @@ -130,10 +180,10 @@ public class ReactNativeMapboxGLAnnotationFactory { polyline.width(strokeWidth); } - return polyline; + return new RNMGLPolylineOptions(polyline); } - static PolygonOptions polygonFromJS(ReadableMap annotation) { + static RNMGLAnnotationOptions polygonOptionsFromJS(ReadableMap annotation) { PolygonOptions polygon = new PolygonOptions(); int coordSize = annotation.getArray("coordinates").size(); @@ -158,6 +208,6 @@ public class ReactNativeMapboxGLAnnotationFactory { polygon.strokeColor(strokeColor); } - return polygon; + return new RNMGLPolygonOptions(polygon); } } diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java index 9797b8b..db6bf5b 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java @@ -14,7 +14,6 @@ import com.facebook.react.modules.core.RCTNativeAppEventEmitter; import com.facebook.react.uimanager.annotations.ReactProp; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.views.scroll.ScrollEventType; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdate; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; @@ -370,16 +369,10 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager _annotations = new HashMap(); private Map _annotationIdsToName = new HashMap(); - private Map _markerOptions = new HashMap(); - private Map _polylineOptions = new HashMap(); - private Map _polygonOptions = new HashMap(); + private Map _annotationOptions = new HashMap(); private android.os.Handler _handler; @@ -177,24 +175,12 @@ public class ReactNativeMapboxGLView extends RelativeLayout implements _map.setOnInfoWindowClickListener(this); // Create annotations - for (Map.Entry entry : _markerOptions.entrySet()) { - Annotation annotation = _map.addMarker(entry.getValue()); + for (Map.Entry entry : _annotationOptions.entrySet()) { + Annotation annotation = entry.getValue().addToMap(_map); _annotations.put(entry.getKey(), annotation); _annotationIdsToName.put(annotation.getId(), entry.getKey()); } - _markerOptions.clear(); - for (Map.Entry entry : _polylineOptions.entrySet()) { - Annotation annotation = _map.addPolyline(entry.getValue()); - _annotations.put(entry.getKey(), annotation); - _annotationIdsToName.put(annotation.getId(), entry.getKey()); - } - _polylineOptions.clear(); - for (Map.Entry entry : _polygonOptions.entrySet()) { - Annotation annotation = _map.addPolygon(entry.getValue()); - _annotations.put(entry.getKey(), annotation); - _annotationIdsToName.put(annotation.getId(), entry.getKey()); - } - _polygonOptions.clear(); + _annotationOptions.clear(); } private void destroyMapView() { @@ -607,9 +593,7 @@ public class ReactNativeMapboxGLView extends RelativeLayout implements @Nullable Annotation _removeAnnotation(String name, boolean keep) { if (_map == null) { - _markerOptions.remove(name); - _polylineOptions.remove(name); - _polygonOptions.remove(name); + _annotationOptions.remove(name); return null; } Annotation annotation = _annotations.remove(name); @@ -626,9 +610,7 @@ public class ReactNativeMapboxGLView extends RelativeLayout implements } public void removeAllAnnotations() { - _markerOptions.clear(); - _polygonOptions.clear(); - _polygonOptions.clear(); + _annotationOptions.clear(); _annotations.clear(); _annotationIdsToName.clear(); if (_map != null) { @@ -636,41 +618,13 @@ public class ReactNativeMapboxGLView extends RelativeLayout implements } } - public void setMarker(String name, MarkerOptions options) { + public void setAnnotation(String name, RNMGLAnnotationOptions options) { Annotation removed = _removeAnnotation(name, true); if (_map == null) { - _markerOptions.put(name, options); + _annotationOptions.put(name, options); } else { - Annotation annotation = _map.addMarker(options); - _annotations.put(name, annotation); - _annotationIdsToName.put(annotation.getId(), name); - } - - if (removed != null) { _map.removeAnnotation(removed); } - } - - public void setPolyline(String name, PolylineOptions options) { - Annotation removed = _removeAnnotation(name, true); - - if (_map == null) { - _polylineOptions.put(name, options); - } else { - Annotation annotation = _map.addPolyline(options); - _annotations.put(name, annotation); - _annotationIdsToName.put(annotation.getId(), name); - } - - if (removed != null) { _map.removeAnnotation(removed); } - } - - public void setPolygon(String name, PolygonOptions options) { - Annotation removed = _removeAnnotation(name, true); - - if (_map == null) { - _polygonOptions.put(name, options); - } else { - Annotation annotation = _map.addPolygon(options); + Annotation annotation = options.addToMap(_map); _annotations.put(name, annotation); _annotationIdsToName.put(annotation.getId(), name); }