在Google表格中,我有一列包含经度和纬度坐标的列。该列表来自A2:A1000。我还分别在B1,C1和D1中分别列出了城市,州和国家的列。是否有可以运行的公式或脚本来读取坐标并在各自的列中提供城市,州和国家?我不知道如何使用JavaScript,XML,JSON,序列化的PHP等,如果你的建议包括其中之一,请提供一些说明。提前致谢。从Google表格的经纬度获取城市,州和国家
0
A
回答
3
那么,我有一个psuedo解决方案。进入谷歌电子表格>工具>脚本编辑器,下面的代码粘贴到一个空白的项目:在电子表格中
function reverse_geocode(lat,lng) {
Utilities.sleep(1500);
var response = Maps.newGeocoder().reverseGeocode(lat,lng);
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);
return result.formatted_address;
}
}
然后,使用这个公式:
=reverse_geocode(latitude_goes_here,longitude_goes_here)
举例来说,如果我有纬度在A2和经度在B2:
=reverse_geocode(A2,B2)
这将提供完整的地址。我现在想弄清楚如何从地址解析国家。
1
基于@ gabriel-rotman解决方案。
进入谷歌电子表格>工具>脚本编辑器,将下面的代码粘贴到一个空白的项目:
/**
* Return the closest, human-readable address type based on the the latitude and longitude values specified.
*
* @param {"locality"} addressType Address type. Examples of address types include
* a street address, a country, or a political entity.
* For more info check: https://developers.google.com/maps/documentation/geocoding/intro#Types
* @param {"52.379219"} lat Latitude
* @param {"4.900174"} lng Longitude
* @customfunction
*/
function reverseGeocode(addressType, lat, lng) {
Utilities.sleep(1500);
if (typeof addressType != 'string') {
throw new Error("addressType should be a string.");
}
if (typeof lat != 'number') {
throw new Error("lat should be a number");
}
if (typeof lng != 'number') {
throw new Error("lng should be a number");
}
var response = Maps.newGeocoder().reverseGeocode(lat, lng),
key = '';
response.results.some(function (result) {
result.address_components.some(function (address_component) {
return address_component.types.some(function (type) {
if (type == addressType) {
key = address_component.long_name;
return true;
}
});
});
});
return key;
}
然后在电子表格中,使用这个公式:
=reverseGeocode(address_type; latitude_goes_here; longitude_goes_here)
例如,如果我有A2的纬度和B2的经度,我想要得到城市,然后我可以使用:
=reverseGeocode("locality"; A2; B2)
如果你愿意,你可以使用的国家:
=reverseGeocode("country"; A2; B2)
奖金函数来提取地址的一部分:
/**
* Return the closest, human-readable address type based on the the address passed.
*
* @param {"locality"} addressType Address type. Examples of address types include
* a street address, a country, or a political entity.
* For more info check: https://developers.google.com/maps/documentation/geocoding/intro#Types
* @param {"Amsterdam"} address The street address that you want to geocode,
* in the format used by the national postal service
* of the country concerned. Additional address elements
* such as business names and unit, suite or floor
* numbers should be avoided.
* @customfunction
*/
function geocode(addressType, address) {
if (typeof addressType != 'string') {
throw new Error("addressType should be a string.");
}
if (typeof address != 'string') {
throw new Error("address should be a string.");
}
var response = Maps.newGeocoder().geocode(address),
key = "";
response.results.some(function (result) {
return result.address_components.some(function (address_component) {
return address_component.types.some(function (type) {
if (type === addressType) {
key = address_component.long_name;
}
});
});
});
return key;
}
+0
感谢您的努力。它正在工作,但给错误。 “服务在一天内调用了太多时间:地理编码(第25行)”。我试图获得1000经纬度的行政区域和地址 – Umair
相关问题
- 1. 从经纬度获取城市,州,国家
- 2. 从城市和州获取国家
- 3. 通过州和国家获取城市Google API
- 4. 获取某个国家(或州)的所有城市的列表,包括经度和纬度
- 5. 从经纬度得到国家,地区和城市名称(PHP)
- 6. 国家城市州名单
- 7. 从城市和州输入获取经度和纬度的快速方法
- 8. 从地址获取城市,州和国家的最佳途径
- 9. 如何转换经度和纬度到国家或城市?
- 10. 从城市名称获取经纬度
- 11. 从经纬度获取城市名称
- 12. 国家,州和城市的PHP库
- 13. 如何经度阅读国家城市纬度的地图API
- 14. 按国家,州和城市汇总
- 15. 从AutoCompleteTextView获取城市和国家
- 16. 如何获取城市的经纬度?
- 17. 从iOS的FacebookSDK获取国家,州,城市
- 18. 获取国家/从MySQL表使用纬度和经度市英明数据colomns
- 19. IP到国家,城市,纬度,经度javascript服务
- 20. 获取国家,州和市的API
- 21. 使用经度和纬度查找城市和州名称
- 22. iPhone中的Google地图 - 经度和纬度的城市和州名称
- 23. 如何从reverseGeocodeCoordinate获取国家,州,城市?
- 24. 如何获取城市/国家名称的经纬度范围android
- 25. 使用Google API获取城市的国家/国家建议
- 26. 如何从R中的城市名称和国家获取经度和纬度坐标?
- 27. 桌面视图与国家/州/城市
- 28. Ruby on Rails - 选择国家和州时,加载州和城市
- 29. 从R经度和纬度点获取国家(和大陆)
- 30. 地图API获取城市和国家
@ Gabriel.Rotman这是工作,但给错误。 “服务调用了一天的太多时间:地理编码(第25行)” – Umair