简体   繁体   English

Mapbox Map 未显示在 android 上

[英]Mapbox Map not showing on android

When I tested it the first couple of times it was showing perfectly well, then I added some code and it just stopped.当我在前几次测试它时,它显示得非常好,然后我添加了一些代码,它就停止了。 It still shows the Mapbox logo on the left bottom screen but it's not loading the map.它仍然在左下角显示 Mapbox 标志,但没有加载 map。 Here's the code on the MapActivity :这是MapActivity上的代码:

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

    private MapView mapView;
    private Button startButton;

    private MapboxMap map;
    private PermissionsManager permissionsManager;
    private LocationEngine locationEngine;
    private Location originLocation;
    private LocationLayerPlugin locationLayerPlugin;
    private Locash customerLocash;
    private Point destinationPosition;
    private Point originPosition;
    private Marker destinationMarker;
    private NavigationMapRoute navigationMapRoute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this, getString(R.string.access_token));
        setContentView(R.layout.activity_map);
        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);


        startButton = findViewById(R.id.startButton);

        Intent activityIntent = getIntent();
        String locationJson = activityIntent.getStringExtra("location");

        Gson gson = new Gson();

        customerLocash = gson.fromJson(locationJson,Locash.class);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Launch Navigation
                NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                        .origin(originPosition)
                        .destination(destinationPosition)
                        .shouldSimulateRoute(true)
                        .build();
                NavigationLauncher.startNavigation(MapActivity.this, options);
            }
        });

    }

    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        map = mapboxMap;
        enableLocation();

        LatLng point = new LatLng(customerLocash.getLongitude(),customerLocash.getLatitude());

        destinationMarker = map.addMarker(new MarkerOptions().position(point));

        destinationPosition = Point.fromLngLat(point.getLongitude(),point.getLatitude());
        originPosition = Point.fromLngLat(originLocation.getLongitude(), originLocation.getLatitude());
        getRoute(originPosition, destinationPosition);

        startButton.setEnabled(true);

    }

    private void enableLocation() {
        if (PermissionsManager.areLocationPermissionsGranted(this)){ 
            initializeLocationEngine();
            initializeLocationLayer();
            
        } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
        }

    }

    @SuppressWarnings("MissingPermission")
    private void initializeLocationEngine() {
        locationEngine = new LocationEngineProvider(this)
                .obtainBestLocationEngineAvailable();
        locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
        locationEngine.activate();

        Location lastLocation = locationEngine.getLastLocation();
        if (lastLocation != null){
            originLocation = lastLocation;
            setCameraPosition(lastLocation);
        } else {
            locationEngine.addLocationEngineListener(this);
        }
    }

    @SuppressWarnings("MissingPermission")
    private void initializeLocationLayer() {
        locationLayerPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
        locationLayerPlugin.setLocationLayerEnabled(true);
        locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
        locationLayerPlugin.setRenderMode(RenderMode.NORMAL);

    }

    private void setCameraPosition(Location location){
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),
                location.getLongitude()),13.0));
    }

    private void getRoute(Point origin, Point destination){
        NavigationRoute.builder()
                .accessToken(Mapbox.getAccessToken())
                .origin(origin)
                .destination(destination)
                .build()
                .getRoute(new Callback<DirectionsResponse>() {
                    @Override
                    public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                        if (response.body()  == null){
                            Toast.makeText(MapActivity.this, "No Routes Found", Toast.LENGTH_SHORT).show();
                            return;
                        } else if (response.body().routes().size() == 0){
                            Toast.makeText(MapActivity.this, "No Routes Found", Toast.LENGTH_SHORT).show();
                            return;
                        }

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

                        navigationMapRoute = new NavigationMapRoute(null, mapView,map);
                        navigationMapRoute.addRoute(currentRoute);
                    }

                    @Override
                    public void onFailure(Call<DirectionsResponse> call, Throwable t) {

                        Toast.makeText(MapActivity.this, "Error:" + t.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });
    }

    @SuppressWarnings("MissingPermission")
    @Override
    public void onConnected() {
        locationEngine.requestLocationUpdates();
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null){
            originLocation = location;
            setCameraPosition(location);
        }

    }

    @Override
    public void onExplanationNeeded(List<String> permissionsToExplain) {
        Toast.makeText(this, "Location Required", Toast.LENGTH_SHORT).show();
    }

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

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

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

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

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

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

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mapView.onSaveInstanceState(outState);
    }

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

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

Oh and the Locash object is a class I use to store user location on firebase and the likes.哦,洛卡什 object 是一个 class 我用来在 firebase 等上存储用户位置。

Looks like a problem I had.看来我遇到了问题。 Not sure if this is the same, but in my case setting the textureView option to true solved it.不确定这是否相同,但在我的情况下,将textureView选项设置为true解决了它。

This is how I did it in code:这就是我在代码中的做法:

val resourceOptions = ResourceOptions.Builder()
            .accessToken(context.getString(R.string.mapbox_access_token))
            .build()

mapView = MapView(context, MapInitOptions(context, resourceOptions).apply { textureView = true })

I am not sure how to do it in XML.我不确定如何在 XML 中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Mapbox NavigationView 未加载地图 - Android Mapbox NavigationView is not loading map 在 Android 上的 Mapbox MapSnapshotter 中设置地图注释 - Setting map annotations in Mapbox MapSnapshotter on Android 如何从 Android 上的地图框 map 中删除线串 - How to remove a Linestring from a mapbox map on Android Android在地图上显示用户 - Android showing users on Map 在 Android Studio 上的 Kotlin 中使用 Mapbox 实现 Geojson 点以映射和定位我 - Implement Geojson points to map and locate me with Mapbox, in Kotlin on Android Studio MapBox Android SDK:如何在应用安装过程中下载离线地图? - MapBox Android SDK: How to download offline map during app installation? 在 Android Studio [Java] 中加载 mapbox map 后的绘图标记 - Drawing markers after mapbox map loads in Android Studio [Java] android 的 Mapbox:当我导入 markerview 依赖项时,我原来的 mapbox map 代码停止工作 - Mapbox for android: when I import markerview dependancy, my original mapbox map code stops working Android Google Maps未显示实际地图 - Android google maps not showing the actual map 如何解决android studio中不显示地图方向? - How to solve the map direction is not showing in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM