Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 25 #35

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open

Feature 25 #35

wants to merge 3 commits into from

Conversation

carlosolimpio
Copy link

No description provided.

Comment on lines 31 to 46
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
onLocationChanged()
}

}, Looper.myLooper())
}

private fun onLocationChanged() {
//var msg = "location changed: " + location?.latitude + ", " + location?.longitude
//Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()

mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location ->
locationListener.onLocationChanged(location)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is confusing. You're using the callback from the requestLocationUpdates to call the onLocationChanged function that defines another callback to call your callback. I don't really know now how to work with LocationServices so I don't know if you need both of "requestLocationUpdates" and "lasLocation.addOnSuccessListener" and I think you shouldn't define a listener inside the function that is called when the onLocationResult is called.

Make sure to read the documentation to have a better understanding of what each of these things do.
https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vinicius is partially right in my humble opinion. Allow me to explain:

requestLocationUpdates is for requesting further location updates, as configured by locationRequest object. As you coded it, location updates gotten from mFusedLocationProviderClient.requestLocationUpdates go to a locationCallback, and then you send to your onLocationChanged, which is correct

What's wrong is calling mFusedLocationProviderClient.lastLocation inside the onLocationChanged callback: a means to get the last known location (almost) immediately if it's available. Then you can save the location and do whatever you want on onLocationChanged

So this is the architecture:

mFusedLocationProviderClient.requestLocationUpdates _______.
mFusedLocationProviderClient.lastLocation _________________|____>    onLocationChanged 

To ways of getting the location, one single-shot, one recurring, both processed by onLocationChanged, where you should do what you want with the gotten location.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

        mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location ->
		onLocationChanged(location)
        }

        mLocationRequest.interval = UPDATE_INTERVAL
        mLocationRequest.fastestInterval = FASTEST_INTERVAL

        var builder = LocationSettingsRequest.Builder()
        builder.addLocationRequest(mLocationRequest)
        var locationSettingsRequest = builder.build()

        //checks if location settings are satisfied
        var settingsClient = LocationServices.getSettingsClient(mContext)
        settingsClient.checkLocationSettings(locationSettingsRequest)

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext)
        mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
            override fun onLocationResult(locationResult: LocationResult?) {
                onLocationChanged(locationResult.location)
            }

        }, Looper.myLooper())
    }

    private fun onLocationChanged(l: Location) {
        locationListener.onLocationChanged(location)
    }


// test
//test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comments


fun startLocationUpdates() {
//updates properties
mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer .apply{} in kotlin

mLocationRequest.apply {
    priority = tal
    interval = tal
    ...
}

Comment on lines 31 to 46
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
onLocationChanged()
}

}, Looper.myLooper())
}

private fun onLocationChanged() {
//var msg = "location changed: " + location?.latitude + ", " + location?.longitude
//Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()

mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location ->
locationListener.onLocationChanged(location)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vinicius is partially right in my humble opinion. Allow me to explain:

requestLocationUpdates is for requesting further location updates, as configured by locationRequest object. As you coded it, location updates gotten from mFusedLocationProviderClient.requestLocationUpdates go to a locationCallback, and then you send to your onLocationChanged, which is correct

What's wrong is calling mFusedLocationProviderClient.lastLocation inside the onLocationChanged callback: a means to get the last known location (almost) immediately if it's available. Then you can save the location and do whatever you want on onLocationChanged

So this is the architecture:

mFusedLocationProviderClient.requestLocationUpdates _______.
mFusedLocationProviderClient.lastLocation _________________|____>    onLocationChanged 

To ways of getting the location, one single-shot, one recurring, both processed by onLocationChanged, where you should do what you want with the gotten location.

@@ -10,5 +10,8 @@ object Constants {
//Value used for location permission
const val PERMISSION_REQUEST_LOCATION_CODE = 99

//maps camera default zoom
const val CAMERA_MAPS_ZOOM = 18.5f

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider making private

Comment on lines 31 to 46
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
onLocationChanged()
}

}, Looper.myLooper())
}

private fun onLocationChanged() {
//var msg = "location changed: " + location?.latitude + ", " + location?.longitude
//Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()

mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location ->
locationListener.onLocationChanged(location)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

        mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location ->
		onLocationChanged(location)
        }

        mLocationRequest.interval = UPDATE_INTERVAL
        mLocationRequest.fastestInterval = FASTEST_INTERVAL

        var builder = LocationSettingsRequest.Builder()
        builder.addLocationRequest(mLocationRequest)
        var locationSettingsRequest = builder.build()

        //checks if location settings are satisfied
        var settingsClient = LocationServices.getSettingsClient(mContext)
        settingsClient.checkLocationSettings(locationSettingsRequest)

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext)
        mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
            override fun onLocationResult(locationResult: LocationResult?) {
                onLocationChanged(locationResult.location)
            }

        }, Looper.myLooper())
    }

    private fun onLocationChanged(l: Location) {
        locationListener.onLocationChanged(location)
    }


override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't nullify the mContext on the onDetach() callback, it will leak.

Copy link

@psteiger psteiger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please nullify the Context at onDetach, or else it's a leak

import android.os.Looper
import com.google.android.gms.location.*

class LocationManager(val mContext: Context, private val locationListener: LocationListener) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not private val mContext?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants