允許在地址和地理座標之間進行轉換。
以下範例說明如何使用這個類別找出符合「科羅拉多州」的主要營業地點前 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); }
參數
名稱 | 類型 | 說明 |
---|---|---|
address | String | 地址 |
Return 鍵
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); }
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 點的緯度 |
longitude | Number | 點的經度 |
Return 鍵
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
swLatitude | Number | 邊界的西南角緯度 |
swLongitude | Number | 邊界的西南角經度 |
neLatitude | Number | 邊界的東北角緯度 |
neLongitude | Number | 邊界的東北角經度 |
Return 鍵
Geocoder
:協助呼叫鏈結的 Geocoding 物件
另請參閱
setLanguage(language)
setRegion(region)
設定在解讀位置名稱時要使用的區域。支援的區域代碼對應到 Google 地圖支援的 ccTLD。例如,區碼「quot;uk"」對應於 &maps.google.co.uk"。
// Creates a Geocoder with the region set to France. var geocoder = Maps.newGeocoder().setRegion('fr');
參數
名稱 | 類型 | 說明 |
---|---|---|
region | String | 要使用的區域代碼 |
Return 鍵
Geocoder
:協助呼叫鏈結的 Geocoding 物件