こんにちは、データベースから住所を取得するこの Java プログラムを持っています。Google ジオコーディング API にリクエストを送信してから、緯度、経度の座標を確認したいと考えています。
また、一部の住所は Google で見つけることができないため、Google が町と通りで住所を見つけることができない場合は、町の名前だけでリクエストを行います。
ここに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 のアドレスの例として、この応答を見てください
ゼロの結果が見つかりました - towwn のみで NEW REQUEST の新しい URL を作成します: http://maps.google.com/maps/api/geocode/xml?address=%D0%9F%D0%BB%D0%BE%D0%B2% D0%B4%D0%B8%D0%B2&センサー=false
町ノードの長さのみを使用した新しい検索後の EHOOO: 0
withtown のみでリクエストを行った後 - 再びゼロの結果
jjj1522
URL をクリックすると、どちらの場合も Google がアドレスを見つけていることがわかります。