-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: dev
Are you sure you want to change the base?
Feature 25 #35
Conversation
abe8835
to
d889207
Compare
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) | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
...
}
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) | ||
} | ||
} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider making private
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) | ||
} | ||
} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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?
No description provided.