Mapbox导航ondidfinishloadingstyle异常

时间:2019-02-20 14:35:02

标签: android mapbox illegalargumentexception mapbox-android

尝试使用本教程https://docs.mapbox.com/help/tutorials/android-navigation-sdk/#customize-the-style

在Android上构建导航

但是它抛出了这个异常 ex

当我按下导航按钮时。道路显示正确,但仅使用google maps api获取我的位置。

谢谢您的建议。

我的代码:

`   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        Mapbox.getInstance(this, getString(R.string.access_token2));
        getLocationPermission();
        manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        setContentView(R.layout.activity_nav);
        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        getDeviceLocation();

        mapView.getMapAsync(this);
    }
    private void getDeviceLocation(){
        Log.d(TAG, "getDeviceLoc");
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }
        FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        try{
            if(mLocationPermissionsGranted){
                final Task location = mFusedLocationProviderClient.getLastLocation();
                location.addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        curLoc = (Location) task.getResult();
                        Log.d(TAG, "found location" + curLoc);
                    }
                });
            }
        }catch (SecurityException e){
            Timber.e("getDeviceLocation: SecurityException: %s", e.getMessage());
        }
    }


    private void getLocationPermission(){
        Log.d(TAG, "getLocationPermission");
        String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION};

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED){
            Timber.d("Can get FINE LOCATION");
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED){
                Timber.d("Can get COARSE LOCATION");
                mLocationPermissionsGranted = true;
            }else{
                Timber.d("Can't get COARSE LOCATION");
                ActivityCompat.requestPermissions(this, permissions,
                        LOCATION_PERMISSION_REQUEST_CODE);
                ActivityCompat.requestPermissions(this,new String[]{
                        Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            }
        }else{
            Timber.d("Can't get FINE LOCATION");
            ActivityCompat.requestPermissions(this, permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
            ActivityCompat.requestPermissions(this,new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        Log.d(TAG, "onRequest");
        mLocationPermissionsGranted = false;

        switch(requestCode){
            case LOCATION_PERMISSION_REQUEST_CODE:{
                if(grantResults.length > 0){
                    for (int grantResult : grantResults) {
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
                            mLocationPermissionsGranted = false;
                            Log.d(TAG, "permission failed");
                            return;
                        }
                    }
                    Log.d(TAG, "permission granted");
                    mLocationPermissionsGranted = true;
                }
            }
        }
    }
    private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false)
                .setPositiveButton("Yes", (dialog, id) -> startActivity(new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)))
                .setNegativeButton("No", (dialog, id) -> dialog.cancel());
        final AlertDialog alert = builder.create();
        alert.show();
    }


    @Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {
        this.mapboxMap = mapboxMap;
        LatLng origin = new LatLng(curLoc.getLatitude(), curLoc.getLongitude());
        CameraPosition position = new CameraPosition.Builder()
                .target(origin)
                .zoom(15)
                .tilt(20)
                .build();
        mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 5000);
        mapboxMap.setStyle(getString(R.string.navigation_guidance_day), new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {
                addDestinationIconSymbolLayer(style);
                Bundle data = getIntent().getExtras();
                Places place = data.getParcelable("place");
                LatLng destination = new LatLng(place.getLocation().getLatitude(),
                        place.getLocation().getLongitude());
                Point destinationPoint = Point.fromLngLat(destination.getLongitude(),
                        destination.getLatitude());
                Point originPoint = Point.fromLngLat(curLoc.getLongitude(), curLoc.getLatitude());
                GeoJsonSource source = mapboxMap.getStyle().getSourceAs("destination-source-id");
                if (source != null) {
                    source.setGeoJson(Feature.fromGeometry(destinationPoint));
                }
                getRoute(originPoint, destinationPoint);
                button = findViewById(R.id.startButton);
                button.setOnClickListener(v -> {
                    setContentView(R.layout.activity_navigation);
                    boolean simulateRoute = true;
                    NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                            .directionsRoute(currentRoute)
                            .shouldSimulateRoute(simulateRoute)
                            .build();

                    NavigationLauncher.startNavigation(Navigate.this, options);
                });
                button.setEnabled(true);

            }
        });
    }

    private void addDestinationIconSymbolLayer(@NonNull Style loadedMapStyle) {
        loadedMapStyle.addImage("destination-icon-id",
                BitmapFactory.decodeResource(this.getResources(), R.drawable.mapbox_marker_icon_default));
        GeoJsonSource geoJsonSource = new GeoJsonSource("destination-source-id");
        loadedMapStyle.addSource(geoJsonSource);
        SymbolLayer destinationSymbolLayer = new SymbolLayer("destination-symbol-layer-id",
                "destination-source-id");
        destinationSymbolLayer.withProperties(
                iconImage("destination-icon-id"),
                iconAllowOverlap(true),
                iconIgnorePlacement(true)
        );
        loadedMapStyle.addLayer(destinationSymbolLayer);
    }

    private void getRoute(Point origin, Point destination) {
        Log.d(TAG, "getRoute");
        NavigationRoute.builder(this)
                .accessToken(getString(R.string.access_token2))
                .origin(origin)
                .destination(destination)
                .build()
                .getRoute(new Callback<DirectionsResponse>() {
                    @Override
                    public void onResponse(Call<DirectionsResponse> call,
                                           Response<DirectionsResponse> response) {
                        Log.d(TAG, "Response code: " + response.code());
                        if (response.body() == null) {
                            Log.e(TAG, "No routes found, make sure you set the right user and access token.");
                            return;
                        } else if (response.body().routes().size() < 1) {
                            Log.e(TAG, "No routes found");
                            return;
                        }

                        currentRoute = response.body().routes().get(0);

                        if (navigationMapRoute != null) {
                            navigationMapRoute.removeRoute();
                        } else {
                            navigationMapRoute = new NavigationMapRoute(null, mapView,
                                    mapboxMap, R.style.NavigationMapRoute);
                        }
                        navigationMapRoute.addRoute(currentRoute);
                    }

                    @Override
                    public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
                        Log.e(TAG, "Error: " + throwable.getMessage());
                    }
                });
    }

`

0 个答案:

没有答案