nativescript-mapbox
nativescript-mapbox copied to clipboard
Error: Location not available
I have the following code snippets:
In NativeScript-Vue.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<Mapbox
accessToken="TOKEN"
mapStyle="streets"
latitude="-5.852365494"
longitude="-35.36503315"
hideCompass="false"
zoomLevel="12"
showUserLocation="true" <!-- Not Working -->
disableZoom="false"
disableRotation="false"
disableScroll="false"
disableTilt="false"
@mapReady="onMapReady($event)">
</Mapbox>
onMapReady(args) {
args.map.addMarkers([
{
lat: -5.852365494,
lng: -35.36503315,
title: "U S F JOSE COELHO",
subtitle: "Rua José Coelho - CENTRO",
onCalloutTap: () => {
utils.openUrl("https://www.thepolyglotdeveloper.com");
}
}
]);
args.map.getUserLocation().then(
function(userLocation) {
alert(userLocation.location.lat + ", " + userLocation.location.lng)
console.log("Current user location: " + userLocation.location.lat + ", " + userLocation.location.lng);
console.log("Current user speed: " + userLocation.speed);
alert()
}
).catch(e => {
alert("Error: " + e) //Error: Location not available
})
}
Hi,
I too had issues with getting the user location to show on the map, and I ran into the same error when calling getUserLocation()
on Android devices.
I encountered two problems:
The plugin failed to request permission
In the npm package version 4.3.1 of this plugin (since 4.4.1 crashes on back navigation and pausing/resuming the app in Android) at node_modules/nativescript-mapbox/mapbox.android.js
[185]: android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, android.Manifest.permission.ACCESS_FINE_LOCATION);
I found that android.support.v4.content
was undefined
. Nativescript is dropping the support for the android support library since Nativescript 6 - as you can read here.
So I added this in the top of the mapbox.android.js
file:
let androidSupport=null;
if (global.androidx && global.androidx.core) {
androidSupport = global.androidx.core;
} else if (android.support && android.support.v4) {
androidSupport = android.support.v4;
}
And then changed any reference to android.support.v4 to the corresponding variable androidSupport
.
That solved the issue with request permissions for me at least. I don't know if this should be included in newer version in this plugin?
LocationEngine fails to get location
Only relevant if you use plugin version 4.3.1 due to issues with Android in version 4.4.1
I also found that the LocationEngine instance failed to get the location. This LocationEngine is used to feed the location to the LocationLayerPlugin which displays the location on the map.
In the plugin (version 4.3.1) there is this line - which creates an instance of the LocationEngine (mapbox.android.js):
[191]: _locationEngine = new com.mapbox.android.core.location.LocationEngineProvider(application.android.foregroundActivity || application.android.startActivity).obtainBestLocationEngine();
I don't know much about the inner workings of the mapbox library and how the method obtainBestLocationEngineAvailable()
finds the best location engine available - but I found that it provides the following LocationEngine.Type
: GOOGLE_PLAY_SERVICES
It seems there have been an issue regarding this in the mapbox android core library. An upgrade of the mapbox android sdk or an upgrade of the core library might resolve this, but I don't know if an upgrade would break anything. Link to issue
So to manually deal with it you can add a specific LocationEngine type: (mapbox.android.js):
[191]: _locationEngine = new com.mapbox.android.core.location.LocationEngineProvider(application.android.foregroundActivity || application.android.startActivity).obtainLocationEngineBy(com.mapbox.android.core.location.LocationEngine.Type.ANDROID);
(Only do this if you are using the plugin version 4.3.1) Which did the trick. I have not tested this on many devices - only on simulator (api level 28) and a physical Android device with Android 9.
This is obviously not the best way to solve the issue but I at least are in a situation where I can't use the plugin version 4.4.1 since it crashes my app on navigation back and pausing/resuming of the app as described in the documentation in this repo.
In version 4.4.1 I dont not experience any issues with the locationEngine, well... since it has been removed and that the LocationLayerPlugin uses the default location engine 😄
Nice @mikkelmoerchkv
@mikkelmoerchkv This really is a temporary solution and give a good headache to get it to work.
@EdvaldoLima I completely agree. I would have created a PR if these issues were related to the latest version of this plugin but the thing is that these issues primarily relates to version 4.3.1 - Which I guess alot of Android devs are forced to use because of #271
Yea, this is my fault as I haven't had a chance to fix the user location on Android in my huge PR. I'm slammed with another project that is going to take me another few/several weeks before I can get back to finishing up the PR. If you want to take a look at my fork and would like to issue a PR against it it would be a huge help:
https://github.com/Yermo/nativescript-mapbox
Hi everyone,
The proposed solution by @mikkelmoerchkv doesn't work for higher Android version (9+).
Can someone have proposed solution for this?
BR, Zhi
It's on my list to fix but I'm still slammed with this other project but hopefully I'll be able to get to it next month.
Hi,
I've found a possible workaround using nativescript-geolocation. Hopefully, I will let you know once I have a good news.
Br, Zhi
@zhitomir-oreshenski-mm Yes, this is a viable solution to solve this problem right now.