根据地点ID集成Google Fetch Places-Android

时间:2019-04-15 18:03:23

标签: android google-maps maps

我正在使用PlacePicker(我想现在称为Places)设置应用程序,以便用户在下达订单时选择位置。

我已经有了依赖项和代码,但是当我按下按钮来获取位置时,什么也没有发生。我已经在我的应用程序中添加了API密钥,并向该项目添加了应用程序ID,但我只是无法理解发生了什么。

下面是我启动位置选择器活动的代码:

Places.initialize(mainActivity!!.applicationContext, getString(R.string.google_api_key))
            val placesClient = Places.createClient(mainActivity!!)
            // Define a Place ID.
            val placeId = "ChIJRcbZaklDXz4RYlEphFBu5r0"
            // Specify the fields to return.
            val placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME)
            // Construct a request object, passing the place ID and fields array.
            val request = FetchPlaceRequest.builder(placeId, placeFields)
                .build()
            placesClient.fetchPlace(request).addOnSuccessListener { response ->
                val place = response.place
                Log.i("TAG", "Place found: " + place.name!!)
            }.addOnFailureListener { exception ->
                Log.e("TAG", "Place not found: " )
                if (exception is ApiException) {
                    val statusCode = exception.statusCode
                    // Handle error with given status code.
                    Log.e("TAG", "Place not found: " + statusCode)
                }
            }

我认为这段代码应该打开一个用户可以选择位置的活动。

0 个答案:

没有答案