簡體   English   中英

使用Google Geocoding API無法將地址轉換為經/緯度。

[英]Trouble converting addresses to lat/long with Google Geocoding API.

我有一個應用程序,它將讀取包含大量名稱和地址(800多個)的Excel電子表格,使用Google Geocoding 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

首先,服務條款禁止您使用Google Maps API:

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

沒有大量下載或大量內容饋送:(...)例如,不允許您提供使用Maps API中包含的內容的批量地理編碼服務。

也:

(g)沒有Google Map,不得使用內容。 沒有相應的Google地圖,您不得使用或顯示內容

您有一些選擇:

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

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

關於您的特定地理編碼問題,結果會隨時間變化嗎? 如果不是這種情況,則可能存在解析錯誤-或根本無法精確定位以相同坐標結尾的地址。 另一方面,在每個請求之后延遲可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM