簡體   English   中英

如何解決 Android Place Autocomplete 意圖問題?

[英]how to solve Android Place Autocomplete intent issue?

我正在嘗試在我的應用程序中使用 google Place Autocomplete

如文檔中所述,有兩種使用此功能的方法:一種使用片段,另一種方法是使用 Intent,這就是我選擇的方法。

我的代碼如下

Gradle:

implementation 'com.google.android.libraries.places:places:2.5.0'

Kotlin:

第1部分

 btn.setOnClickListener {

            if (!Places.isInitialized()) {
                Places.initialize(
                    requireActivity().applicationContext,
                    getString(R.string.google_maps_key)
                )
            }

            @Suppress("DEPRECATION")
            startActivityForResult(
                Autocomplete
                    .IntentBuilder(
                        AutocompleteActivityMode.OVERLAY,
                        listOf(
                            Place.Field.ID,
                            Place.Field.NAME,
                            Place.Field.LAT_LNG,
                            Place.Field.ADDRESS,
                            Place.Field.ADDRESS_COMPONENTS
                        )
                    ).build(requireContext()),
                AUTOCOMPLETE_REQUEST_CODE
            )
        }

第2部分

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            when (resultCode) {
                Activity.RESULT_OK -> {
                    data?.let {
                        val place = Autocomplete.getPlaceFromIntent(data)
                        Timber.i(TAG, "Place: ${place.name}, ${place.id}")
                    }
                }
                AutocompleteActivity.RESULT_ERROR -> {
                    // TODO: Handle the error.
                    data?.let {
                        val status = Autocomplete.getStatusFromIntent(data)
                        Timber.i(TAG, status.statusMessage)
                    }
                }
                Activity.RESULT_CANCELED -> {
                    // The user canceled the operation.
                }
            }
            return
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

疊加的“Place Auto Complete”視圖完美顯示,但是當我嘗試在鍵盤上敲擊以搜索某個位置時,視圖崩潰而不是應用程序。 只有視圖,我得到了下面的錯誤(我不知道它是否相關):

set_timerslack_ns write failed: Operation not permitted

知道如何解決這個問題嗎?

您是否“啟用”了您的 api 密鑰以使用 google places?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM