Skip to content

Commit

Permalink
- Native SDK's updated to 3.x (incl a new 'hybrid' map style)
Browse files Browse the repository at this point in the history
- Added animateCamera for iOS and Android
- Added setTilt/getTilt for Android (native SDK lacks support for iOS currently)
  • Loading branch information
EddyVerbruggen committed Feb 1, 2016
1 parent a873483 commit 700a2c5
Show file tree
Hide file tree
Showing 37 changed files with 461 additions and 23,375 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Mapbox.js is brought in automatically. There is no need to change or add anythin

## 4. Usage

Check the [demo code](demo/index.html) for all the tricks in the book!
[We've documented the Mapbox plugin API here.](http://plugins.telerik.com/cordova/plugin/mapbox)

For a quick demo, check the [demo code](demo/index.html) which shows all the tricks in the book.

## 5. License

Expand Down
67 changes: 47 additions & 20 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="msapplication-tap-highlight" content="no"/>
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height"/>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
<title>Mapbox test</title>
Expand All @@ -15,29 +14,28 @@ <h1>Mapbox test</h1>

<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>

<p class="event received">Device is Ready</p>
<br/>
<button onclick="showMap()">Show</button>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<button onclick="addMarkers()">Markers</button>
<!--button onclick="addGeoJSON()">Add GeoJSON</button-->
<button onclick="addPolygon()">Poly</button>
<button onclick="getZoomLevel()">getZoom</button>
<button onclick="zoomIn()">+</button>
<button onclick="zoomOut()">-</button>
<button onclick="setCenter()">set center</button>
<button onclick="getCenter()">get center</button>
<button onclick="hideMap()">hide</button>
<button onclick="showMap()">Show</button>
</div>
</div>

<div style="position:absolute; bottom:0; background-color: #eee">
<button onclick="animateCamera()">Camera</button>
<button onclick="addMarkers()">Markers</button>
<!--button onclick="addGeoJSON()">Add GeoJSON</button-->
<button onclick="addPolygon()">Poly</button>
<button onclick="hideMap()">hide</button><br/><br/>
<button onclick="getZoomLevel()">getZoom</button>
<button onclick="zoomIn()">zoom +</button>
<button onclick="zoomOut()">zoom -</button><br/><br/>
<button onclick="setCenter()">set center</button>
<button onclick="getCenter()">get center</button>
<button onclick="setTilt()">set tilt</button>
<button onclick="getTilt()">get tilt</button><br/><br/>
</div>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script>
Expand All @@ -57,6 +55,19 @@ <h1>Mapbox test</h1>
});
}

function setTilt() {
Mapbox.setTilt({
pitch: 45,
duration: 4500
});
}

function getTilt() {
Mapbox.getTilt(function (t) {
alert(t)
});
}

