hello I've got this java programe which gets the addresse from a database,make requeset to google geocoding api and then I want to see the lat, lng cordinates.
Also as some addresses are not able to be found by google - so i've decided if google is unable to find the address with town and street, i make request only with the town name.
here is the 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;
}
The problem is with the response I get:
I've tested the addresses and the cordinates and most of teh time thety are right and the response is right. But at some point i notice this weird situation- LOOK THIS RESPONSE FOR EXAMPLE FOR address with id=1512
FIRST URL WITH THE WHOLE ADDRESS: 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 RESULTS FOUND - making NEW REQUEST new url only with 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 after the new search only with town nodes length is: 0
After making an request only withtown - again zero result
jjj1522
if you click on the urls you will se that at both times the gooogle finds the address - but as you see the program gives me that google hasn't found the address - and i can't understand why