[英]Reverse Geocoding using Java in localhost
我正在尝试使用Java逆向地理代码,但由于连接超时而无法执行。该程序在locahost:8080上运行,它使我连接超时。 请让我知道这个问题。谢谢
该程序如下
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class ReverseGeo {
public static void main(String ar[]) throws Exception {
System.out.println(new ReverseGeo().getAddress("13.031067,80.239656"));
}
public String getAddress(String latlong){
String address = null;
String gURL = "http://maps.google.com/maps/api/geocode/xml?latlng=" + latlong + "&sensor=true";
try {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder db = df.newDocumentBuilder();
Document dom = db.parse(gURL);
Element docEl = dom.getDocumentElement();
NodeList nl = docEl.getElementsByTagName("result");
if (nl != null && nl.getLength() > 0){
address=((Element)nl.item(0)).getElementsByTagName("formatted_address").item(0).getTextContent();
for(int i=0;i<nl.getLength();i++){
String temp=((Element)nl.item(i)).getElementsByTagName("formatted_address").item(0).getTextContent();
}
}
} catch (Exception ex) {
System.out.println("Error"+ex);
address = "Err";
}
return address;
}
public String getAddress(String lat, String lon) {
return getAddress(lat+ "," + lon);
}
public String getAddress(double lat, double lon) {
return getAddress("" + lat, "" + lon);
}
}
错误是
Errorjava.net.ConnectException: Connection timed out: connect
Err
将以下代码添加到您的代码中..不会有错误
System.setProperty("http.proxyHost", " proxy name");
System.setProperty("http.proxyPort", " Port number");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.