function getZoomLevel() {
Mapbox.getZoomLevel(function (zl) {
alert(zl)
Expand Down Expand Up @@ -84,7 +95,7 @@ <h1>Mapbox test</h1>
'left': 0,
'right': 0,
'top': 0,
'bottom': 80
'bottom': 160
},
center: {
lat: 52.3702160,
Expand All @@ -97,6 +108,7 @@ <h1>Mapbox test</h1>
hideCompass: false, // default false
disableRotation: false, // default false
disableScroll: false, // default false
disableTilt: false, // default false
disableZoom: false, // default false
disablePitch: false, // default false
markers: [
Expand Down Expand Up @@ -158,6 +170,21 @@ <h1>Mapbox test</h1>
});
}

function animateCamera() {
Mapbox.animateCamera({
// Sets the center of the map to Maracanã
target: {
lat: 52.3632160,
lng: 4.9011680
},
zoom: 17, // Android, zoomLevel
altitude: 1000, // iOS, meters from the ground
bearing: 270, // Sets the orientation of the camera to look west
tilt: 40, // // Sets the tilt of the camera to 30 degrees
duration: 10 // in seconds
});
}

function addMarkers() {
Mapbox.addMarkers([
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.2",
"version": "1.2.0",
"name": "cordova-plugin-mapbox",
"cordova_name": "Mapbox",
"description": "Need native maps? Look no further!",
Expand Down
6 changes: 3 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-mapbox"
version="1.1.2">
version="1.2.0">

<name>Mapbox</name>

Expand Down Expand Up @@ -87,6 +87,7 @@
<header-file src="src/ios/Headers/MGLAnnotation.h" />
<header-file src="src/ios/Headers/MGLAnnotationImage.h" />
<header-file src="src/ios/Headers/MGLGeometry.h" />
<header-file src="src/ios/Headers/MGLMapCamera.h" />
<header-file src="src/ios/Headers/MGLMapView+IBAdditions.h" />
<header-file src="src/ios/Headers/MGLMapView.h" />
<header-file src="src/ios/Headers/MGLMultiPoint.h" />
Expand All @@ -95,16 +96,15 @@
<header-file src="src/ios/Headers/MGLPolygon.h" />
<header-file src="src/ios/Headers/MGLPolyline.h" />
<header-file src="src/ios/Headers/MGLShape.h" />
<header-file src="src/ios/Headers/MGLStyle.h" />
<header-file src="src/ios/Headers/MGLTypes.h" />
<header-file src="src/ios/Headers/MGLUserLocation.h" />
<header-file src="src/ios/Headers/MGLMapCamera.h" />

<source-file src="src/ios/libs/libMapbox.a" framework="true"/>
<resource-file src="src/ios/libs/Mapbox.bundle" />
<!-- This bundle adds a mandatory opt-out switch for Mapbox Metrics to the iOS Settings app -->
<resource-file src="src/ios/libs/Settings.bundle" />

<framework src="CoreTelephony.framework" weak="true" />
<framework src="GLKit.framework" weak="true" />
<framework src="ImageIO.framework" weak="true" />
<framework src="MobileCoreServices.framework" weak="true" />
Expand Down
103 changes: 92 additions & 11 deletions src/android/Mapbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
Expand Down Expand Up @@ -55,6 +57,9 @@ public class Mapbox extends CordovaPlugin {
private static final String ACTION_SET_ZOOMLEVEL = "setZoomLevel";
private static final String ACTION_GET_CENTER = "getCenter";
private static final String ACTION_SET_CENTER = "setCenter";
private static final String ACTION_GET_TILT = "getTilt";
private static final String ACTION_SET_TILT = "setTilt";
private static final String ACTION_ANIMATE_CAMERA = "animateCamera";

public static MapView mapView;
private static float retinaFactor;
Expand Down Expand Up @@ -119,6 +124,7 @@ public void run() {
mapView.setRotateEnabled(options.isNull("disableRotation") || !options.getBoolean("disableRotation"));
mapView.setScrollEnabled(options.isNull("disableScroll") || !options.getBoolean("disableScroll"));
mapView.setZoomEnabled(options.isNull("disableZoom") || !options.getBoolean("disableZoom"));
mapView.setTiltEnabled(options.isNull("disableTilt") || !options.getBoolean("disableTilt"));

// placing these offscreen in case the user wants to hide them
if (!options.isNull("hideAttribution") && options.getBoolean("hideAttribution")) {
Expand All @@ -132,13 +138,17 @@ public void run() {
showUserLocation();
}

final double zoomLevel = options.isNull("zoomLevel") ? 10 : options.getDouble("zoomLevel");
Double zoom = options.isNull("zoomLevel") ? 10 : options.getDouble("zoomLevel");
float zoomLevel = zoom.floatValue();
if (center != null) {
final double lat = center.getDouble("lat");
final double lng = center.getDouble("lng");
mapView.setCenterCoordinate(new LatLngZoom(lat, lng, zoomLevel));
mapView.setLatLng(new LatLngZoom(lat, lng, zoomLevel));
} else {
mapView.setZoomLevel(zoomLevel);
if (zoomLevel > 18.0) {
zoomLevel = 18.0f;
}
mapView.setZoom(zoomLevel);
}

if (options.has("markers")) {
Expand Down Expand Up @@ -183,7 +193,7 @@ public void run() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final double zoomLevel = mapView.getZoomLevel();
final double zoomLevel = mapView.getZoom();
callbackContext.success("" + zoomLevel);
}
});
Expand All @@ -199,7 +209,7 @@ public void run() {
final double zoom = options.getDouble("level");
if (zoom >= 0 && zoom <= 20) {
final boolean animated = !options.isNull("animated") && options.getBoolean("animated");
mapView.setZoomLevel(zoom, animated);
mapView.setZoom(zoom, animated);
callbackContext.success();
} else {
callbackContext.error("invalid zoomlevel, use any double value from 0 to 20 (like 8.3)");
Expand All @@ -216,7 +226,7 @@ public void run() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final LatLng center = mapView.getCenterCoordinate();
final LatLng center = mapView.getLatLng();
Map<String, Double> result = new HashMap<String, Double>();
result.put("lat", center.getLatitude());
result.put("lng", center.getLongitude());
Expand All @@ -235,7 +245,76 @@ public void run() {
final boolean animated = !options.isNull("animated") && options.getBoolean("animated");
final double lat = options.getDouble("lat");
final double lng = options.getDouble("lng");
mapView.setCenterCoordinate(new LatLng(lat, lng), animated);
mapView.setLatLng(new LatLng(lat, lng), animated);
callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
}

} else if (ACTION_GET_TILT.equals(action)) {
if (mapView != null) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final double tilt = mapView.getTilt();
callbackContext.success("" + tilt);
}
});
}

} else if (ACTION_SET_TILT.equals(action)) {
if (mapView != null) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
final JSONObject options = args.getJSONObject(0);
mapView.setTilt(
options.optDouble("pitch", 20), // default 20
options.optLong("duration", 5000)); // default 5s
callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
}

} else if (ACTION_ANIMATE_CAMERA.equals(action)) {
if (mapView != null) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
// TODO check mandatory elements
final JSONObject options = args.getJSONObject(0);

final JSONObject target = options.getJSONObject("target");
final double lat = target.getDouble("lat");
final double lng = target.getDouble("lng");

final CameraPosition.Builder builder =
new CameraPosition.Builder()
.target(new LatLng(lat, lng));

if (options.has("bearing")) {
builder.bearing(((Double)options.getDouble("bearing")).floatValue());
}
if (options.has("tilt")) {
builder.tilt(((Double)options.getDouble("tilt")).floatValue());
}
if (options.has("zoomLevel")) {
builder.zoom(((Double)options.getDouble("zoomLevel")).floatValue());
}

mapView.animateCamera(
CameraUpdateFactory.newCameraPosition(builder.build()),
(options.optInt("duration", 15)) * 1000, // default 15 seconds
null);

callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
Expand All @@ -252,7 +331,7 @@ public void run() {
final PolygonOptions polygon = new PolygonOptions();
final JSONObject options = args.getJSONObject(0);
final JSONArray points = options.getJSONArray("points");
for (int i=0; i<points.length(); i++) {
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");
Expand Down Expand Up @@ -317,7 +396,7 @@ private void addMarkers(JSONArray markers) throws JSONException {
private class MarkerClickListener implements MapView.OnInfoWindowClickListener {

@Override
public boolean onMarkerClick(Marker marker) {
public boolean onMarkerClick(@NonNull Marker marker) {
// callback
if (markerCallbackContext != null) {
final JSONObject json = new JSONObject();
Expand Down Expand Up @@ -366,7 +445,7 @@ private boolean permissionGranted(String... types) {
return true;
}
for (final String type : types) {
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this.cordova.getActivity(), type)) {
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this.cordova.getActivity(), type)) {
return false;
}
}
Expand All @@ -375,6 +454,7 @@ private boolean permissionGranted(String... types) {

protected void showUserLocation() {
if (permissionGranted(COARSE_LOCATION, FINE_LOCATION)) {
//noinspection MissingPermission
mapView.setMyLocationEnabled(showUserLocation);
} else {
requestPermission(COARSE_LOCATION, FINE_LOCATION);
Expand All @@ -389,6 +469,7 @@ private void requestPermission(String... types) {
LOCATION_REQ_CODE);
}

// TODO
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
Expand Down
2 changes: 1 addition & 1 deletion src/android/mapbox.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {

dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:2.3.0@aar'){
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:3.2.0@aar'){
transitive=true
}
}
5 changes: 5 additions & 0 deletions src/ios/CDVMapbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- (void) addMarkers:(CDVInvokedUrlCommand*)command;
- (void) addMarkerCallback:(CDVInvokedUrlCommand*)command;

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

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

- (void) addGeoJSON:(CDVInvokedUrlCommand*)command;
Expand All @@ -23,4 +25,7 @@
- (void) getZoomLevel:(CDVInvokedUrlCommand*)command;
- (void) setZoomLevel:(CDVInvokedUrlCommand*)command;

- (void) getTilt:(CDVInvokedUrlCommand*)command;
- (void) setTilt:(CDVInvokedUrlCommand*)command;

@end
Loading

3 comments on commit 700a2c5

@roblav96
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES THANK YOU!!!

I'm so excited to see work being done on this project! Much love!

Any chances of implementing a project method?

@EddyVerbruggen
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 👍

I couldn't find it.. can you perhaps check if they have it in their native iOS/Android SDK's? Or ask on their github. Plz let me know what you find.

@roblav96
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a pretty good look into their docs. Couldn't find much on projecting. It seems like you can't manipulate html/DOM with the SDK.

Please sign in to comment.