0

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

回答

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) 

这将提供完整的地址。我现在想弄清楚如何从地址解析国家。

+0

@ Gabriel.Rotman这是工作,但给错误。 “服务调用了一天的太多时间:地理编码(第25行)” – Umair

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

相关问题