【问题标题】:Mapbox 'setMyLocationEnabled' doesn't workMapbox 'setMyLocationEnabled' 不起作用
【发布时间】:2018-04-26 16:43:47
【问题描述】:

我正在尝试在我的 mapbox 地图中启用一个位置。 但是,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>

【问题讨论】:

    标签: android android-studio gradle geolocation mapbox


    【解决方案1】:

    @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 链接。

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多