Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add polyline support #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ <h1>Mapbox test</h1>
<button onclick="addMarkers()">Markers</button>
<button onclick="removeAllMarkers()">Remove All Markers</button>
<!--button onclick="addGeoJSON()">Add GeoJSON</button-->
<button onclick="addPolygon()">Poly</button>
<button onclick="addPolygon()">Polygon</button>
<button onclick="addPolyline()">Polyline</button>
<button onclick="hideMap()">hide</button><br/><br/>
<button onclick="getZoomLevel()">getZoom</button>
<button onclick="zoomIn()">zoom +</button>
Expand Down Expand Up @@ -188,6 +189,27 @@ <h1>Mapbox test</h1>

function addPolygon() {
Mapbox.addPolygon({
alpha: 0.25
points: [
{
'lat': 52.3832160,
'lng': 4.8991680
},
{
'lat': 52.3632160,
'lng': 4.9011680
},
{
'lat': 52.3932160,
'lng': 4.8911680
}
]
});
}

function addPolyline() {
Mapbox.addPolyline({
alpha: 0.25
points: [
{
'lat': 52.3832160,
Expand Down
27 changes: 27 additions & 0 deletions src/android/Mapbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -394,6 +396,30 @@ public void run() {
});
}

} else if (ACTION_ADD_POLYLINE.equals(action)) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
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");
polyline.add(new LatLng(lat, lng));
}
polyline.alpha((float)options.getDouble("alpha"));
mapView.addPolyline(polyline);

callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});

} else if (ACTION_ADD_POLYGON.equals(action)) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
Expand All @@ -408,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();
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVMapbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- (void) animateCamera:(CDVInvokedUrlCommand*)command;

- (void) addPolygon:(CDVInvokedUrlCommand*)command;
- (void) addPolyline:(CDVInvokedUrlCommand*)command;

- (void) addGeoJSON:(CDVInvokedUrlCommand*)command;

Expand Down
24 changes: 24 additions & 0 deletions src/ios/CDVMapbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<points.count; i++) {
NSDictionary* point = points[i];
NSNumber *lat = [point valueForKey:@"lat"];
NSNumber *lng = [point valueForKey:@"lng"];
coordinates[i] = CLLocationCoordinate2DMake(lat.doubleValue, lng.doubleValue);
}
NSUInteger numberOfCoordinates = points.count; // sizeof(coordinates) / sizeof(CLLocationCoordinate2D);
MGLPolyline *shape = [MGLPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
[_mapView addAnnotation:shape];
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
} else {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}

- (void)addPolygon:(CDVInvokedUrlCommand*)command {
NSDictionary *args = [command.arguments objectAtIndex:0];
NSArray* points = [args objectForKey:@"points"];
Expand Down
4 changes: 4 additions & 0 deletions www/Mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = {
cordova.exec(successCallback, errorCallback, "Mapbox", "addPolygon", [options]);
},

addPolyline: function (options, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "Mapbox", "addPolyline", [options]);
},

convertCoordinate: function(options, successCallback, errorCallback){
cordova.exec(successCallback, errorCallback, "Mapbox", "convertCoordinate", [options]);
},
Expand Down