2013-03-07 15 views
-2

你好我有這個java程序,它從數據庫中獲取地址,向谷歌地理編碼API請求,然後我想看看經緯度座標。爲什麼谷歌地理編碼無法找到一些地址

此外,由於有些地址無法通過谷歌找到 - 所以我已經決定,如果谷歌無法找到鎮和街道的地址,我只請求鎮名。

這裏是java PROGRAME

public class TestCon { 
    static String nodeString=""; 
    static String nody=""; 

    static String elementValue = ""; 
    static String townstr=""; 
    static String streetstr=""; 
    static String urlString; 
    static Document geocoderResultDocument; 

    static NodeList nodes2; 
    static Connection conn2 = null; 
    static NodeList nodes; 
    private static final String GEOCODE_REQUEST_PREFIX = "http://maps.google.com/maps/api/geocode/xml"; 



    public String _xpath = null; 
    public Document _xml = null; 

    public static void main(String[] args) throws IOException, URISyntaxException, ParserConfigurationException, SAXException, ClassNotFoundException, SQLException 
    {   Class.forName("org.postgresql.Driver"); 

     try 
     { 
      conn2 = DriverManager.getConnection(
        "jdbc:postgresql://localhost:5432/plovdivbizloca", 
        "postgres", "tan"); 
     } 

     catch (SQLException ex) 
     { 

      ex.printStackTrace(); 
     } 

     for (int j=1500;j<1550;j++) 

     { 
      try 
     { 
     conn2 = DriverManager.getConnection(
        "jdbc:postgresql://localhost:5432/plovdivbizloca", 
        "postgres", "tan"); 
     } 

     catch (SQLException ex) 
     { 

      ex.printStackTrace(); 
     } 

     Statement mystmt = null; 

     String selectQuery = "SELECT main_office_town, address FROM pl_biz where id="+j; 

     try 
     { 
      mystmt = conn2.createStatement(); 
      ResultSet mysr = mystmt.executeQuery(selectQuery); 

      ResultSetMetaData rsmd = mysr.getMetaData(); 
      int colCount = rsmd.getColumnCount(); 

      elementValue=""; 
      townstr=""; 

      while (mysr.next()) 
      { 

       for (int i = 1; i <= colCount; i++) 
       { 

        elementValue += mysr.getString(i); 
        if (i < colCount) 
         elementValue += ","; 
       } 
       townstr = mysr.getString(1); 
       streetstr = mysr.getString(2); 
       //System.out.println(elementValue); 
       //System.out.println(townstr); 
       //System.out.println(streetstr); 


      } 

     } 
     catch (Exception ex) 
     { 
     } 
     // NEW GEOCODING; 

     String inputQuery, resultXml, xPathString, xi = null; 
     inputQuery = elementValue; 

     urlString = GEOCODE_REQUEST_PREFIX + "?address=" + URLEncoder.encode(inputQuery, "UTF-8") + "&sensor=false"; 
     System.out.println("FIRST URL WITH THE WHOLE ADDRESS: "+urlString); 

     // Convert the string to a URL so we can parse it 
     URL url = new URL(urlString); 

     geocoderResultDocument = makeConnection(url); 


     // Process the results 
     xPathString = "//GeocodeResponse//location/lat"; 
     nodes = process(geocoderResultDocument, xPathString); 
     xi = "//GeocodeResponse//location/lng"; 
     nodes2 = process(geocoderResultDocument, xi); 

     if ((nodes.getLength()==0)&&(nodes2.getLength()==0)) 
     { 
      System.out.println ("ZERO RESULTS FOUND - making NEW REQUEST"); 
      urlString = GEOCODE_REQUEST_PREFIX + "?address=" + URLEncoder.encode(townstr, "UTF-8") + "&sensor=false"; 
      System.out.println("new url only with towwn" + urlString); 
      //System.out.println(townstr); 
      // Convert the string to a URL so we can parse it 
      URL url2 = new URL(urlString); 

      geocoderResultDocument = makeConnection(url2); 
      System.out.println("EHOOO"); 
      nodes = process(geocoderResultDocument, xPathString); 
      nodes2 = process(geocoderResultDocument, xi); 
      System.out.println("after teh new search only with town nodes length is: "+nodes.getLength()); 
     } 


     if ((nodes.getLength()>=1)&&(nodes2.getLength()>=1)) 
     { 
      if ((nodes.getLength()>1)&&(nodes2.getLength()>1)) 

        {System.out.println("more than one result found - get the first closest");} 


     // Print results 
         nodeString = nodes.item(0).getTextContent(); 

      System.out.println("lat_cordinate:" + nodeString); 



        nody = nodes2.item(0).getTextContent(); 
         System.out.println("longitude:" + nody); 


     } 
     else 
     { 

      System.out.println("After making an request only with town - again zero result"); 
     } 

     System.out.println("jjj"+j); 


     } 
    } 



    public static NodeList process(Document xml, String xPathStrings) 
      throws IOException { 

     NodeList result = null; 



     XPathFactory factory = XPathFactory.newInstance(); 

     XPath xpath = factory.newXPath(); 

     try { 
      result = (NodeList) xpath.evaluate(xPathStrings, xml, 
        XPathConstants.NODESET); 
     } 
     catch (XPathExpressionException ex) { 
      ex.printStackTrace(); 
     } 

     return result; 
    } 

    public static Document makeConnection(URL url) throws IOException, SAXException, ParserConfigurationException  
    { 
    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(); 
    } 

    return geocoderResultDocument; 
    } 

的問題是與響應我得到:

我測試過的地址和cordinates最TEH時間thety的是正確的,反應是對。但在某些時候我發現這個奇怪的情勢尋找與ID = 1512


與整個首先處理URL地址。例如這樣的響應:http://maps.google.com/maps/api/geocode/xml?address=%D0%9F%D0%BB%D0%BE%D0%B2%D0%B4%D0%B8%D0%B2%2C%D1%83%D0%BB.+%D0%94%D0%98%D0%9B%D0%AF%D0%9D%D0%9A%D0%90+2%D0%90+&sensor=false

ZERO找到的結果 - 使新的請求 新網址僅與towwn:僅與鎮節點長度新的搜索之後http://maps.google.com/maps/api/geocode/xml?address=%D0%9F%D0%BB%D0%BE%D0%B2%D0%B4%D0%B8%D0%B2&sensor=false

EHOOO 是:0

製備請求ONL後Ÿwithtown - 再次零結果

jjj1522


如果你點擊的URL,你會本身,在這兩個時間的Gooogle企業找到了地址 - 但正如你看到的節目給了我,谷歌不是招」 t上找不到的地址 - 我不明白爲什麼

回答

0

嘗試將它們轉換爲英文地址...使用的地址,建議在結果而不是編碼俄語地址..