From 8924ba8f8738ffa6360c708e1df3ca5a195ad62e Mon Sep 17 00:00:00 2001 From: Warren Crossing Date: Wed, 13 Dec 2017 10:29:08 +1100 Subject: [PATCH 1/3] add polyline support --- src/android/Mapbox.java | 25 +++++++++++++++++++++++++ src/ios/CDVMapbox.h | 1 + src/ios/CDVMapbox.m | 24 ++++++++++++++++++++++++ www/Mapbox.js | 4 ++++ 4 files changed, 54 insertions(+) diff --git a/src/android/Mapbox.java b/src/android/Mapbox.java index 9701a66..4898849 100644 --- a/src/android/Mapbox.java +++ b/src/android/Mapbox.java @@ -17,6 +17,7 @@ import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.annotations.PolygonOptions; +import com.mapbox.mapboxsdk.annotations.PolylineOptions; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngZoom; import com.mapbox.mapboxsdk.geometry.CoordinateBounds; @@ -57,6 +58,7 @@ public class Mapbox extends CordovaPlugin { // TODO: // private static final String ACTION_REMOVE_MARKER_CALLBACK = "removeMarkerCallback"; private static final String ACTION_ADD_POLYGON = "addPolygon"; + private static final String ACTION_ADD_POLYLINE = "addPolyline"; private static final String ACTION_ADD_GEOJSON = "addGeoJSON"; private static final String ACTION_GET_CENTER = "getCenter"; private static final String ACTION_SET_CENTER = "setCenter"; @@ -394,6 +396,29 @@ public void run() { }); } + } else if (ACTION_ADD_POLYLINE.equals(action)) { + cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + try { + final PolylineOptions polygon = new PolylineOptions(); + final JSONObject options = args.getJSONObject(0); + final JSONArray points = options.getJSONArray("points"); + for (int i = 0; i < points.length(); i++) { + final JSONObject marker = points.getJSONObject(i); + final double lat = marker.getDouble("lat"); + final double lng = marker.getDouble("lng"); + polygon.add(new LatLng(lat, lng)); + } + mapView.addPolyline(polygon); + + callbackContext.success(); + } catch (JSONException e) { + callbackContext.error(e.getMessage()); + } + } + }); + } else if (ACTION_ADD_POLYGON.equals(action)) { cordova.getActivity().runOnUiThread(new Runnable() { @Override diff --git a/src/ios/CDVMapbox.h b/src/ios/CDVMapbox.h index 72bd049..32dd03c 100644 --- a/src/ios/CDVMapbox.h +++ b/src/ios/CDVMapbox.h @@ -20,6 +20,7 @@ - (void) animateCamera:(CDVInvokedUrlCommand*)command; - (void) addPolygon:(CDVInvokedUrlCommand*)command; +- (void) addPolyline:(CDVInvokedUrlCommand*)command; - (void) addGeoJSON:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/CDVMapbox.m b/src/ios/CDVMapbox.m index a3e1d8a..a228b29 100644 --- a/src/ios/CDVMapbox.m +++ b/src/ios/CDVMapbox.m @@ -200,6 +200,30 @@ - (void)animateCamera:(CDVInvokedUrlCommand*)command { [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } +- (void)addPolyline:(CDVInvokedUrlCommand*)command { + NSDictionary *args = [command.arguments objectAtIndex:0]; + NSArray* points = [args objectForKey:@"points"]; + if (points != nil) { + [self.commandDelegate runInBackground:^{ + CLLocationCoordinate2D *coordinates = malloc(points.count * sizeof(CLLocationCoordinate2D)); + for (int i=0; i Date: Fri, 15 Dec 2017 19:11:37 +1100 Subject: [PATCH 2/3] update demo for addPolyline --- demo/index.html | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/demo/index.html b/demo/index.html index 624479e..29ecdca 100644 --- a/demo/index.html +++ b/demo/index.html @@ -24,7 +24,8 @@

Mapbox test

- + +

@@ -205,6 +206,25 @@

Mapbox test

}); } + function addPolyline() { + Mapbox.addPolyline({ + points: [ + { + 'lat': 52.3832160, + 'lng': 4.8991680 + }, + { + 'lat': 52.3632160, + 'lng': 4.9011680 + }, + { + 'lat': 52.3932160, + 'lng': 4.8911680 + } + ] + }); + } + function animateCamera() { Mapbox.animateCamera({ // Sets the center of the map to MaracanĂ£ From 508efa305b5e1734a9d2904f0ec895070926e1a7 Mon Sep 17 00:00:00 2001 From: Warren Crossing Date: Sat, 16 Dec 2017 07:32:34 +1100 Subject: [PATCH 3/3] add alpha for polylines polygons --- demo/index.html | 2 ++ src/android/Mapbox.java | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/demo/index.html b/demo/index.html index 29ecdca..fa701cb 100644 --- a/demo/index.html +++ b/demo/index.html @@ -189,6 +189,7 @@

Mapbox test

function addPolygon() { Mapbox.addPolygon({ + alpha: 0.25 points: [ { 'lat': 52.3832160, @@ -208,6 +209,7 @@

Mapbox test

function addPolyline() { Mapbox.addPolyline({ + alpha: 0.25 points: [ { 'lat': 52.3832160, diff --git a/src/android/Mapbox.java b/src/android/Mapbox.java index 4898849..0f8c0e2 100644 --- a/src/android/Mapbox.java +++ b/src/android/Mapbox.java @@ -401,16 +401,17 @@ public void run() { @Override public void run() { try { - final PolylineOptions polygon = new PolylineOptions(); + final PolylineOptions polyline = new PolylineOptions(); final JSONObject options = args.getJSONObject(0); final JSONArray points = options.getJSONArray("points"); for (int i = 0; i < points.length(); i++) { final JSONObject marker = points.getJSONObject(i); final double lat = marker.getDouble("lat"); final double lng = marker.getDouble("lng"); - polygon.add(new LatLng(lat, lng)); + polyline.add(new LatLng(lat, lng)); } - mapView.addPolyline(polygon); + polyline.alpha((float)options.getDouble("alpha")); + mapView.addPolyline(polyline); callbackContext.success(); } catch (JSONException e) { @@ -433,6 +434,7 @@ public void run() { final double lng = marker.getDouble("lng"); polygon.add(new LatLng(lat, lng)); } + polygon.alpha((float)options.getDouble("alpha")); mapView.addPolygon(polygon); callbackContext.success();