gpt4 book ai didi

java - 使用 Google 地理编码 API 将地址转换为纬度/经度时遇到问题。

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:48 24 4
gpt4 key购买 nike

我有一个应用程序,可以读取包含大量名称和地址 (800+) 的 Excel 电子表格,使用 Google 地理编码 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

No Mass Downloads or Bulk Feeds of Content: (...) For example, you are not permitted to offer a batch geocoding service that uses Content contained in the Maps API(s).

另外:

(g) No Use of Content without a Google Map. You must not use or display the Content without a corresponding Google map

您有一些替代方案:

http://developer.mapquest.com/web/products/open

http://services.gisgraphy.com/public/geocoding.html

关于您的具体地理编码问题,结果会随时间变化吗?如果不是这种情况,可能存在解析错误 - 或者只是无法精确定位以相同坐标结尾的地址。另一方面,在每个请求之后延迟可能会有所帮助。

关于java - 使用 Google 地理编码 API 将地址转换为纬度/经度时遇到问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com