title: Mapbox shows wrong current location in android
body: <p>I am using <code>Mapbox</code> <code>sdk</code> for android.
  I want to get current location,and i am also getting,but the problem is:</p>
  
  <p>I am getting wrong current location,which is near Nigeria.
  can anyone help me to get perfect location??</p>
  
  <p>here is my <code>gradle</code> file's details:</p>
  
  <pre><code>    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
          }
      }
  </code></pre>
  
  <p>and here are the permisssions:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt;
  &lt;uses-feature android:name="android.hardware.location.gps"/&gt;
  </code></pre>
  
  <p>and here is my code for getting user location:</p>
  
  <pre><code>    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&lt;DirectionsResponse&gt;() {
                      @Override
                      public void onResponse(Call&lt;DirectionsResponse&gt; call, Response&lt;DirectionsResponse&gt; 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() &lt; 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&lt;DirectionsResponse&gt; 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&lt;String&gt; 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 &amp;&amp; edtLatitude.getText().length() == 0) &amp;&amp; (edtLongitude.getText() == null &amp;&amp; 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);
              }
          }
  
      }
  </code></pre>
  
  <p>please help me to get user location.
  Help is appreciated,i referred a lot of questions but didnt get any solution.
  than also,suggest any help in reffering questions too.</p>