你好,我有这个 java 程序,它从数据库中获取地址,对谷歌地理编码 api 进行请求,然后我想查看 lat、lng 坐标。
此外,由于谷歌无法找到某些地址 - 所以我决定如果谷歌无法找到城镇和街道的地址,我只使用城镇名称提出请求。
这是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 的地址
包含完整地址的第一个 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+&传感器=假
发现零结果 - 仅使用城镇创建新请求新网址: 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
仅在withtown提出请求后-再次为零结果
jjj1522
如果你点击网址,你会发现谷歌在两次都找到了地址——但是正如你看到的程序给我的,谷歌没有找到地址——我不明白为什么