필드 가져오기
기존 Place
객체 또는 장소 ID가 있는 경우 Place.fetchFields()
메서드를 사용하여 해당 장소에 대한 세부정보를 가져옵니다. 반환할 장소 데이터 필드의 쉼표로 구분된 목록을 제공합니다. 필드 이름을 카멜 표기법으로 지정합니다. 반환된 Place
객체를 사용하여 요청된 필드의 데이터를 가져옵니다.
다음 예에서는 장소 ID를 사용하여 새 Place
를 만들고 displayName
및 formattedAddress
필드를 요청하는 Place.fetchFields()
를 호출하고, 지도에 마커를 추가하고, 일부 데이터를 콘솔에 로깅합니다.
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, }); }
자바스크립트
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");