Google文档中的反向地理编码

时间:2012-12-13 22:44:06

标签: google-api reverse-geocoding geocode

我试图在Google电子表格中获取Geocodes位置的国家和城市(即:51.5665,-1.2410)。我找到了一个代码来获取该国家/地区的纬度和经度,但我无法将其更改为使用反向功能。

function onOpen() {
    // Add the Geocode menu
    SpreadsheetApp.getActiveSpreadsheet().addMenu("Geocoder", [{
        name: "Geocode addresses",
        functionName: 'geocode'
    }]);
}

function geocode() {
    // Get the current spreadsheet, sheet, range and selected addresses
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = SpreadsheetApp.getActiveRange();
    var addresses = range.getValues();

    // Determine the first row and column to geocode
    var row = range.getRow();
    var column = range.getColumn();

    // Set default destination columns
    var destination = new Array();
    destination[0] = column + 1;
    destination[1] = column + 2;

    // Prompt for latitude and longitude columns
    var response = Browser.inputBox("Coordinate Columns",
        "Please specify which columns should contain the latitude " +
        "and longitude values [ie. 'C,D', 'A,F', etc]. Leave blank to " +
        "insert new columns.",
        Browser.Buttons.OK_CANCEL);
    if (response == 'cancel') return;
    if (response == '')
        sheet.insertColumnsAfter(column, 2);
    else {
        var coord_columns = response.split(',');
        destination[0] = sheet.getRange(coord_columns[0] + '1').getColumn();
        destination[1] = sheet.getRange(coord_columns[1] + '1').getColumn();
    }

    // Initialize the geocoder and set loading status
    var geocoder = Maps.newGeocoder();
    var count = range.getHeight();
    spreadsheet.toast(count + " addresses are currently being geocoded. " +
                      "Please wait.", "Loading...", -1);

    // Iterate through addresses and geocode
    for (i in addresses) {
        var location = geocoder.geocode(
            addresses[i]).results[0].geometry.location;
        sheet.getRange(row, destination[0]).setValue(location.lat);
        sheet.getRange(row++, destination[1]).setValue(location.lng);
        Utilities.sleep(200);
    }

    // Remove loading status
    spreadsheet.toast("Geocoding is now complete.", "Finished", -1);
}

更准确地说,我希望找到一个经纬度的脚本,然后将该城市归入一列,将该国家归入另一列。

1 个答案:

答案 0 :(得分:3)

您需要使用此功能(如下)而不是:

  

var location =   geocoder.geocode(地址[I])的结果[0] .geometry.location;

reverseGeocode(纬度,经度)

获取给定地理点的大致地址。



      // Gets the address of a point in Times Square.
     var response = Maps.newGeocoder().reverseGeocode(40.758577, -73.984464);
     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);
     }
&#13;
&#13;
&#13;

https://developers.google.com/apps-script/reference/maps/geocoder#reverseGeocode(Number,Number)