关闭navigationview mapbox后的黑色窗口

时间:2018-08-06 11:08:07

标签: android mapbox

关闭navigationview后,加载地图时出现问题。 navigationview我正在以这种方式运行navigationview

NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                                .directionsRoute(currentRoute)
                                .build();

NavigationLauncher.startNavigation(SearchResultPage.this, options);

我介绍了代码外推的结果 view the route on the map

navigation view

result after the navigation menu is closed

这是用于调用导航视图的类

 public class SearchResultPage extends Activity implements OnMapReadyCallback,
            DialogInterface.OnDismissListener, MapboxMap.OnMarkerClickListener{

        private static final String TAG = SearchResultPage.class.toString();

        private MapView   mapView;
        private MapboxMap map;

        private UserLocation userLocation;

        private ImageView buttonBack;

        private Table  table;
        private String type;
        private int numberLocation;
        private ArrayList<Coordinates> coordinates;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Mapbox.getInstance(getApplicationContext(), getString(R.string.mapbox_access_token));

            setContentView(R.layout.activity_result_page);

            buttonBack = (ImageView) findViewById(R.id.button_back);
            buttonBack.setOnClickListener(buttonBackClickListener);

            mapView     = (MapView) findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);

            mapView.getMapAsync(this);
        }

        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            map = mapboxMap;

            userLocation = new UserLocation(this, mapView, map);
            userLocation.enableLocationPlugin();

            Intent intent     = getIntent();
            type = intent.getStringExtra(Keys.TYPE);
            coordinates = new ArrayList<>();

            table       = (Table) intent.getSerializableExtra(Keys.DATA);
            coordinates = table.coordinates;

            for(int i = 0; i < coordinates.size(); i++) {
                LatLng position = new LatLng(coordinates.get(i).latitude, coordinates.get(i).longitude);
                map.addMarker(new MarkerOptions().position(position).title(String.valueOf(i)));
                map.moveCamera(new CameraUpdateFactory().newLatLngZoom(position, 15));

                map.setOnMarkerClickListener(this);
            }
        }

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

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

        @Override
        protected void onDestroy() {
            super.onDestroy();
            mapView.onDestroy();
            if(userLocation != null)
                userLocation.disableLocationPlugin();

            if(navigationMapRoute != null) {
                navigationMapRoute.onStop();
            }
        }

        @Override
        public void onBackPressed() {
            if(userLocation != null)
                userLocation.disableLocationPlugin();

            if(navigationMapRoute != null) {
                navigationMapRoute.onStop();
            }
            finish();
        }

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

        View.OnClickListener buttonBackClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(userLocation != null)
                    userLocation.disableLocationPlugin();

                if(navigationMapRoute != null) {
                    navigationMapRoute.onStop();
                }
                finish();
            }
        };

        private Point originPosition;
        private Point destinationPosition;
        private DirectionsRoute currentRoute;
        private NavigationMapRoute navigationMapRoute;

        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            getRoute();
        }

        public void getRoute() {
            if(userLocation != null) {
                originPosition      = userLocation.getUserPosition();
                destinationPosition = Point.fromLngLat(table.coordinates.get(numberLocation).longitude,
                        table.coordinates.get(numberLocation).latitude);
            }

            if(originPosition == null) {
                Log.e(TAG, "user location is null");
                Message.Show(this, "Ошибка получения геоданных");
                return;
            }

            NavigationRoute.builder(this)
                    .accessToken(Mapbox.getAccessToken())
                    .origin(originPosition)
                    .destination(destinationPosition)
                    .profile(DirectionsCriteria.PROFILE_WALKING)
                    .build()
                    .getRoute(new Callback<DirectionsResponse>() {
                        @Override
                        public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {

                            if(response.body() == null) {
                                Log.e(TAG, "response body is null");
                                return;
                            }

                            if(response.body().routes().isEmpty()) {
                                Log.e(TAG, "rout not found");
                                Message.Show(SearchResultPage.this, "Маршрут не найден");
                                return;
                            }

                            Log.i(TAG, "route found");
                            currentRoute = response.body().routes().get(0);

                            if(navigationMapRoute != null) {
                                navigationMapRoute.removeRoute();
                            }
                            else {
                                navigationMapRoute = new NavigationMapRoute(null, mapView, map, R.style.CustomNavigationMapRoute);
                            }

                            navigationMapRoute.addRoute(currentRoute);

                            NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                                    .directionsRoute(currentRoute)
                                    .build();

                            NavigationLauncher.startNavigation(SearchResultPage.this, options);
                        }

                        @Override
                        public void onFailure(Call<DirectionsResponse> call, Throwable t) {
                            Log.i(TAG, "getting route is empty");
                            Message.Show(SearchResultPage.this, "getting route is empty");
                        }
                    });
        }

        @Override
        public boolean onMarkerClick(@NonNull Marker marker) {
            numberLocation = Integer.parseInt(marker.getTitle());

            InfoPage infoPage = new InfoPage();

            Bundle args = new Bundle();
            args.putString(Keys.TYPE, type);
            args.putSerializable(Keys.DATA, table);
            infoPage.setArguments(args);

            infoPage.show(getFragmentManager(), TAG);
            return true;
        }
    }

0 个答案:

没有答案