Android Kotlin中Google Places API无法识别Pharmacy(药店)类型的问题求助
Hey there! Let's break down why you're not seeing the Pharmacy type in your placeTypes list, and how to fix this for good.
Why isn't PlaceTypes.PHARMACY showing up?
The issue lies with the FindCurrentPlace API you're using right now. This API is designed for quickly fetching places near your device, but it only returns a limited set of common place types (like Museum, Cafe, Restaurant) in the placeTypes field. Unfortunately, PHARMACY isn't included in that default supported list—even though you can see pharmacies on the map, FindCurrentPlace doesn't tag them with this specific type in its response. That's why swapping to MUSEUM works perfectly (it’s in the default type set), but PHARMACY doesn't.
How to fix it: Switch to the SearchNearby API
The most reliable solution is to replace findCurrentPlace with the SearchNearby API, which supports the full range of place types including PHARMACY. This API lets you directly specify the type of place you want to search for, so you don't have to filter through unrelated results afterward.
Here's how to rewrite your function with this approach:
private fun searchNearbyPharmacies(location: LatLng) { // 构建附近搜索请求:指定中心位置、搜索半径(单位米)、目标类型 val searchRequest = SearchNearbyRequest.builder( location, 5000 // 可根据需求调整搜索半径,这里是5公里 ) .setPlaceTypes(listOf(PlaceTypes.PHARMACY)) // 直接指定只搜索药店 .build() placesClient.searchNearby(searchRequest) .addOnSuccessListener { response -> // 遍历返回的所有药店地点 for (place in response.places) { place.location?.let { latLng -> addMarkerForPharmacy(latLng, place.displayName ?: "Pharmacy") } } } .addOnFailureListener { exception -> exception.printStackTrace() } }
Important checks for this solution:
- Permissions: Just like
findCurrentPlace,searchNearbyrequiresACCESS_FINE_LOCATIONorACCESS_COARSE_LOCATIONpermission—make sure you've requested this from the user and they've granted it. - API Enablement: Confirm that your Google Cloud project has the Places API enabled (not just the Maps SDK for Android), and that your API key has the correct restrictions and permissions.
- Radius Tuning: The
5000value is the search radius in meters—adjust it based on how far you want to look for pharmacies.
A less ideal fallback (if you must use FindCurrentPlace)
If you can't switch to searchNearby for some reason, you could try guessing pharmacies by checking their display name for keywords like "Pharmacy", "Drugstore", or local language equivalents, or checking if placeTypes includes PlaceTypes.HEALTH or PlaceTypes.STORE. But this is a hacky approach that will miss many pharmacies or tag non-pharmacies incorrectly, so it's not recommended.
Since you can already see pharmacies on the map, the searchNearby method will correctly pull those locations and tag them as pharmacies—this should solve your problem completely!
备注:内容来源于stack exchange,提问作者GM42

