in google map if i open there is a function to set my location button enable and disable "gmaps.getUiSettings().setMyLocationButtonEnabled(false);" same way is there any function in mapbox sdk to enable or disable? or we need to write our own logic?
I've implemented something similar to what I believe you are trying to accomplish. I don't think there's a "setMyLocationButtonEnabled" method so the way I did it is I just create a Floating Action Button and on its click I just get the users location. From that I move the camera to the new position.
A couple things to note are that if you're using android 6.0 (API 23) or higher you need to get location permission at runtime else you can just put these permissions in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
When you create your MapView within the onCreate method also include:
mapView.setMyLocationEnabled(true);
From the Docs "While setMyLocationEnabled enabled, the my-location layer continuously draws an indication of a user's current location and bearing." After that I add the FAB view in xml and create the method in java and just call it within the onCreate method.
<!-- Implementation of find my location button -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/myLocationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@android:drawable/ic_menu_mylocation"
app:borderWidth="0dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:fabSize="normal" />
private void userLocationFAB(){
FloatingActionButton FAB = (FloatingActionButton) findViewById(R.id.myLocationButton);
FAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mapView.getMyLocation() != null) { // Check to ensure coordinates aren't null, probably a better way of doing this...
mapView.setCenterCoordinate(new LatLngZoom(mapView.getMyLocation().getLatitude(), mapView.getMyLocation().getLongitude(), 20), true);
}
}
});
}
The end result gives you something like this:

