Mapbox在android中显示错误的当前位置

时间:2018-06-04 11:07:23

标签: mapbox mapbox-android

我正在使用Mapbox sdk用于安卓。 我想得到当前的位置,我也得到了,但问题是:

我现在错误的当前位置,这是在尼日利亚附近。 任何人都可以帮助我获得完美的位置吗?

这是我的gradle文件的详细信息:

    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.app.mapboxdemo"
            minSdkVersion 15
            targetSdkVersion 27
            multiDexEnabled true
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        //noinspection GradleCompatible
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support:design:27.1.1'
        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'
        implementation 'com.intuit.ssp:ssp-android:1.0.5'
        implementation 'com.intuit.sdp:sdp-android:1.0.5'
        implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:6.1.3@aar') {
            transitive = true
        }
        implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.5.3'
        implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-cluster-utils:0.3.0'
        implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.13.0'
        implementation('com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.13.0') {
            transitive = true
        }
    }

以下是permisssions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-feature android:name="android.hardware.location.gps"/>

这是获取用户位置的代码:

    package com.app.mapboxdemo;// classes needed to initialize map

    import com.mapbox.api.geocoding.v5.MapboxGeocoding;
    import com.mapbox.mapboxsdk.Mapbox;
    import com.mapbox.mapboxsdk.maps.MapView;

    // classes needed to add location layer
    import com.mapbox.geojson.Point;
    import com.mapbox.mapboxsdk.maps.MapboxMap;
    import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

    import android.location.Location;

    import com.mapbox.mapboxsdk.geometry.LatLng;

    import android.os.Bundle;
    import android.support.annotation.NonNull;

    import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
    import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
    import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
    import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
    import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions;
    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;

    // classes needed to add a marker
    import com.mapbox.mapboxsdk.annotations.Marker;
    import com.mapbox.mapboxsdk.annotations.MarkerOptions;

    // classes to calculate a route
    import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
    import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
    import com.mapbox.api.directions.v5.models.DirectionsResponse;
    import com.mapbox.api.directions.v5.models.DirectionsRoute;

    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;

    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;

    // classes needed to launch navigation UI
    import android.view.View;
    import android.widget.Button;

    import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher;


    // classes needed to add location layer
    import com.mapbox.geojson.Point;
    import com.mapbox.mapboxsdk.maps.MapboxMap;
    import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

    import android.location.Location;

    import com.mapbox.mapboxsdk.geometry.LatLng;

    import android.support.annotation.NonNull;

    import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
    import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
    import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
    import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions;
    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;


    // classes needed to add a marker
    import com.mapbox.mapboxsdk.annotations.Marker;
    import com.mapbox.mapboxsdk.annotations.MarkerOptions;


    // classes to calculate a route
    import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
    import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
    import com.mapbox.api.directions.v5.models.DirectionsResponse;
    import com.mapbox.api.directions.v5.models.DirectionsRoute;

    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;

    import android.util.Log;


    // classes needed to launch navigation UI
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher;

    import java.util.List;


    public class NavigationActivity extends AppCompatActivity implements LocationEngineListener, PermissionsListener {


        private MapView mapView;


        // variables for adding location layer
        private static MapboxMap map;
        private PermissionsManager permissionsManager;
        private LocationLayerPlugin locationPlugin;
        private LocationEngine locationEngine;
        private Location originLocation;


        // variables for adding a marker
        private Marker destinationMarker;
        private LatLng originCoord;
        private LatLng destinationCoord;


        // variables for calculating and drawing a route
        private Point originPosition;
        private Point destinationPosition;
        private DirectionsRoute currentRoute;
        private static final String TAG = "DirectionsActivity";
        private NavigationMapRoute navigationMapRoute;

        EditText edtLatitude, edtLongitude;


        private Button button, buttonGetLocation;


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


            mapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(final MapboxMap mapboxMap) {


                    map = mapboxMap;
                    enableLocationPlugin();


                    originCoord = new LatLng(originLocation.getLatitude(), originLocation.getLongitude());
                    /*mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
                        @Override
                        public void onMapClick(@NonNull LatLng point) {
                            if (destinationMarker != null) {
                                mapboxMap.removeMarker(destinationMarker);
                            }
                            destinationCoord = point;
                            destinationMarker = mapboxMap.addMarker(new MarkerOptions()
                                .position(destinationCoord)
                            );

                            destinationPosition = Point.fromLngLat(destinationCoord.getLongitude(), destinationCoord.getLatitude());
                            originPosition = Point.fromLngLat(originCoord.getLongitude(), originCoord.getLatitude());
                            getRoute(originPosition, destinationPosition);


                            button.setEnabled(true);
                            button.setBackgroundResource(R.color.mapboxBlue);


                        }

                    });*/


                    button = findViewById(R.id.startButton);
                    edtLatitude = findViewById(R.id.edtLatitude);
                    edtLongitude = findViewById(R.id.edtLongitude);

                    buttonGetLocation = findViewById(R.id.buttonGetLocation);
                    buttonGetLocation.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e(TAG, "buttonGetLocation onClick: " );
                            checkLatLong(mapboxMap);
                        }
                    });
                    button.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            Point origin = originPosition;
                            Point destination = destinationPosition;

                            boolean simulateRoute = true;
                            NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                                .origin(origin)
                                .destination(destination)
                                .shouldSimulateRoute(simulateRoute)
                                .build();
                            Log.e(TAG, "onClick: Start Button");
                            // Call this method with Context from within an Activity
                            NavigationLauncher.startNavigation(NavigationActivity.this, options);
                        }
                    });


                }
            });


        }


        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) {
                        // 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, map, 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 enableLocationPlugin() {
            // Check if permissions are enabled and if not request
            if (PermissionsManager.areLocationPermissionsGranted(this)) {

                initializeLocationEngine();
                locationPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
                locationPlugin.setLocationLayerEnabled(true);
                locationPlugin.setCameraMode(CameraMode.TRACKING);
                locationPlugin.setRenderMode(RenderMode.COMPASS);

            } else {
                permissionsManager = new PermissionsManager(this);
                permissionsManager.requestLocationPermissions(this);
            }
        }

        @SuppressWarnings({"MissingPermission"})
        private void initializeLocationEngine() {
            LocationEngineProvider locationEngineProvider = new LocationEngineProvider(this);
            locationEngineProvider.obtainLocationEngineBy(LocationEngine.Type.ANDROID);
            locationEngine = locationEngineProvider.obtainBestLocationEngineAvailable();
            locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
            locationEngine.activate();
            locationEngine.requestLocationUpdates();


            Location lastLocation = locationEngine.getLastLocation();
    //        Log.e(TAG, "initializeLocationEngine: Latitude is"+lastLocation.getLatitude()+" Longitude is "+lastLocation.getLongitude() );
            if (lastLocation != null) {
                originLocation = lastLocation;
                setCameraPosition(lastLocation);
            } else {
                locationEngine.addLocationEngineListener(this);
            }
        }

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

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

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

        }

        @Override
        public void onPermissionResult(boolean granted) {
            if (granted) {
                enableLocationPlugin();
            } else {
                finish();
            }
        }

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

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

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

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

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

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

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

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

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



        public void checkLatLong(MapboxMap mapboxMap) {
            if ((edtLatitude.getText() == null && edtLatitude.getText().length() == 0) && (edtLongitude.getText() == null && edtLongitude.getText().length() == 0)) {
                Toast.makeText(getApplicationContext(), "Enter Proper Longitude and Latitude.", Toast.LENGTH_LONG).show();
            } else {
                if (destinationMarker != null) {
                    map.removeMarker(destinationMarker);
                }
                destinationCoord = new LatLng(Double.parseDouble(edtLatitude.getText().toString()), Double.parseDouble(edtLongitude.getText().toString()));
                destinationMarker = map.addMarker(new MarkerOptions()
                    .position(destinationCoord)
                );
                Log.e(TAG, "checkLatLong: ");
                destinationPosition = Point.fromLngLat(destinationCoord.getLongitude(), destinationCoord.getLatitude());
                originPosition = Point.fromLngLat(originCoord.getLongitude(), originCoord.getLatitude());
                getRoute(originPosition, destinationPosition);
                setCameraPosition(originLocation);
                button.setEnabled(true);
                button.setBackgroundResource(R.color.mapboxBlue);
            }
        }

    }

请帮助我获取用户位置。 感谢帮助,我提到了很多问题,但没有得到任何解决方案。 而且,也提出任何有关提出问题的帮助。

1 个答案:

答案 0 :(得分:0)

我通过对方法initializeLocationEngine()enableLocationPlugin()

进行一些更改来解决这个问题

以下是经过修改的答案方法:

    private void enableLocationPlugin() {
        // Check if permissions are enabled and if not request
        if (PermissionsManager.areLocationPermissionsGranted(this)) {

            initializeLocationEngine();
            locationPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
            locationPlugin.setLocationLayerEnabled(true);
            locationPlugin.setCameraMode(CameraMode.TRACKING);
            locationPlugin.setRenderMode(RenderMode.COMPASS);
            getLifecycle().addObserver(locationPlugin);
            Log.e(TAG, "enableLocationPlugin:Permission Granted" );

        } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
            Log.e(TAG, "enableLocationPlugin:Permission Not Granted" );

        }
    }



    private void initializeLocationEngine() {
        locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
        locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
        locationEngine.addLocationEngineListener(this);
        locationEngine.activate();
        Location lastLocation = locationEngine.getLastLocation();
        if (lastLocation != null) {
            setCameraPosition(lastLocation);
        } else {
            locationEngine.addLocationEngineListener(this);
        }
    }