擷取欄位
如果您已經有 Place
物件或地點 ID,請使用 Place.fetchFields()
方法取得該地點的詳細資料。提供以半形逗號分隔的地點資料欄位清單,以傳回這些欄位;請以駝峰式大小寫指定欄位名稱。使用傳回的 Place
物件,即可取得所要求欄位的資料。
下例使用地點 ID 建立新的 Place
,並呼叫 Place.fetchFields()
來要求 displayName
和 formattedAddress
欄位,然後在地圖上新增標記,並將部分資料記錄到控制台。
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, }); }
Map
和 Place
已在這個函式之前宣告:
const { Map } = await google.maps.importLibrary("maps"); const { Place } = await google.maps.importLibrary("places");