【问题标题】:Get City, State, Country from Latitude and Longitude in Google Sheets从 Google 表格中的纬度和经度获取城市、州、国家/地区
【发布时间】:2014-12-22 17:00:37
【问题描述】:

在 Google 表格中,我有一列包含经纬度坐标。该列表来自 A2:A1000。我还在 B1、C1 和 D1 中分别有城市、州和国家的列。是否有我可以运行的公式或脚本来读取坐标并在各自的列中提供城市、州和国家?我不知道如何使用 JavaScript、XML、JSON、序列化 PHP 等,所以如果您的建议包括其中之一,请提供一些说明。提前致谢。

【问题讨论】:

    标签: google-sheets geocoding latitude-longitude reverse-geocoding


    【解决方案1】:

    嗯,我有一个伪解决方案。进入 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 它正在工作但出现错误。 “一天服务调用次数过多:地理编码。(第 25 行)”
    • 我知道我今天收到这个回复真的迟到了。我通过拆分字符串来获取国家/地区值。 “函数 reverse_geocode(lat,long) { Utilities.sleep(1500); var response = Maps.newGeocoder().reverseGeocode(lat,long); for (var i = 0; i
    【解决方案2】:

    基于@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;
    }
    

    【讨论】:

    • 感谢您的努力。它正在工作但给出错误。 “一天服务调用次数过多:地理编码。(第 25 行)”。我试图获取 1000 lat/lon 的行政区域和地址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2017-09-02
    • 2014-06-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多