Навигационный интерфейс MapBox для Android - PullRequest
0 голосов
/ 06 марта 2019

Я пытаюсь отследить маршрут и указать в приложении тип навигации "waze". Но после отслеживания маршрута, когда я нажимаю «начать навигацию» и происходит сбой приложения, появляется следующая ошибка:

Process: PID: 18052
java.lang.NoSuchFieldError: No static field navigationView of type I in class Lcom/mapbox/services/android/navigation/ui/v5/R$id; or its superclasses (declaration of 'com.mapbox.services.android.navigation.ui.v5.R$id' appears in /data/app/ep.class.class-1/split_lib_slice_9_apk.apk)
    at com.mapbox.services.android.navigation.ui.v5.MapboxNavigationActivity.onCreate(MapboxNavigationActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:6904)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)

Я уже просмотрел всю документацию по mapbox и не смог решить проблему! Мои коды:

test.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this, myKey);
    setContentView(R.layout.activity_test);
    getSupportActionBar().setTitle("Navegação");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mapView = findViewById(R.id.mapView);
    //mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(test.this);

}

@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;
    mapboxMap.setStyle(getString(R.string.navigation_guidance_day), new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {
            enableLocationComponent(style);

            addDestinationIconSymbolLayer(style);

            mapboxMap.addOnMapClickListener(test.this);
            button = findViewById(R.id.startButton);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean simulateRoute = true;
                    NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                            .directionsRoute(currentRoute)
                            .shouldSimulateRoute(true)
                            .build();
                    // Call this method with Context from within an Activity
                    NavigationLauncher.startNavigation(test.this, options);
                }
            });
        }
    });
}

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);
}

@SuppressWarnings( {"MissingPermission"})
@Override
public boolean onMapClick(@NonNull LatLng point) {

    Point destinationPoint = Point.fromLngLat(point.getLongitude(), point.getLatitude());
    Point originPoint = Point.fromLngLat(locationComponent.getLastKnownLocation().getLongitude(),
            locationComponent.getLastKnownLocation().getLatitude());

    GeoJsonSource source = mapboxMap.getStyle().getSourceAs("destination-source-id");
    if (source != null) {
        source.setGeoJson(Feature.fromGeometry(destinationPoint));
    }

    getRoute(originPoint, destinationPoint);
    button.setEnabled(true);
    button.setBackgroundResource(R.color.mapboxBlue);
    return true;
}

private void getRoute(Point origin, Point destination) {
    NavigationRoute.builder(this)
            .accessToken(Mapbox.getAccessToken())
            .origin(origin)
            .destination(destination)
            .build()
            .getRoute(new Callback<DirectionsResponse>() {
                @Override
                public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                    // You can get the generic HTTP info about the 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);

                    // Draw the route on the map
                    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());
                }
            });
}

@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
    // Check if permissions are enabled and if not request
    if (PermissionsManager.areLocationPermissionsGranted(this)) {
        // Activate the MapboxMap LocationComponent to show user location
        // Adding in LocationComponentOptions is also an optional parameter
        locationComponent = mapboxMap.getLocationComponent();
        locationComponent.activateLocationComponent(this, loadedMapStyle);
        locationComponent.setLocationComponentEnabled(true);
        // Set the component's camera mode
        locationComponent.setCameraMode(CameraMode.TRACKING);
    } else {
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}

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

@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
    //Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
}

@Override
public void onPermissionResult(boolean granted) {
    if (granted) {
        enableLocationComponent(mapboxMap.getStyle());
    } else {
        //Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
protected void onStart() {
    super.onStart();
    mapView.onStart();
}

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

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

@Override
protected void onStop() {
    super.onStop();
    mapView.onStop();
}

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

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

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

}

Построить Gradle

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//FireBase
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'com.google.firebase:firebase-auth:16.1.0'
//MapBox Depencies
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.1.2'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:0.2.0'
//implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.30.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.30.0'
//Button Login Dependencies Animation
implementation 'br.com.simplepass:loading-button-android:1.7.2'
//Cloud Messaging FireBase
implementation 'com.google.firebase:firebase-messaging:17.4.0'

}

...