商家和其他搜尋點

選取平台: Android iOS JavaScript

根據預設,搜尋點會與其對應的圖示一併顯示在基本地圖上。搜尋點包括公園、學校、政府大樓等。此外,如果地圖類型為 kGMSTypeNormal,預設會顯示「商家」搜尋點。商家搜尋點代表商家,例如商店、餐廳、飯店等。

依照 Places SDK for iOS 設定,每個搜尋點都會對應至一個地點 ID。舉例來說,休閒公園是搜尋點,但像噴水池這類的項目通常就不是搜尋點 (除非其本身具有國家或歷史意義)。

監聽搜尋點的點擊事件

如果您想要回應輕觸搜尋點的使用者,請實作 GMSMapViewDelegate,並實作 mapView(_:didTapPOIWithPlaceID:name:location:) 方法,如以下範例所示:

Swift

import GoogleMaps

class POI: UIViewController, GMSMapViewDelegate {

  override func loadView() {
    let camera = GMSCameraPosition.camera(
      withLatitude: 47.603,
      longitude:-122.331,
      zoom:14
    )
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    mapView.delegate = self
    self.view = mapView
  }

  func mapView(
    _ mapView: GMSMapView,
    didTapPOIWithPlaceID placeID: String,
    name: String,
    location: CLLocationCoordinate2D
  ) {
    print("You tapped \(name): \(placeID), \(location.latitude)/\(location.longitude)")
  }
}
      

Objective-C

#import "POI.h"
@import GoogleMaps;

@interface POI () <GMSMapViewDelegate>

@end

@implementation POI

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.603
                                                            longitude:-122.331
                                                                 zoom:14];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.delegate = self;
  self.view = mapView;
}

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView
    didTapPOIWithPlaceID:(NSString *)placeID
                    name:(NSString *)name
                location:(CLLocationCoordinate2D)location {
  NSLog(@"You tapped %@: %@, %f/%f", name, placeID, location.latitude, location.longitude);
}

@end
      

在資訊視窗中顯示詳細資料

根據預設,地圖上會顯示搜尋點,但系統不會提供預設的點選式 UI (使用者輕觸搜尋點時,API 不會自動顯示資訊視窗或任何其他使用者介面)。以下範例說明如何使用標記顯示 POI 的資訊視窗:

Swift

// Declare GMSMarker instance at the class level.
let infoMarker = GMSMarker()

// Attach an info window to the POI using the GMSMarker.
func mapView(
  _ mapView: GMSMapView,
  didTapPOIWithPlaceID placeID: String,
  name: String,
  location: CLLocationCoordinate2D
) {
  infoMarker.snippet = placeID
  infoMarker.position = location
  infoMarker.title = name
  infoMarker.opacity = 0;
  infoMarker.infoWindowAnchor.y = 1
  infoMarker.map = mapView
  mapView.selectedMarker = infoMarker
}
      

Objective-C

// Declare a GMSMarker instance at the class level.
GMSMarker *infoMarker;

// Attach an info window to the POI using the GMSMarker.
- (void)mapView:(GMSMapView *)mapView
    didTapPOIWithPlaceID:(NSString *)placeID
                    name:(NSString *)name
                location:(CLLocationCoordinate2D)location {
  infoMarker = [GMSMarker markerWithPosition:location];
  infoMarker.snippet = placeID;
  infoMarker.title = name;
  infoMarker.opacity = 0;
  CGPoint pos = infoMarker.infoWindowAnchor;
  pos.y = 1;
  infoMarker.infoWindowAnchor = pos;
  infoMarker.map = mapView;
  mapView.selectedMarker = infoMarker;
}
      

停止在地圖上顯示搜尋點

將自訂樣式套用至所有搜尋點或特定類別的搜尋點,即可隱藏搜尋點。

下列 JSON 樣式宣告會隱藏地圖上的所有商家搜尋點:

[
  {
    "featureType": "poi.business",
    "stylers": [
      { "visibility": "off" }
    ]
  }
]

再舉一個例子,下列 JSON 簡化了所有類別搜尋點的顯示方式:

[
  {
    "featureType": "poi",
    "stylers": [
      { "visibility": "simplified" }
    ]
  }
]

詳情請參閱使用樣式設定隱藏地圖項目指南。