在Android应用中使用mapbox在地图上显示用户当前位置时出错

时间:2018-11-30 08:04:51

标签: android

我正在android应用中使用mapbox。首次正确运行,但随后的attemtps崩溃

我遇到的错误-

   11-30 10:04:41.161 2842-2842/? E/ActivityThread: Activity com.android.launcher2.Launcher has leaked IntentReceiver com.android.launcher2.Launcher$2@2227827 that was originally registered here. Are you missing a call to unregisterReceiver()?

相应的权限,依赖项和代码

我在清单中添加了位置权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

在build.gradle中,我使用了maven {},在依赖项中,我使用了导航,位置层和mapbox库

repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://mapbox.bintray.com/mapbox' } 
}

    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.23.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.11.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.7.1'*

活动

 package com.example.pc_5.route_location_finder;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import java.util.List;
import static android.icu.lang.UCharacter.BidiPairedBracketType.NONE;
import static com.mapbox.mapboxsdk.location.modes.CameraMode.NONE_COMPASS;
import static com.mapbox.mapboxsdk.location.modes.CameraMode.TRACKING;
import static com.mapbox.mapboxsdk.location.modes.CameraMode.TRACKING_COMPASS;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,LocationEngineListener, PermissionsListener {

    private MapView mapView;
    private MapboxMap map;
    LocationEngine locationEngine;
    LocationLayerPlugin locationLayerPlugin;
    PermissionsManager permissionsManager;
    Location originLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this, "pk.eyJ1IjoiYWhtYWRuYXdheiIsImEiOiJjam95Y2ZsZ3EwYTcyM3hwNTFzdWtjZGVxIn0.5r3klhP7ROmdUYuRj7OhUw");
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);

        mapView.getMapAsync(MainActivity.this);
        mapView.onCreate(savedInstanceState);
    }
    void locationEnable() {

        if (PermissionsManager.areLocationPermissionsGranted(this)) {
            intialLocationEngine();
            intializLocationLayer();
        } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
        }


    }
    void intialLocationEngine() {
        locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
        locationEngine.setPriority(LocationEnginePriority.BALANCED_POWER_ACCURACY);
        locationEngine.activate();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location lastLocation = locationEngine.getLastLocation();
        if (lastLocation != null) {
            originLayout = lastLocation;
            setCamerpostion(lastLocation);
        }

        else {
            locationEngine.addLocationEngineListener(this);
        }
    }

    @SuppressLint("WrongConstant")
    void intializLocationLayer() {

        locationLayerPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
        locationLayerPlugin.setLocationLayerEnabled(true);
        locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
        locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
    }
    void setCamerpostion(Location camerpostion) {
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(camerpostion.getLatitude(), camerpostion.getLongitude()), 13.0));
    }

    //    @Override
    public void onConnected() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationEngine.requestLocationUpdates();
    }

    // @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            originLayout = location;
            setCamerpostion(location);
        }
    }

    // @Override
    public void onExplanationNeeded(List<String> permissionsToExplain) {
    }

    // @Override
    public void onPermissionResult(boolean granted) {
        if (granted) {
            locationEnable();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @SuppressWarnings("MissingPermission")
    @Override
    public void onStart() {
        super.onStart();
        if (locationEngine != null) {
            locationEngine.requestLocationUpdates();
        }
        if (locationLayerPlugin!=null)
        {
            locationLayerPlugin.onStart();
        }
        mapView.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();

        mapView.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();

        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (locationEngine!=null){
            locationEngine.removeLocationUpdates();
        }
        if (locationLayerPlugin!=null)
        {
            locationLayerPlugin.onStop();
        }

        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locationEngine != null) {
            locationEngine.deactivate();
            locationLayerPlugin.setLocationLayerEnabled(false);
        }
        mapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        map = mapboxMap;
        locationEnable();
       mapboxMap.getUiSettings().setZoomControlsEnabled(true);
       mapboxMap.getUiSettings().setZoomGesturesEnabled(true);
        mapboxMap.getUiSettings().setScrollGesturesEnabled(true);
        mapboxMap.getUiSettings().setAllGesturesEnabled(true);
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    >

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_cameraTargetLat="40.73581"
        mapbox:mapbox_cameraTargetLng="-73.99155"
        mapbox:mapbox_cameraZoom="3"
/>
</RelativeLayout>

0 个答案:

没有答案