问题描述
在我的应用中,我希望用户能够保存他们的粗略位置,其中包含城市、州/县和国家/地区的名称。
有两种方法可以使用 Places SDK for Android 或通过设备的 GPS 搜索城市。当用户从 Google Maps Places API 中选择一个城市时,即使是所选位置的 ID 也会被检索,这便于交叉引用来自同一位置的用户,但是当通过设备的位置设置选择设备位置时,则没有位置身份证。
那么有没有一种方法可以通过 Android 的设备位置获取当前位置的 ID,该 ID 与使用 Google Places API 检索到的 ID 相同
以下是我使用 Places SDK for Android 检索用户手动选择位置的方法:
val placesClient = createClient(this)
// Use the builder to create a FindAutocompletePredictionsRequest.
val request =
FindAutocompletePredictionsRequest.builder()
.setTypeFilter(typeFilter)
.setSessionToken(token)
.setQuery(searchQuery)
.build()
placesClient.findAutocompletePredictions(request)
.addOnSuccessListener { response: FindAutocompletePredictionsResponse
val predictions = response.autocompletePredictions
for (prediction in predictions) {
val placeId = prediction.placeId // Here is the PlaceId from Google Maps
}
}
通过 GPS 使用 Android 设备位置获取位置:
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val location = if ( ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
locationManager.getLastKNownLocation(LocationManager.NETWORK_PROVIDER)
} else {
null
}
val gcd = Geocoder(baseContext,Locale.getDefault())
location?.let {
val addresses = gcd.getFromLocation(location.latitude,location.longitude,1)[0]
addresses.
val city = addresses.locality
val state = addresses.adminArea
val country = addresses.countryName
//where is the PlaceId??
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)