Class Geocoder

地理編碼器

可轉換地址和地理座標。
以下範例說明如何使用這個類別,找出科羅拉多州「Main St」的前九個相符地點、將這些地點新增至地圖,然後將地圖嵌入新的 Google 文件。

// Find the best matches for "Main St" in Colorado.
const response = Maps.newGeocoder()
                     // The latitudes and longitudes of southwest and northeast
                     // corners of Colorado, respectively.
                     .setBounds(36.998166, -109.045486, 41.001666, -102.052002)
                     .geocode('Main St');

// Create a Google Doc and map.
const doc = DocumentApp.create('My Map');
const map = Maps.newStaticMap();

// Add each result to the map and doc.
for (let i = 0; i < response.results.length && i < 9; i++) {
  const result = response.results[i];
  map.setMarkerStyle(null, null, i + 1);
  map.addMarker(result.geometry.location.lat, result.geometry.location.lng);
  doc.appendListItem(result.formatted_address);
}

// Add the finished map to the doc.
doc.appendImage(Utilities.newBlob(map.getMapImage(), 'image/png'));

另請參閱

方法

方法傳回類型簡短說明
geocode(address)Object取得指定地址的概略地理位置點。
reverseGeocode(latitude, longitude)Object取得指定地理位置點的概略地址。
setBounds(swLatitude, swLongitude, neLatitude, neLongitude)Geocoder設定區域的界線,搜尋結果應優先顯示該區域的內容。
setLanguage(language)Geocoder設定結果中使用的語言。
setRegion(region)Geocoder設定解讀地點名稱時要使用的區域。

內容詳盡的說明文件

geocode(address)

取得指定地址的概略地理位置點。

// Gets the geographic coordinates for Times Square.
const response = Maps.newGeocoder().geocode('Times Square, New York, NY');
for (let i = 0; i < response.results.length; i++) {
  const result = response.results[i];
  Logger.log(
      '%s: %s, %s',
      result.formatted_address,
      result.geometry.location.lat,
      result.geometry.location.lng,
  );
}

參數

名稱類型說明
addressString地址。

回攻員

Object:包含地理編碼資料的 JSON 物件,如這裡所述。


reverseGeocode(latitude, longitude)

取得指定地理位置點的概略地址。

// Gets the address of a point in Times Square.
const response = Maps.newGeocoder().reverseGeocode(40.758577, -73.984464);
for (let i = 0; i < response.results.length; i++) {
  const result = response.results[i];
  Logger.log(
      '%s: %s, %s',
      result.formatted_address,
      result.geometry.location.lat,
      result.geometry.location.lng,
  );
}

參數

名稱類型說明
latitudeNumber點的緯度。
longitudeNumber該點的經度。

回攻員

Object:包含反向地理編碼資料的 JSON 物件,如這裡所述。

另請參閱


setBounds(swLatitude, swLongitude, neLatitude, neLongitude)

設定區域的界線,搜尋結果應優先顯示該區域的內容。

// Creates a Geocoder that prefers points in the area of Manhattan.
const geocoder = Maps.newGeocoder().setBounds(
    40.699642,
    -74.021072,
    40.877569,
    -73.908548,
);

參數

名稱類型說明
swLatitudeNumber邊界西南角的緯度。
swLongitudeNumber邊界西南角的經度。
neLatitudeNumber邊界的東北角緯度。
neLongitudeNumber邊界的東北角經度。

回攻員

Geocoder:Geocoder 物件,可簡化呼叫鏈結。

另請參閱


setLanguage(language)

設定結果中使用的語言。

// Creates a Geocoder with the language set to French.
const geocoder = Maps.newGeocoder().setLanguage('fr');

參數

名稱類型說明
languageStringBCP-47 語言 ID。

回攻員

Geocoder:Geocoder 物件,可簡化呼叫鏈結。

另請參閱


setRegion(region)

設定解讀地點名稱時要使用的區域。支援的區域代碼對應至 Google 地圖支援的 ccTLD。舉例來說,區域代碼「uk」對應至「maps.google.co.uk」。

// Creates a Geocoder with the region set to France.
const geocoder = Maps.newGeocoder().setRegion('fr');

參數

名稱類型說明
regionString要使用的區域代碼。

回攻員

Geocoder:Geocoder 物件,可簡化呼叫鏈結。

另請參閱