mrmans0n / smart-location-lib

Android library project that lets you manage the location updates to be as painless as possible
1.65k stars 352 forks source link

Not able to get location in kotlin #238

Open pawarn opened 6 years ago

pawarn commented 6 years ago

Thanks for the great library

I have implemented code where I need location offline.

I have implemented same code in Java I am getting a location instantly when I enable GPS location (it envoke onLocationUpdated instantly) but in kotlin It not invoking as It is working in Java.

Declare Permission in Manifest. Also allow runtime permission.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Using SmarLocation Library. compile 'io.nlopez.smartlocation:library:3.3.3'

OnButton Click check location enables or not and as per conditions start Location

btn.setOnClickListener(View.OnClickListener { Log.e(TAG, "get Location click ")

        if(!SmartLocation.with(this).location().state().locationServicesEnabled()){
            Log.e(TAG, "btn click displayLocationSettingsRequest  ")
            displayLocationSettingsRequest(this)
        }else{
            Log.e(TAG, "btn click start SmartLocation  ")

            SmartLocation.with(this).location().start(this)

        }

    })

Implemented OnLocationUpdatedLister & print latitutde & longitude in Log.

override fun onLocationUpdated(location: Location?) {

    Log.e(TAG, "onLocationUpdated  ")
    if (location != null) {
        Log.e(TAG, "onLocationUpdated latitude " + location.getLatitude())
        Log.e(TAG, "onLocationUpdated longitude " + location.getLongitude())
        Log.e(TAG, "onLocationUpdated provider " + location.getProvider())

    }

}

Is there I am missing something in this code?

Same way I have implemented in Java it works perfectly. But in kotlin it didn't provide callback for onUpdateLocation(location: Location) method.

spidgorny commented 6 years ago

If you target a newer Android platform, you should ask a user for access to their location explicitly. https://developer.android.com/training/permissions/requesting