名前と住所の大規模なリスト (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