gpt4 book ai didi

android - 显示异常的地方自动完成 api

转载 作者:行者123 更新时间:2023-12-02 12:57:20 24 4
gpt4 key购买 nike

我正在使用 Places 自动完成 api,我面临的问题是,当我尝试通过键入搜索任何地方时,它显示错误为 ->

enter image description here

现在我的代码是

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val apiKey = getString(R.string.api_key)

if (!Places.isInitialized()) {
Places.initialize(
applicationContext,
apiKey
)
}

val placesClient =
Places.createClient(this)
gmap.setOnClickListener {
onSearchCalled();
//startActivity(Intent(this, Gmap::class.java))
}

fun onSearchCalled() { // Set the fields to specify which types of place data to return.
val fields: List<Place.Field> = Arrays.asList(
Place.Field.ID,
Place.Field.NAME,
Place.Field.ADDRESS,
Place.Field.LAT_LNG,
Place.Field.ADDRESS_COMPONENTS
)
// Start the autocomplete intent.
val intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields
).build(this)
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
}

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
val place =
Autocomplete.getPlaceFromIntent(data!!)
Log.i(
"aaaaa",
"Place: " + place.name + ", " + place.id
)
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) { // TODO: Handle the error.
val status =
Autocomplete.getStatusFromIntent(data!!)
Log.i("aaaa", status.statusMessage)
} else if (resultCode == Activity.RESULT_CANCELED) { // The user canceled the operation.
}
}
}

我相信我按照 Google Documentation 进行了所有操作
我还附加了 list 和 build.gradle 依赖项
implementation 'com.google.android.libraries.places:places:2.1.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'

最佳答案

Places SDK for Android 的 Google Play 服务版本是 deprecated 2019 年 1 月。所以首先,从你的 build.gradle 依赖项中删除这一行:

implementation 'com.google.android.gms:play-services-places:17.0.0'

其次,确保您拥有 billing在您的项目上启用,并且 Places API也被启用。

我刚刚测试了您的代码(使用我的有效 API key 并删除了上述 gms 依赖项),并且 Place Autocomplete 对我来说完美运行。希望这可以帮助!

关于android - 显示异常的地方自动完成 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59787814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com