1

我有一个应用程序,它将读取一个包含大量姓名和地址(800+)的 Excel 电子表格,使用 Google 地理编码 API 将地址转换为纬度/经度,并生成一个带有关联航点的 .kml 文档。

当转换大量地址时,.kml 文件将最终得到为不同地址分配相同纬度/经度的小组。我想知道是否需要处理小批量、暂停程序并处理另一批?

public void GeocodingSample() throws IOException, XPathExpressionException, ParserConfigurationException, SAXException
{

    l1 = new String[lineCount];
    l2 = new String[lineCount];
    //address2 = add;
    //city2 = city;

    bxg.UpdateTA("Converting address to lat/long...");

    ProgressBar progress = new ProgressBar();


    // URL prefix to the geocoder
    String GEOCODER_REQUEST_PREFIX_FOR_XML = "http://maps.google.com/maps/api/geocode/xml";

    // query address
    address = new String[lineCount];

    int x = 0;

    //System.out.println("Im about to enter the loop and the count is: "+lineCount);

    while(x < lineCount)
    {
        pbarvalue = x + 1;

        address[x] = (sheet[x][a2]+", "+sheet[x][s2]);

        progress.PbarValue(pbarvalue, lineCount, address[x]);

        url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address[x], "UTF-8") + "&sensor=false");

        // prepare a URL to the geocoder
        //URL url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");

        // prepare an HTTP connection to the geocoder
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        Document geocoderResultDocument = null;
        try 
        {
          // open the connection and get results as InputSource.
          conn.connect();
          InputSource geocoderResultInputSource = new InputSource(conn.getInputStream());

          // read result and parse into XML Document
          geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
        } finally {
          conn.disconnect();
        }

        // prepare XPath
        XPath xpath = XPathFactory.newInstance().newXPath();

        // extract the result
        NodeList resultNodeList = null;

        // a) obtain the formatted_address field for every result
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET);
        for(int i=0; i<resultNodeList.getLength(); ++i) {
          //System.out.println(resultNodeList.item(i).getTextContent());
        }

        // b) extract the locality for the first result
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text()='locality']/long_name", geocoderResultDocument, XPathConstants.NODESET);
        for(int i=0; i<resultNodeList.getLength(); ++i) {
          //System.out.println(resultNodeList.item(i).getTextContent());
        }

        // c) extract the coordinates of the first result
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*", geocoderResultDocument, XPathConstants.NODESET);

        for(int i=0; i<resultNodeList.getLength(); ++i) {
          Node node = resultNodeList.item(i);
          if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
          if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
        }
        //System.out.println("lat/lng=" + lat + "," + lng);
        l1[x] = (""+lat);
        l2[x] = (""+lng);

        // c) extract the coordinates of the first result
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text() = 'administrative_area_level_1']/country[short_name/text() = 'US']/*", geocoderResultDocument, XPathConstants.NODESET);
        for(int i=0; i<resultNodeList.getLength(); ++i) {
          Node node = resultNodeList.item(i);
          if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
          if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
        }
        //System.out.println("lat/lng=" + lat + "," + lng);

        x++;
    }//end While

    //System.out.println("The output is ready");

    progress.ShowFrame(false);
    OutputKML();



}// end GeoCoding Sample
4

1 回答 1

1

首先,服务条款禁止您使用 Google Maps API:

https://developers.google.com/maps/terms

禁止大量下载或批量提供内容:(...) 例如,您不得提供使用 Maps API 中包含的内容的批量地理编码服务。

还:

(g) 不得使用没有 Google 地图的内容。如果没有相应的 Google 地图,您不得使用或显示内容

您有一些选择:

http://developer.mapquest.com/web/products/open

http://services.gisgraphy.com/public/geocoding.html

关于您的具体地理编码问题,结果是否会随时间变化?如果不是这种情况,则可能存在解析错误 - 或者只是地址无法精确定位在相同坐标上。另一方面,在每个请求之后延迟可能会有所帮助。

于 2012-10-16T08:02:43.530 回答