Skip to content

Commit

Permalink
Updated Android SDK to 2.2.0 for Android 6 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 3, 2015
1 parent 2b4afc2 commit b8770c7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,33 @@ Draw a shape (like a line/route, or star). Just connect the dots like we did as
]
})
```

### function: hasFineLocationPermission / requestFineLocationPermission
On Android 6 you need to request permission to be able to show the user's position on the map at runtime when targeting API level 23+.
Even if the `uses-permission` tag for `ACCESS_FINE_LOCATION` is present in `AndroidManifest.xml`.

Note that `hasFineLocationPermission` will return true when:
* You're running this on iOS, or
* You're targeting an API level lower than 23, or
* You're using Android < 6, or
* You've already granted permission.

```js
mapbox.hasFineLocationPermission().then(
function(granted) {
// if this is 'false' you probably want to call 'requestFineLocationPermission' now
console.log("Has Location Permission? " + result);
}
);

// if no permission was granted previously this wil open a user consent screen
mapbox.requestFineLocationPermission().then(
function() {
console.log("Location permission requested");
}
);
```

Note that the `show` function will also check for permission if you passed in `showUserLocation : true`.
If you didn't request permission before showing the map, and permission was needed, then
the location is not drawn on the map and the plugin will log an error to the console.
4 changes: 2 additions & 2 deletions demo/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<Button row="3" col="0" text="add markers" tap="{{ doAddMarkers }}" />
<Button row="3" col="1" text="add polygon" tap="{{ doAddPolygon }}" />

<Button row="4" col="0" text="has location permission?" tap="{{ doCheckHasFineLocationPermission }}" />
<Button row="4" col="1" text="request location permission" tap="{{ doRequestFineLocationPermission }}" />
<Button row="4" col="0" text="location perm?" tap="{{ doCheckHasFineLocationPermission }}" />
<Button row="4" col="1" text="ask permission" tap="{{ doRequestFineLocationPermission }}" />

</GridLayout>
</TabViewItem.view>
Expand Down
64 changes: 32 additions & 32 deletions mapbox.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,37 @@ var application = require("application");
var frame = require("ui/frame");
var mapbox = require("./mapbox-common");
var context = application.android.context;
var FINE_LOCATION_PERMISSION_REQUEST_CODE = 111;
var ACCESS_FINE_LOCATION_PERMISSION_REQUEST_CODE = 111;

mapbox._fineLocationPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, android.Manifest.permission.ACCESS_FINE_LOCATION);
}
return hasPermission;
};

mapbox.hasFineLocationPermission = function () {
return new Promise(function (resolve) {
resolve(mapbox._fineLocationPermissionGranted());
});
};

mapbox.requestFineLocationPermission = function () {
return new Promise(function (resolve) {
if (!mapbox._fineLocationPermissionGranted()) {
// in a future version we could hook up the callback and change this flow a bit
android.support.v4.app.ActivityCompat.requestPermissions(
application.android.foregroundActivity,
[android.Manifest.permission.ACCESS_FINE_LOCATION],
ACCESS_FINE_LOCATION_PERMISSION_REQUEST_CODE);
// this is not the nicest solution as the user needs to initiate scanning again after granting permission,
// so enhance this in a future version, but it's ok for now
resolve();
}
});
};

mapbox.show = function(arg) {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -46,7 +76,7 @@ mapbox.show = function(arg) {
mapView.setZoomEnabled(!settings.disableZoom);

if (settings.showUserLocation) {
if (mapbox.hasFineLocationPermission()) {
if (mapbox._fineLocationPermissionGranted()) {
mapView.setMyLocationEnabled(true);
} else {
// devs should ask permission upfront, otherwise enabling location will crash the app on Android 6
Expand Down Expand Up @@ -203,34 +233,4 @@ mapbox.addPolygon = function (arg) {
});
};

mapbox.requestFineLocationPermission = function () {
return new Promise(function (resolve) {
if (!mapbox._fineLocationPermissionGranted()) {
// in a future version we could hook up the callback and change this flow a bit
android.support.v4.app.ActivityCompat.requestPermissions(
appModule.android.foregroundActivity,
[android.Manifest.permission.FINE_LOCATION],
FINE_LOCATION_PERMISSION_REQUEST_CODE);
// this is not the nicest solution as the user needs to initiate scanning again after granting permission,
// so enhance this in a future version, but it's ok for now
resolve();
}
});
};

mapbox._fineLocationPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
android.support.v4.content.ContextCompat.checkSelfPermission(appModule.android.foregroundActivity, android.Manifest.permission.FINE_LOCATION);
}
return hasPermission;
};

mapbox.hasFineLocationPermission = function () {
return new Promise(function (resolve) {
resolve(mapbox._fineLocationPermissionGranted());
});
};

module.exports = mapbox;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-mapbox",
"version": "1.0.3",
"version": "1.0.4",
"description" : "Native Maps, by Mapbox.",
"main" : "mapbox.js",
"nativescript": {
Expand Down

0 comments on commit b8770c7

Please sign in to comment.