-1

I am trying to get the lastlocation in my android app.This is doing by the help of mapbox in my app.But i cant import the "import com.mapbox.mapboxsdk.location.LocationServices;"

apply plugin: 'com.android.application'

dependencies { compile group: 'com.mapbox.mapboxsdk', name: 'mapbox-android-navigation', version: '0.1.0'

compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:3.2.0@aar') {
    transitive = true
}
compile ('com.mapbox.mapboxsdk:mapbox-android-directions:1.0.0@aar'){
    transitive=true
}
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar'){
    transitive=true
}
compile ('com.mapbox.mapboxsdk:mapbox-android-services:2.1.0@aar'){
    transitive=true
}
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:4.0.0@aar') {
    transitive = true
}

}

Aju
  • 1
  • 1

1 Answers1

0

I'm using the following code to get the last Location in my app:

public static Location getLastKnownLocation(Context context) {
    try {
        LocationManager mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        List<String> providers = mLocationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = mLocationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                // Found best last known location: %s", l);
                bestLocation = l;
            }
        }
        return bestLocation;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    }
}

This code still needs an improvement: it is better to ask for the permission instead of catching the Security Exception: see this stackoverflow thread

Don't forget to add the required permissions on the manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Community
  • 1
  • 1
Pedro Hidalgo
  • 861
  • 10
  • 15