Class Geocoder

地理編碼器

允許在地址和地理座標之間進行轉換。
以下範例說明如何使用這個類別,找出科羅拉多州「主要街」的九位前 9 個相符結果、將其新增至地圖,然後再嵌入新的 Google 文件中。

// Find the best matches for "Main St" in Colorado.
var 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.
var doc = DocumentApp.create('My Map');
var map = Maps.newStaticMap();

// Add each result to the map and doc.
for (var i = 0; i < response.results.length && i < 9; i++) {
  var 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.
var response = Maps.newGeocoder().geocode('Times Square, New York, NY');
for (var i = 0; i < response.results.length; i++) {
  var 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.
var response = Maps.newGeocoder().reverseGeocode(40.758577, -73.984464);
for (var i = 0; i < response.results.length; i++) {
  var 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.
var geocoder = Maps.newGeocoder()
    .setBounds(40.699642, -74.021072, 40.877569, -73.908548);

參數

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

回攻員

Geocoder:地理編碼器物件,用於鏈結呼叫

另請參閱


setLanguage(language)

設定在結果中使用的語言。

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

參數

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

回攻員

Geocoder:地理編碼器物件,用於鏈結呼叫。

另請參閱


setRegion(region)

設定解譯位置名稱時使用的區域。支援的區碼與 Google 地圖支援的 ccTLD。例如,區碼「uk」對應「maps.google.co.uk」。

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

參數

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

回攻員

Geocoder:地理編碼器物件,用於鏈結呼叫

另請參閱