从Google Sheets中的纬度和经度获取城市、州和国家

4
在Google表格中,我有一个包含纬度和经度坐标的列。该列表从A2:A1000中列出。我还有B1、C1和D1分别用于城市、州和国家的列。是否有一种公式或脚本可以读取坐标并在各自的列中提供城市、州和国家信息?我不知道如何使用JavaScript、XML、JSON、序列化PHP等内容,因此如果您的建议包括其中之一,请提供一些说明。提前感谢。
2个回答

13

好的,我有一个近似解决方案。进入谷歌电子表格 > 工具 > 脚本编辑器,并将以下代码粘贴到空白项目中:

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 它正在工作,但是出现了错误。"一天内调用服务次数过多:地理编码(第25行)"。 - Umair
我知道我回复晚了,因为我今天才开始处理这个。通过分割字符串,我获取了国家值。“function reverse_geocode(lat,long) { Utilities.sleep(1500); var response = Maps.newGeocoder().reverseGeocode(lat,long); for (var i = 0; i < response.results.length; i++) { var result = response.results[i]; var result = result.formatted_address; var result = result.split(","); return result[(result.length)-1]; } }" - Jason

2
基于 @gabriel-rotman 的解决方案。
进入 Google Spreadsheets > 工具 > 脚本编辑器,并将以下代码粘贴到空白项目中:
/**
 * 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;
}

谢谢你的努力。它正在工作,但是出现了错误。“一天内调用服务次数过多:地理编码(第25行)”。我试图获取1000个纬度/经度的行政区域和地址。 - Umair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接