Mapbox'setMyLocationEnabled'不起作用 [英] Mapbox 'setMyLocationEnabled' doesn't work
问题描述
我正在尝试在我的地图框地图中启用一个位置.
但是,mapboxMap.setMyLocationEnabled(true);
行失败,Android Studio提供了无法解析方法setMyLocationEnabled(boolean)".
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).
我在Gradle中使用的是MapBox 6.0.1版(最新版):
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
}
当我切换到5.1.3版本(我在以前的应用程序中使用过)时,Android Studio会检测到该方法,但是在运行时,该应用程序会引发以下异常:
android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
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));
// }
}
});
}
这是XML文件中的代码:
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>
推荐答案
@Hey米奇检查一下,如何获取当前位置:
@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);
}
检查 查看全文