允許在地址和地理座標之間進行轉換。
以下範例說明如何使用這個類別,找出科羅拉多州「主要街」的九位前 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 | 住家地址 |
回攻員
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 | 點的經度 |
回攻員
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 | 邊界東北角的經度 |
回攻員
Geocoder
:地理編碼器物件,用於鏈結呼叫
另請參閱
setLanguage(language)
setRegion(region)
設定解譯位置名稱時使用的區域。支援的區碼與 Google 地圖支援的 ccTLD。例如,區碼「uk」對應「maps.google.co.uk」。
// Creates a Geocoder with the region set to France. var geocoder = Maps.newGeocoder().setRegion('fr');
參數
名稱 | 類型 | 說明 |
---|---|---|
region | String | 要使用的區碼 |
回攻員
Geocoder
:地理編碼器物件,用於鏈結呼叫