[英]Trouble converting addresses to lat/long with Google Geocoding API.
我有一个应用程序,它将读取包含大量名称和地址(800多个)的Excel电子表格,使用Google Geocoding API将地址转换为经/纬度,并生成带有相关航点的.kml文档。
转换大量地址时,.kml文件将以为不同地址分配了相同纬度/经度的小组结尾。 我想知道是否需要处理小批量,暂停程序并处理另一批?
public void GeocodingSample() throws IOException, XPathExpressionException, ParserConfigurationException, SAXException
{
l1 = new String[lineCount];
l2 = new String[lineCount];
//address2 = add;
//city2 = city;
bxg.UpdateTA("Converting address to lat/long...");
ProgressBar progress = new ProgressBar();
// URL prefix to the geocoder
String GEOCODER_REQUEST_PREFIX_FOR_XML = "http://maps.google.com/maps/api/geocode/xml";
// query address
address = new String[lineCount];
int x = 0;
//System.out.println("Im about to enter the loop and the count is: "+lineCount);
while(x < lineCount)
{
pbarvalue = x + 1;
address[x] = (sheet[x][a2]+", "+sheet[x][s2]);
progress.PbarValue(pbarvalue, lineCount, address[x]);
url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address[x], "UTF-8") + "&sensor=false");
// prepare a URL to the geocoder
//URL url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");
// prepare an HTTP connection to the geocoder
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();
}
// prepare XPath
XPath xpath = XPathFactory.newInstance().newXPath();
// extract the result
NodeList resultNodeList = null;
// a) obtain the formatted_address field for every result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
//System.out.println(resultNodeList.item(i).getTextContent());
}
// b) extract the locality for the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text()='locality']/long_name", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
//System.out.println(resultNodeList.item(i).getTextContent());
}
// c) extract the coordinates of the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
}
//System.out.println("lat/lng=" + lat + "," + lng);
l1[x] = (""+lat);
l2[x] = (""+lng);
// c) extract the coordinates of the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text() = 'administrative_area_level_1']/country[short_name/text() = 'US']/*", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
}
//System.out.println("lat/lng=" + lat + "," + lng);
x++;
}//end While
//System.out.println("The output is ready");
progress.ShowFrame(false);
OutputKML();
}// end GeoCoding Sample
首先,服务条款禁止您使用Google Maps API:
https://developers.google.com/maps/terms
没有大量下载或大量内容馈送:(...)例如,不允许您提供使用Maps API中包含的内容的批量地理编码服务。
也:
(g)没有Google Map,不得使用内容。 没有相应的Google地图,您不得使用或显示内容
您有一些选择:
http://developer.mapquest.com/web/products/open
http://services.gisgraphy.com/public/geocoding.html
关于您的特定地理编码问题,结果会随时间变化吗? 如果不是这种情况,则可能存在解析错误-或根本无法精确定位以相同坐标结尾的地址。 另一方面,在每个请求之后延迟可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.