使用 Places SDK for Android,您可以探索当前报告的设备位置处的地点。地点示例包括本地商家、地图注点和地理位置。
权限
如需使用该库,您无需在应用清单中声明任何其他权限,因为该库会在其清单中声明其使用的所有权限。不过,如果您的应用使用 PlacesClient.findCurrentPlace()
,您必须在运行时请求位置信息权限。
如果您的应用不使用 PlacesClient.findCurrentPlace()
,请在清单中添加以下内容,以明确移除该库引入的 ACCESS_FINE_LOCATION
和 ACCESS_COARSE_LOCATION
权限:
<manifest ... xmlns:tools="http://schemas.android.com/tools"> ... <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove"/> ... </manifest>
详细了解 权限,并考虑使用 EasyPermissions 来上手使用。
获取当前位置
如需查找设备当前所在的本地商家或其他地点,请按以下步骤操作:
- 调用
ContextCompat.checkSelfPermission
以检查用户是否已授予访问其设备位置信息的权限。您的应用还必须包含用于向用户提示权限并处理结果的代码。 如需了解详情,请参阅请求应用权限。 - 创建
FindCurrentPlaceRequest
,传递Place.Field
的List
,指定应用应请求的地点数据类型。 - 调用
PlacesClient.findCurrentPlace()
,并传递您在上一步中创建的FindCurrentPlaceRequest
。 - 从
FindCurrentPlaceResponse
获取PlaceLikelihood
列表。
这些字段与地点搜索结果相对应,而且分为三个结算类别:基本、联系人和氛围。“基本”字段按基本费率结算,且不会产生额外费用。“联系人”和“氛围”字段按更高的费率结算。如需详细了解地点数据请求的结算方式,请参阅用量和结算。
该 API 会在 Task
中返回 FindCurrentPlaceResponse
。
FindCurrentPlaceResponse
包含一个 PlaceLikelihood
对象列表,表示设备可能位于的位置。对于每个地点,结果中都会显示该地点是正确地点的可能性。如果没有与给定设备位置对应的已知地点,则列表可能为空。
您可以调用 PlaceLikelihood.getPlace()
检索 Place
对象,并调用 PlaceLikelihood.getLikelihood()
获取地点的可能性评分。值越高,相应地点是最佳匹配的可能性就越大。
以下代码示例会检索设备最有可能位于的地点列表,并记录每个地点的名称和可能性。
// Use fields to define the data types to return. val placeFields: List<Place.Field> = listOf(Place.Field.NAME) // Use the builder to create a FindCurrentPlaceRequest. val request: FindCurrentPlaceRequest = FindCurrentPlaceRequest.newInstance(placeFields) // Call findCurrentPlace and handle the response (first check that the user has granted permission). if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { val placeResponse = placesClient.findCurrentPlace(request) placeResponse.addOnCompleteListener { task -> if (task.isSuccessful) { val response = task.result for (placeLikelihood: PlaceLikelihood in response?.placeLikelihoods ?: emptyList()) { Log.i( TAG, "Place '${placeLikelihood.place.name}' has likelihood: ${placeLikelihood.likelihood}" ) } } else { val exception = task.exception if (exception is ApiException) { Log.e(TAG, "Place not found: ${exception.statusCode}") } } } } else { // A local method to request required permissions; // See https://developer.android.com/training/permissions/requesting getLocationPermission() }
// Use fields to define the data types to return. List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME); // Use the builder to create a FindCurrentPlaceRequest. FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields); // Call findCurrentPlace and handle the response (first check that the user has granted permission). if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request); placeResponse.addOnCompleteListener(task -> { if (task.isSuccessful()){ FindCurrentPlaceResponse response = task.getResult(); for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) { Log.i(TAG, String.format("Place '%s' has likelihood: %f", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood())); } } else { Exception exception = task.getException(); if (exception instanceof ApiException) { ApiException apiException = (ApiException) exception; Log.e(TAG, "Place not found: " + apiException.getStatusCode()); } } }); } else { // A local method to request required permissions; // See https://developer.android.com/training/permissions/requesting getLocationPermission(); }
关于可能性值的说明:
- 该概率表示相应地点在单个请求的返回地点列表中是最佳匹配项的相对概率。您无法比较不同请求的可能性。
- 可能性的值介于 0.0 到 1.0 之间。
例如,若要表示正确地点是地点 A 的概率为 55%,是地点 B 的概率为 35%,则响应包含两个成员,地点 A 的概率为 0.55,地点 B 的概率为 0.35。
在应用中显示提供方说明
当应用显示从 PlacesClient.findCurrentPlace()
获取的信息时,还必须显示提供方信息。请参阅提供方说明文档。