为什么谷歌地理编码找不到一些地址

时间:2013-03-07 14:12:27

标签: java google-geocoding-api

你好我有这个java程序从数据库中获取地址,对谷歌地理编码api进行requeset然后我想看看lat,lng cordinates。

此外,由于某些地址无法通过谷歌找到 - 所以我已经决定,如果谷歌无法找到城镇和街道的地址,我只提出城镇名称请求。

这是java程序

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

问题在于我得到的回应:

我测试了地址和坐标,大部分时间都是正确的,反应是对的。但在某些时候我注意到这种奇怪的情况 - 看看这个回复的例子,地址为id = 1512


整个地址的第一个网址: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

发现零结果 - 提出新请求 只有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

仅在提出请求之后 - 再次为零结果

jjj1522


如果您点击网址,您将会在gooogle找到地址的两个时间点 - 但正如您所看到的程序告诉我谷歌没有找到地址 - 我无法理解为什么< /强>

1 个答案:

答案 0 :(得分:0)

尝试将它们转换为英语地址...使用结果中建议的地址而不是编码的俄语地址..