2013-03-05 1 views
0

데이터베이스에서 주소를 가져 와서 주소를 얻기 위해 Google 지오 코드 api에 요청하는 프로그램입니다.Google 지오 코딩 API의 문제

내가 여기있는 실수는 내가 누구를 도와 줄 수 있는지, 어떻게하면 Google에서 응답을 더 정확하게 만들 수 있는지 알려주는 것입니다.

  1. 그것은 도시를 무시하고 같은 이름의 stret의 중간막을 반환하지만 예를

다른 마을 에 비딘, Perushtica 5는 5

Perushtica을 하스 코보의 중간막을 반환

2. 나는 가끔 오늘 예를 들어 1000 개의 주소를 가지고 있고 결과를 볼 때 google이 잘 형식화 된 주소를 찾지 못했음을 알게된다. Google 주소 https://maps.google.bg/maps?hl=en&tab=wl에 가서 주소를 입력하면 완벽하게 발견된다. . 그리고 오늘 나는이 주소 중 하나를 확인하기로 결정했는데 이번에 발견합니다. 왜 때때로 https://developers.google.com/maps/faq#geocoder_differences

두 개의 서로 다른 서비스가 서로 다른 데이터 세트를 사용 참조하지

내 코드 # 2의

public class TestCon { 
     static URL url; 
     static String nodeString=""; 
     static String nody=""; 
     //static String[] nodeString={}; 
     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=1;j<1000;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=""; 

      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(urlString); 

     // Convert the string to a URL so we can parse it 
     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 ("You're here"); 
      urlString = GEOCODE_REQUEST_PREFIX + "?address=" + URLEncoder.encode(townstr, "UTF-8") + "&sensor=false"; 
      //System.out.println(urlString); 

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

      geocoderResultDocument = makeConnection(url); 
      nodes = process(geocoderResultDocument, xPathString); 
      nodes2 = process(geocoderResultDocument, xi); 

     } 

     if ((nodes.getLength()>1)&&(nodes2.getLength()>1)) 
     { 
      System.out.println("morethan one"); 
     } 

     // Print results 
     //for (int i = 0; i < nodes.getLength(); i++) 
     //{ 
      nodeString = nodes.item(0).getTextContent(); 
      // nodeString = nodes(i).getTextContent(); 
      System.out.println("lat_cordinate:" + nodeString); 


     //} 

     //for (int i = 0; i < nodes2.getLength(); i++) 
     //{ 
      nody = nodes2.item(0).getTextContent(); 
      // nodeString = nodes(i).getTextContent(); 
      System.out.println("longitude:" + nody); 

     //} 



     } 
    } 

    private void TestCon() { 
    } 

    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; 
    } 

} 

답변

0

때때로을 찾습니다.

# 1의 경우 지오 코더가 한 도시에서 주소를 찾을 수 없으므로 근처의 다른 도시를 시도했을 수 있습니다.