回應當地特色事件

如果您的版面配置會在 Place Details 檢視畫面顯示時變更,請使用事件監聽器 placedetailsviewshowstartplacedetailsviewhidestart。在下例中,有三個以自訂標記表示的區域。當使用者按下其中一個區域標記時,系統會開啟 InfoWindow 來說明該區域。如果使用者在 InfoWindow 開啟時點選搜尋點,InfoWindow 就會在搜尋點的 Place Details 檢視畫面顯示時關閉,並在使用者關閉 Place Details 檢視畫面時重新開啟。

瞭解程式碼

管理包含 Place Details 檢視事件的 InfoWindow

當 Place Details 檢視畫面開啟且您呼叫 InfoWindow.close() 時,開啟的 InfoWindow 就會從 DOM 中移除。如要建立「重新開啟」InfoWindow 的效果,您必須將 InfoWindow 的屬性儲存在 DOM 外部的變數中,以便在希望 InfoWindow 恢復顯示時重新建立。建議在建立 InfoWindow 時,將資訊儲存到儲存空間變數。

let infoStorage;

function createInfoWindow(district, marker) {
  // Build the content of the InfoWindow
  let contentDiv = document.createElement('div');
  ...

  // Create and open a new InfoWindow
  infoWindow = new google.maps.InfoWindow();
  infoWindow.setContent(contentDiv);
  infoWindow.open(map, marker);

  // Store key properties of the InfoWindow for future restoration
  infoStorage = {
    'district': district,
    'marker': marker,
  };
}

之後當 Place Details 檢視畫面關閉時,您可以呼叫相同的 InfoWindow 建立函式,重新建立最後開啟的 InfoWindow

TypeScript

localContextMapView.addListener("placedetailsviewhidestart", () => {
  if (infoStorage) {
    createInfoWindow(infoStorage.district, infoStorage.marker);
  }
});

JavaScript

localContextMapView.addListener("placedetailsviewhidestart", () => {
  if (infoStorage) {
    createInfoWindow(infoStorage.district, infoStorage.marker);
  }
});

更新路線起點

這張地圖上有多個區域標記可供使用者選擇,因此在標記的點擊事件監聽器中,我們會使用下列幾行程式碼,將 directionsOptions 的起點更新為最後按下的區域。這表示即使在 localContextMapView 初始化後,仍可更新 directionsOptions

試用範例