รายละเอียดสถานที่ (ใหม่)

เลือกแพลตฟอร์ม: Android iOS JavaScript Web Service
นักพัฒนาแอปในเขตเศรษฐกิจยุโรป (EEA)

ดึงข้อมูลช่อง

หากมีออบเจ็กต์ Place หรือรหัสสถานที่อยู่แล้ว ให้ใช้วิธี Place.fetchFields() เพื่อดูรายละเอียดเกี่ยวกับสถานที่นั้น ระบุรายการฟิลด์ข้อมูลสถานที่ที่คั่นด้วยคอมมาเพื่อแสดงผล ระบุชื่อฟิลด์ในรูปแบบ Camel Case ใช้Placeออบเจ็กต์ที่ส่งคืนเพื่อรับข้อมูลสำหรับ ฟิลด์ที่ขอ

ตัวอย่างต่อไปนี้ใช้รหัสสถานที่เพื่อสร้าง 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: '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 และ Place ก่อนฟังก์ชันนี้แล้ว
const { Map } = await google.maps.importLibrary("maps");
const { Place } = await google.maps.importLibrary("places");
ดูตัวอย่างฉบับสมบูรณ์