我尝试在地图集地图中启用某个位置。
但是,行mapboxMap.setMyLocationEnabled(true);
失败,Android Studio提供了“无法解析方法setMyLocationEnabled(boolean)”。
我在Gradle中使用MapBox版本6.0.1(最新版本):
//
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:6.0.1@aar') { // Map BOX
transitive=true
}
当我切换到版本5.1.3(我在之前的应用程序中使用过)时,Android Studio会检测到该方法,但在运行时,应用程序会抛出以下异常:
android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
在这一方面会有所帮助。 相关代码如下,谢谢!
这是活动中的相关代码:
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));
// }
}
});
}
这是XML文件中的代码:
<?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>
答案 0 :(得分:0)
@嘿米奇检查一下,如何获取当前位置:
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);
}
检查this链接。