Établissements et autres points d'intérêt

Sélectionnez une plate-forme:Android iOS JavaScript

Par défaut, les points d'intérêt (POI) s'affichent sur la carte de base avec les icônes correspondantes. Les POI incluent les parcs, les écoles, les bâtiments administratifs, etc. Par ailleurs, les POI de type établissement sont affichés par défaut sur la carte lorsque le type de la carte est défini sur kGMSTypeNormal. Ces POI représentent des établissements tels que des magasins, des restaurants, des hôtels, etc.

Un POI correspond à un ID de lieu, tel que défini dans le SDK Places pour iOS. Par exemple, les parcs de loisirs sont des POI, mais les éléments tels que les fontaines ne sont généralement pas des POI (à moins qu'ils ne revêtent une importance nationale ou historique).

Écouter les événements de clic sur des POI

Si vous souhaitez répondre à un utilisateur qui appuie sur un POI, implémentez GMSMapViewDelegate, puis la méthode mapView(_:didTapPOIWithPlaceID:name:location:), comme illustré dans l'exemple suivant:

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
      

Afficher les détails dans une fenêtre d'informations

Les POI sont affichés par défaut sur la carte, mais il n'y a pas d'interface utilisateur de clic par défaut (l'API n'affiche pas automatiquement une fenêtre d'informations ni une autre interface utilisateur lorsque l'utilisateur appuie sur un POI). L'exemple suivant montre comment utiliser un repère afin d'afficher une fenêtre d'informations pour un 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;
}
      

Arrêter l'affichage de POI sur la carte

Vous pouvez masquer les POI en appliquant des styles personnalisés à tous les POI ou à des catégories spécifiques de POI.

La déclaration de style JSON suivante masque tous les POI commerciaux sur la carte :

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

Dans cet autre exemple, la déclaration JSON simplifie l'affichage de toutes les catégories de POI :

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

Pour en savoir plus, consultez le guide sur le masquage d'éléments cartographiques à l'aide des styles.