在谷歌表格中,我有一列带有纬度和经度坐标。该列表从 A2:A1000 开始。我还分别在 B1、C1 和 D1 中提供了城市、州和国家/地区列。是否有我可以运行的公式或脚本来读取坐标并在各自的列中提供城市、州和国家/地区?我不知道如何使用JavaScript,XML,JSON,序列化PHP等,所以如果你的建议包括其中之一,请提供一些说明。提前谢谢。
好吧,我有一个伪解决方案。进入 Google 电子表格>工具>脚本编辑器,然后将以下代码粘贴到空白项目中:
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)
这将提供完整的地址。我现在正试图弄清楚如何从地址解析国家/地区。
基于 @gabriel-rotman 解决方案。
进入 Google 电子表格>工具>脚本编辑器,将以下代码粘贴到空白项目中:
/**
* 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;
}