默认情况下,地图注点 (POI) 将与对应的图标一起显示在基本地图上。地图注点包括公园、学校、政府大楼和
。另外,商家地图注点默认会显示在地图上
类型为 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
在信息窗口中显示详细信息
地图注点会默认显示在地图上,但是没有默认的点击界面(用户点击地图注点时,API 不会自动显示信息窗口或任何其他界面)。以下示例展示了如何使用标记来 显示地图注点的信息窗口:
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" }
]
}
]
如需了解详情,请参阅有关通过样式设置隐藏地图项的指南。