在Google表格中,我有一个包含纬度和经度坐标的列。列表来自A2:A1000。我还分别在B1,C1和D1中有City,State和Country的列。是否有我可以运行的公式或脚本读取坐标并在各自的列中提供城市,州和国家/地区?我不知道如何使用JavaScript,XML,JSON,序列化PHP等,所以如果您的建议包括其中之一,请提供一些说明。提前谢谢。
答案 0 :(得分:3)
好吧,我有一个伪解决方案。进入Google Spreadsheets>工具>脚本编辑器将以下代码粘贴到空白项目中:
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 :(得分:1)
基于@ gabriel-rotman解决方案。
进入Google Spreadsheets&gt;工具&gt;脚本编辑器将以下代码粘贴到空白项目中:
/**
* 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;
}