Now there's probably a better way to move the camera besides "setCenterCoordinate" but I'm still transitioning from the old Android SDK (where I simply used goToUserLocation). Anyones welcome to correct me on this.
@ragummrsa2930 User location is enabled / disabled via the MapView.setMyLocationEnabled(boolean enabled) method. There is no Mapbox specific UI component though for turning it on / off (we don't want to force a UI on apps). That said we have an example in the TestApp using the FloatingActionButton similar to @cammace's example.
Hi Brad Leege,
I have one problem while using mapbox sdk. getting following error
Caused by: com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
Using MapView requires setting a valid access token. See the INSTALL.md
i will explain you this error step by steps.
1) it happens in android 6.0 Marshmallow (23)
2) open the app with map view its working fine.
3) go to app setting and click app permission
4) turn off any permission and open app its crashing getting the above
crash report.
5) again open app it will work fine..
Thanks,
M M Ragupathy
Problem solved, after adding onSaveInstanceState method. Anyway thanks.
On Fri, Jan 8, 2016 at 12:05 PM, Ragupathy [email protected] wrote:
Hi Brad Leege,
I have one problem while using mapbox sdk. getting following error
Caused by: com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
Using MapView requires setting a valid access token. See the INSTALL.mdi will explain you this error step by steps.
1) it happens in android 6.0 Marshmallow (23)
2) open the app with map view its working fine.
3) go to app setting and click app permission
4) turn off any permission and open app its crashing getting the above
crash report.
5) again open app it will work fine..Thanks,
M M Ragupathy
@ragummrsa2930 Glad to hear that things are working. :+1:
Yep, the lifecycle methods are _critically important_ to implement. We have a full list showing which ones and how to set them up in the Mapbox Examples documentation for future reference.
Hi Brad Leege,
hope you are doing good.
im getting following crash some times
@ragummrsa2930 This looks like another lifecycle issue. I'd review the examples again and if there are still issues please open a new ticket with information about the version of the SDK in use, the device that your experiencing this issue on, and a code sample. Thanks!
@bleege . I am using mapview in fragment.
I am assigning map view in different fragmet life cycles as below:
@Override
public void onStart() {
super.onStart();
if (mapView != null) {
mapView.onStart();
}
}
` @Override
public void onResume() {
super.onResume();
if (mapView != null) {
mapView.onResume();
}
}`
where to destroy()themapview. Means in what life cycle is it in onDetach()method to destroy the mapview
@Dharma237 I'd recommend using the MapFragment class that's part of the SDK instead of the MapView in the Fragment. There's an example in the TestApp that shows how to use it.
@bleege ,
I have already implemented the code by adding mapview in fragment in my project and its has many classes to change into Fragment Activity.
But when I using mapview, I am getting error like below
Caused by: com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
Using MapView requires setting a valid access token. See the INSTALL.md.
Whenever this error comes, my app gets closed and reopens from home screen. Can you please help me out from this problem?
I have already implemented the code by adding mapview in fragment in my project and its has many classes to change into Fragment Activity.
Yep, just to clarify but the recommendation was to use MapFragment from the SDK. The name of the example in the TestApp is called MapFragmentActivity.
Caused by: com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
Using MapView requires setting a valid access token. See the INSTALL.md.Whenever this error comes, my app gets closed and reopens from home screen. Can you please help me out from this problem?
The access token needs to be set in the MapView. It can either be set programmatically via MapView.setAccessToken() of in the Layout.xml as shown in the Examples.
in xml file it keeps showing:
The following classes could not be instantiated:
-聽com.mapbox.mapboxsdk.maps.widgets.UserLocationView (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE Exception Details java.lang.UnsupportedOperationException: Unsupported Service: location 聽聽at com.android.layoutlib.bridge.android.BridgeContext.getSystemService(BridgeContext.java:570) 聽聽at com.mapzen.android.lost.internal.FusionEngine.
How can I solve this and display my maps activity?
hi
cannot find symbol method setMyLocationEnabled(boolean)
cannot find symbol method getMyLocation()
cannot find symbol class LatLngZoom
cannot find symbol method getMyLocation()
The error picture is below
Thank you for help
@moslem72 code this as true in MapReady method in the bottom of mainActivity:
Like mMapView.setMyLocationEnabled(true) etc
I've implemented something similar to what I believe you are trying to accomplish. I don't think there's a "setMyLocationButtonEnabled" method so the way I did it is I just create a Floating Action Button and on its click I just get the users location. From that I move the camera to the new position.
A couple things to note are that if you're using android 6.0 (API 23) or higher you need to get location permission at runtime else you can just put these permissions in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>When you create your MapView within the onCreate method also include:
mapView.setMyLocationEnabled(true);From the Docs "While setMyLocationEnabled enabled, the my-location layer continuously draws an indication of a user's current location and bearing." After that I add the FAB view in xml and create the method in java and just call it within the onCreate method.
<!-- Implementation of find my location button --> <android.support.design.widget.FloatingActionButton android:id="@+id/myLocationButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:layout_marginBottom="20dp" android:layout_marginRight="20dp" android:src="@android:drawable/ic_menu_mylocation" app:borderWidth="0dp" app:elevation="6dp" app:pressedTranslationZ="12dp" app:fabSize="normal" />private void userLocationFAB(){ FloatingActionButton FAB = (FloatingActionButton) findViewById(R.id.myLocationButton); FAB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mapView.getMyLocation() != null) { // Check to ensure coordinates aren't null, probably a better way of doing this... mapView.setCenterCoordinate(new LatLngZoom(mapView.getMyLocation().getLatitude(), mapView.getMyLocation().getLongitude(), 20), true); } } }); }The end result gives you something like this:
Now there's probably a better way to move the camera besides "setCenterCoordinate" but I'm still transitioning from the old Android SDK (where I simply used goToUserLocation). Anyones welcome to correct me on this.
Your description looks very nice, but mapView.setCenterCoordinate not working?
Hello,
I need this also and I can't find a property in Mapbox to make this button show up to be able to get back to the current location centered.
Please fix this,
Our SDK doesn't make any assumptions about the end user UI as floating action buttons.
You probably however can reuse some examples around this listed in https://github.com/mapbox/mapbox-gl-native/tree/master/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/location
Most helpful comment
I've implemented something similar to what I believe you are trying to accomplish. I don't think there's a "setMyLocationButtonEnabled" method so the way I did it is I just create a Floating Action Button and on its click I just get the users location. From that I move the camera to the new position.
A couple things to note are that if you're using android 6.0 (API 23) or higher you need to get location permission at runtime else you can just put these permissions in the manifest:
When you create your MapView within the onCreate method also include:
From the Docs "While setMyLocationEnabled enabled, the my-location layer continuously draws an indication of a user's current location and bearing." After that I add the FAB view in xml and create the method in java and just call it within the onCreate method.
The end result gives you something like this:
Now there's probably a better way to move the camera besides "setCenterCoordinate" but I'm still transitioning from the old Android SDK (where I simply used goToUserLocation). Anyones welcome to correct me on this.