商家和其他搜尋點
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
根據預設,搜尋點 (POI) 與對應的圖示會一併顯示在基本地圖上。搜尋點包括公園、學校、政府大樓等。
此外,如果地圖類型為 normal
,預設會顯示「商家」搜尋點。商家搜尋點代表商家,例如商店、餐廳、飯店等。室內地圖 (平面圖) 上的商家搜尋點,只會在精簡模式地圖顯示。
每個搜尋點都會對應至 Places SDK for Android 中定義的一個地點 ID。舉例來說,休閒公園是搜尋點,但像噴水池這類的項目通常就不是搜尋點 (除非其本身具有國家或歷史意義)。
監聽搜尋點的點擊事件
如果您想要回應輕觸搜尋點的使用者,可以使用 OnPoiClickListener
,如以下程式碼範例所示:
Kotlin
internal class OnPoiClickDemoActivity : AppCompatActivity(), OnMapReadyCallback, OnPoiClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.poi_click_demo)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map)
as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap) {
map.setOnPoiClickListener(this)
}
override fun onPoiClick(poi: PointOfInterest) {
Toast.makeText(this, """Clicked: ${poi.name}
Place ID:${poi.placeId}
Latitude:${poi.latLng.latitude} Longitude:${poi.latLng.longitude}""",
Toast.LENGTH_SHORT
).show()
}
}
Java
class OnPoiClickDemoActivity extends AppCompatActivity implements
OnMapReadyCallback, GoogleMap.OnPoiClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poi_click_demo);
SupportMapFragment mapFragment;
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.setOnPoiClickListener(this);
}
@Override
public void onPoiClick(PointOfInterest poi) {
Toast.makeText(this, "Clicked: " +
poi.name + "\nPlace ID:" + poi.placeId +
"\nLatitude:" + poi.latLng.latitude +
" Longitude:" + poi.latLng.longitude,
Toast.LENGTH_SHORT).show();
}
}
根據預設,地圖上會顯示搜尋點,但系統不會提供預設的點選式 UI;也就是說,使用者輕觸搜尋點時,API 不會自動顯示資訊視窗或任何其他使用者介面。
如以上範例所示,您可以透過呼叫 GoogleMap.setOnPoiClickListener(OnPoiClickListener)
在地圖上設定 OnPoiClickListener
。
當使用者按一下 (輕觸) 搜尋點時,您的應用程式會收到 OnPoiClick(PointOfInterest)
事件,指出該搜尋點。PointOfInterest
包含經緯度座標、地點 ID 和搜尋點名稱。
停止在地圖上顯示搜尋點
將自訂樣式套用至全部或特定類別的搜尋點,即可加以隱藏。
下列 JSON 樣式宣告會隱藏地圖上的所有商家搜尋點:
[
{
"featureType": "poi.business",
"stylers": [
{ "visibility": "off" }
]
}
]
再舉一個例子,下列 JSON 簡化了所有類別搜尋點的顯示方式:
[
{
"featureType": "poi",
"stylers": [
{ "visibility": "simplified" }
]
}
]
如需 Java 程式碼和其他詳細資料,請參閱「使用樣式設定隱藏地圖項目」指南。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-05-09 (世界標準時間)。
[null,null,["上次更新時間:2024-05-09 (世界標準時間)。"],[[["\u003cp\u003ePoints of Interest (POIs) such as parks, schools, and businesses, are displayed on the map by default with corresponding icons.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can listen for click events on POIs and respond to user interactions using the \u003ccode\u003eOnPoiClickListener\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAlthough POIs are displayed by default, there is no default on-click UI, meaning developers need to implement their own info windows or other UI elements.\u003c/p\u003e\n"],["\u003cp\u003eThe display of POIs can be customized or hidden altogether using JSON style declarations to control their visibility and appearance.\u003c/p\u003e\n"]]],["Points of interest (POIs), including parks, schools, and businesses, appear by default on maps. To handle user taps on POIs, use `OnPoiClickListener` and set it via `GoogleMap.setOnPoiClickListener`. This triggers an `OnPoiClick` event, providing the POI's name, place ID, and coordinates. You can also hide POIs using custom JSON styling by setting the `visibility` to `off` for business POIs for instance or `simplified` for all POIs.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/poi \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/poi \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/examples/event-poi \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nBy default, points of interest (POIs) appear on the base map along with their\ncorresponding icons. POIs include parks, schools, government buildings, and\nmore.\n\nIn addition, *business* POIs appear by default on the map when the map type is\n`normal`. Business POIs represent businesses such as shops, restaurants, hotels,\nand more. Business POIs on [indoor maps (floor plans)](/maps/documentation/android-sdk/map#indoor) appear only on a\n[lite mode](/maps/documentation/android-sdk/lite) map.\n\nA POI corresponds to a [Place ID](/maps/documentation/places/android-sdk/place-id), as defined in the\nPlaces SDK for Android. For example, recreational parks are POIs, but\nthings like water fountains are generally not POIs (unless they're of national\nor historic significance).\n\nListen for click events on POIs\n\nIf you want to respond to a user tapping on a POI, you can use an\n[`OnPoiClickListener`](/android/reference/com/google/android/gms/maps/GoogleMap.OnPoiClickListener) as shown in the following code\nsample:\n\n\nKotlin \n\n```kotlin\ninternal class OnPoiClickDemoActivity : AppCompatActivity(), OnMapReadyCallback, OnPoiClickListener {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.poi_click_demo)\n val mapFragment = supportFragmentManager.findFragmentById(R.id.map)\n as SupportMapFragment\n mapFragment.getMapAsync(this)\n }\n\n override fun onMapReady(map: GoogleMap) {\n map.setOnPoiClickListener(this)\n }\n\n override fun onPoiClick(poi: PointOfInterest) {\n Toast.makeText(this, \"\"\"Clicked: ${poi.name}\n Place ID:${poi.placeId}\n Latitude:${poi.latLng.latitude} Longitude:${poi.latLng.longitude}\"\"\",\n Toast.LENGTH_SHORT\n ).show()\n }\n}\n\n \n```\n\nJava \n\n```java\nclass OnPoiClickDemoActivity extends AppCompatActivity implements\n OnMapReadyCallback, GoogleMap.OnPoiClickListener {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.poi_click_demo);\n SupportMapFragment mapFragment;\n mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);\n mapFragment.getMapAsync(this);\n }\n\n @Override\n public void onMapReady(GoogleMap map) {\n map.setOnPoiClickListener(this);\n }\n\n @Override\n public void onPoiClick(PointOfInterest poi) {\n Toast.makeText(this, \"Clicked: \" +\n poi.name + \"\\nPlace ID:\" + poi.placeId +\n \"\\nLatitude:\" + poi.latLng.latitude +\n \" Longitude:\" + poi.latLng.longitude,\n Toast.LENGTH_SHORT).show();\n }\n}\n\n \n```\n\n\u003cbr /\u003e\n\nPOIs appear on the map by default, but there is no default on-click UI. That is,\nthe API does not automatically display an info window or any other user\ninterface when the user taps a POI.\n\nAs the above sample shows, you set the\n[`OnPoiClickListener`](/android/reference/com/google/android/gms/maps/GoogleMap.OnPoiClickListener) on the map by\ncalling\n[`GoogleMap.setOnPoiClickListener(OnPoiClickListener)`](/android/reference/com/google/android/gms/maps/GoogleMap#setOnPoiClickListener(com.google.android.gms.maps.GoogleMap.OnPoiClickListener)).\nWhen a user clicks (taps) on a POI, your app receives an\n[`OnPoiClick(PointOfInterest)`](/android/reference/com/google/android/gms/maps/GoogleMap.OnPoiClickListener#onPoiClick(com.google.android.gms.maps.model.PointOfInterest)) event\nindicating the point of interest (POI) that the user clicked. The\n[`PointOfInterest`](/android/reference/com/google/android/gms/maps/model/PointOfInterest) contains the latitude/longitude coordinates,\nplace ID and name of the point of interest.\n\nStop POIs from showing on the map\n\nYou can hide points of interest (POIs) by applying custom styles to all POIs or\nto specific categories of POIs.\n\nThe following JSON style declaration hides all business POIs on the map: \n\n [\n {\n \"featureType\": \"poi.business\",\n \"stylers\": [\n { \"visibility\": \"off\" }\n ]\n }\n ]\n\nAs another example, the following JSON simplifies the display of all categories\nof POIs: \n\n [\n {\n \"featureType\": \"poi\",\n \"stylers\": [\n { \"visibility\": \"simplified\" }\n ]\n }\n ]\n\nFor Java code and other details, see the guide to\n[hiding map features with styling](/maps/documentation/android-sdk/hiding-features)."]]