gpt4 book ai didi

android - Mapbox 'setMyLocationEnabled' 不工作

转载 作者:太空狗 更新时间:2023-10-29 14:39:50 26 4
gpt4 key购买 nike

我正在尝试在我的 mapbox map 中启用一个位置。但是,行 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:二进制 XML 文件第 0 行:错误膨胀类 com.mapbox.mapboxsdk.maps.MapView

希望在这方面得到一些帮助。相关代码如下,谢谢!

这是 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));
// }
}
});
}

这是来自 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>

最佳答案

@Hey mickey 检查这个,如何获取当前位置:

           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关联。

关于android - Mapbox 'setMyLocationEnabled' 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com