Posiziona oggetto
L'oggetto Place, che contiene informazioni su un luogo, viene restituito dinamicamente
da Text Search, Nearby Search e Place Autocomplete. Puoi anche creare un oggetto Place
da un ID luogo o da un nome risorsa (il nome risorsa è l'ID luogo con il prefisso
places/). Il seguente snippet mostra la creazione di un oggetto Place utilizzando un
ID luogo:
// Use a place ID to create a new Place instance. const place = new Place({ id: 'ChIJyYB_SZVU2YARR-I1Jjf08F0', // San Diego Zoo });
Puoi anche creare un oggetto Place dal nome di una risorsa di luogo:
// Use a place resource name to create a new Place instance. const place = new Place({ resourceName: 'places/ChIJyYB_SZVU2YARR-I1JRF08F0', // San Diego Zoo });
Per saperne di più, consulta PlaceOptions.
Recupera campi
Se hai un oggetto Place esistente o un ID luogo o un nome risorsa, utilizza il
metodo Place.fetchFields() per ottenere i dettagli del luogo. Fornisci un elenco separato da virgole
di campi di dati sui luoghi da
restituire; specifica i nomi dei campi in formato camel case. Utilizza l'oggetto Place restituito per ottenere i dati
per i campi richiesti.
L'esempio seguente utilizza un ID luogo per creare un nuovo Place, chiama Place.fetchFields()
richiedendo i campi displayName e formattedAddress e aggiunge un
indicatore alla mappa.
TypeScript
async function getPlaceDetails() { const { Place } = (await google.maps.importLibrary( 'places' )) as google.maps.PlacesLibrary; const { AdvancedMarkerElement } = (await google.maps.importLibrary( 'marker' )) as google.maps.MarkerLibrary; // Use place ID to create a new Place instance. const place = new Place({ id: 'ChIJyYB_SZVU2YARR-I1Jjf08F0', // San Diego Zoo }); // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: [ 'displayName', 'formattedAddress', 'location', 'googleMapsURI', ], }); // Add an Advanced Marker const marker = new AdvancedMarkerElement({ map: innerMap, position: place.location, title: place.displayName, }); // Assemble the info window content. const content = document.createElement('div'); const address = document.createElement('div'); const placeId = document.createElement('div'); address.textContent = place.formattedAddress || ''; placeId.textContent = place.id; content.append(placeId, address); if (place.googleMapsURI) { const link = document.createElement('a'); link.href = place.googleMapsURI; link.target = '_blank'; link.textContent = 'View Details on Google Maps'; content.appendChild(link); } // Display an info window. infoWindow.setHeaderContent(place.displayName); infoWindow.setContent(content); infoWindow.open({ anchor: marker, }); }
JavaScript
async function getPlaceDetails() { const { Place } = (await google.maps.importLibrary('places')); const { AdvancedMarkerElement } = (await google.maps.importLibrary('marker')); // Use place ID to create a new Place instance. const place = new Place({ id: 'ChIJyYB_SZVU2YARR-I1Jjf08F0', // San Diego Zoo }); // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: [ 'displayName', 'formattedAddress', 'location', 'googleMapsURI', ], }); // Add an Advanced Marker const marker = new AdvancedMarkerElement({ map: innerMap, position: place.location, title: place.displayName, }); // Assemble the info window content. const content = document.createElement('div'); const address = document.createElement('div'); const placeId = document.createElement('div'); address.textContent = place.formattedAddress || ''; placeId.textContent = place.id; content.append(placeId, address); if (place.googleMapsURI) { const link = document.createElement('a'); link.href = place.googleMapsURI; link.target = '_blank'; link.textContent = 'View Details on Google Maps'; content.appendChild(link); } // Display an info window. infoWindow.setHeaderContent(place.displayName); infoWindow.setContent(content); infoWindow.open({ anchor: marker, }); }
Map e Place sono stati dichiarati prima di questa funzione:
const { Map } = await google.maps.importLibrary("maps"); const { Place } = await google.maps.importLibrary("places");