Szczegóły miejsca (nowość)

Deweloperzy z Europejskiego Obszaru Gospodarczego (EOG)

Pobieranie pól

Jeśli masz już identyfikator obiektu lub miejsca Place, użyj metody Place.fetchFields(), aby uzyskać szczegółowe informacje o tym miejscu. Podaj listę pól danych o miejscu rozdzielonych przecinkami, które mają zostać zwrócone. Nazwy pól podaj w formacie camel case. Użyj zwróconego obiektu Place, aby uzyskać dane dla żądanych pól.

W tym przykładzie użyto identyfikatora miejsca do utworzenia nowego Place, wywołano Place.fetchFields(), aby wysłać żądanie dotyczące pól displayNameformattedAddress, dodano do mapy marker i zapisano w konsoli niektóre dane.

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: 'ChIJN5Nz71W3j4ARhx5bwpTQEGg',         requestedLanguage: 'en', // optional     });      // Call fetchFields, passing the desired data fields.     await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });      // Log the result     console.log(place.displayName);     console.log(place.formattedAddress);      // Add an Advanced Marker     const marker = new AdvancedMarkerElement({         map,         position: place.location,         title: place.displayName,     }); }

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: 'ChIJN5Nz71W3j4ARhx5bwpTQEGg',         requestedLanguage: 'en', // optional     });     // Call fetchFields, passing the desired data fields.     await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });     // Log the result     console.log(place.displayName);     console.log(place.formattedAddress);     // Add an Advanced Marker     const marker = new AdvancedMarkerElement({         map,         position: place.location,         title: place.displayName,     }); }
Pamiętaj, że zmienne MapPlace zostały zadeklarowane przed tą funkcją:
const { Map } = await google.maps.importLibrary("maps"); const { Place } = await google.maps.importLibrary("places");
Zobacz pełny przykład