问题
I'm trying to enable a location in my mapbox map.
However, the line mapboxMap.setMyLocationEnabled(true); fail and Android Studio gives the 'Cannot resolve method setMyLocationEnabled(boolean).
I'm using MapBox version 6.0.1 (the latest) in my Gradle:
//
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:6.0.1@aar') {  // Map BOX
    transitive=true
}
When I switch to version 5.1.3 (which I had used in a previous app I made), Android studio detects the method, but on runtime the app throws the following exception:
android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
Would love some help on this one. Relevant code is below, thanks!
This is the relevant code in the activity:
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            // Set map style
            mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
            // Set the camera's starting position
             CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(32.1622,34.8338)) // set the camera's center position on the first coordinate of the tour
                    .zoom(14)  // set the camera's zoom level
                    .tilt(0)  // set the camera's tilt
                    .build();
            // Move the camera to that position
                mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            // Set user location
            mapboxMap.setMyLocationEnabled(true);
            //Set MyLocation Button
            //userLocationFAB(DrivingActivity.get, mapboxMap);
            // Add route line
            //mapboxMap.addPolyline(getRoute());
//                // Add markers
//                ArrayList<MarkerOptions> markers = getMarkers();
//
//                for (int i=0 ; i<markers.size() ; i++) {
//                    mapboxMap.addMarker(markers.get(i));
//                }
            }
        });
    }
And this is the code from the XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="36dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Score:"
                android:layout_centerInParent="true"
                android:id="@+id/driving_window_txt"
                android:textStyle="bold"
                android:layout_margin="10dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0000000"
                android:layout_below="@id/driving_window_txt"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:id="@+id/score_counter"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Distance: 0"
                android:layout_below="@id/score_counter"
                android:layout_centerHorizontal="true"
                android:id="@+id/total_distance"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.mapbox.mapboxsdk.maps.MapView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/map"/>
            <!-- Implementation of find my location button -->
            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                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"
                android:id="@+id/fab_location"/>
        </RelativeLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
回答1:
@Hey mickey check this , how to get current location:
           mapFragment.getMapAsync(new OnMapReadyCallback() {
           @Override
           public void onMapReady(MapboxMap mapboxMap) {
            mapboxmap = mapboxMap;
            mapboxMap.setOnMapClickListener(DetailDirectionFragment.this);
            mapboxMap.getUiSettings().setZoomControlsEnabled(true);
            initLocationEngine();
        }
    });
@SuppressWarnings( {"MissingPermission"})
private void initLocationEngine() {
    locationEngine = new LocationEngineProvider(activity).obtainBestLocationEngineAvailable();
    locationEngine.setPriority(HIGH_ACCURACY);
    locationEngine.setInterval(0);
    locationEngine.setFastestInterval(1000);
    locationEngine.addLocationEngineListener(this);
    locationEngine.activate();
    if (locationEngine.getLastLocation() != null) {
        Location lastLocation = locationEngine.getLastLocation();
        onLocationChanged(lastLocation);
        currentLocation = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());
        updateLocation(lastLocation.getLongitude(), lastLocation.getLatitude());
    }
}
         @Override
public void onLocationChanged(Location location) {
    currentLocation = Point.fromLngLat(location.getLongitude(), location.getLatitude());
    Log.e("currentlocation",location.getLatitude()+"lat"+location.getLongitude()+"lng");
    onLocationFound(location);
}
@SuppressWarnings( {"MissingPermission"})
@Override
public void onConnected() {
 locationEngine.requestLocationUpdates();
  }
   private void onLocationFound(Location location) {
    if (!locationFound) {
          animateCamera(new LatLng(location.getLatitude(),                              location.getLongitude()));
        MarkerViewOptions markerViewOptions = new MarkerViewOptions()
                .position(new LatLng(location.getLatitude(), location.getLongitude()));
        currentMarker = mapboxmap.addMarker(markerViewOptions);
        locationFound = true;
    }
}
private void animateCamera(LatLng point) {
    mapboxmap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, DEFAULT_CAMERA_ZOOM), CAMERA_ANIMATION_DURATION);
}
check this link.
来源:https://stackoverflow.com/questions/50048184/mapbox-setmylocationenabled-doesnt-work