我正在从NETWORK_PROVIDER获取位置,如下所示:
if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
val locationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
// Define a listener that responds to location updates
val locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
// Called when a new location is found by the network location provider.
if (location != null) {
lastLocation = location
currentLatLng = LatLng(location.latitude, location.longitude)
if (currentLatLng != null &&
currentLatLng.latitude != null &&
currentLatLng.longitude != null
) {
sharedPreferenceManager?.setStringData(
Constant.PrefKey.userLatitude,
"" + currentLatLng.latitude
)
sharedPreferenceManager?.setStringData(
Constant.PrefKey.userLongitude,
"" + currentLatLng.longitude
)
getAddress(currentLatLng)
}
}else{
ToastUtil.displayLongDurationToast(mContext,"Could not get Location. Please turn on GPS and try again.")
getAndSaveCurrentLocation()
}
}
}
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
但是,方法requestLocationUpdates的最后一行却显示错误为:
以下任何一个函数都不能用参数调用提供
可能是什么问题?我已经传递了有效的参数。
编辑:在此行获取参数错误:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
[很可能您导入了错误的LocationListener。您可能正在使用com.google.android.gms.location.LocationListener
但是您需要导入android.location.LocationListener
检查您的导入并将导入更改为正确的LocationListener,并且该调用应该可以正常工作。