使用Google Geocoding API将地址转换为纬度/经度时遇到问题。

时间:2012-08-23 20:34:15

标签: java geocoding

我有一个应用程序,它将读取包含大量名称和地址(800+)的Excel电子表格,使用Google地理编码API将地址转换为lat / long,并生成带有相关航点的.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

1 个答案:

答案 0 :(得分:1)

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

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

  

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

此外:

  

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

您有一些替代方案:

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

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

关于您的特定地理编码问题,结果是否会及时更改?如果不是这种情况,可能会出现解析错误 - 或者只是地址无法精确定位在相同的坐标上。 另一方面,在每次请求后延迟可能有所帮助。