URLからデータを抽出するためのJavaプログラムがあります。Googleマップから場所情報を取得するJava
Extract.java
public class Uni_Extract {
protected static String lat;
protected static String lng;
public static void main(String[] args) throws Exception {
System.out.println("Started");
String csvFile = "C://Users/Kennedy/Desktop/university.csv";
FileWriter writer = new FileWriter(csvFile);
for (int i=2; i<=2; i++){
String url = "http://www.4icu.org/reviews/index"+i+".htm";
Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
Elements cells = doc.select("td.i");
Iterator<Element> iterator = cells.iterator();
while (iterator.hasNext()) {
Element cell = iterator.next();
String university = Jsoup.parse((cell.select("a").text())).text();
String country = cell.nextElementSibling().select("img").attr("alt");
LatLngBackup LatLngBackup = new LatLngBackup();
LatLngBackup.getLatLongPositions(university);
System.out.print(university);
System.out.printf(", lat : %s, lng : %s %n", lat, lng);
CSVUtils.writeLine(writer,Arrays.asList(country,university,lat,lng),',','"');
}
}
writer.flush();
writer.close();
}
}
それから私はまた、GoogleマップAPIからの緯度及び経度を求めるためにJavaクラス緯度経度を呼び出します。
public class LatLngBackup extends Uni_Extract
{
public String[] getLatLongPositions(String address) throws Exception
{
int responseCode = 0;
String api = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=" + URLEncoder.encode(address, "UTF-8") +
"&key=AIzaSyCTBvomwAMvq0pSxgN0y2I_wALFJ8cx57Y";
URL url = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.connect();
responseCode = httpConnection.getResponseCode();
if(responseCode == 200)
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();;
Document document = builder.parse(httpConnection.getInputStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/PlaceSearchResponse/status");
String status = (String)expr.evaluate(document, XPathConstants.STRING);
if(status.equals("OK"))
{
expr = xpath.compile("//geometry/location/lat");
super.lat = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile("//geometry/location/lng");
super.lng = (String)expr.evaluate(document, XPathConstants.STRING);
return new String[] {lat, lng};
}
else
{
super.lat="";
super.lng="";
}
}
return null;
}
}
ただし、APIキーの到達制限のメッセージが表示されます。私が同じことをすることができる他の方法はありますか?
おそらく、この関数を呼び出すたびに接続を禁止するためにsigletonなどを使用する必要があります。そして、あなたはOOPについて何かを読んでください。 –
Googleサービスに定期購読して、必要なすべてのデータをリクエストできます。無料のAPIキーは開発目的であり、要求の制限があります。 –
@ shutdown-hnowもう少し説明してください。私はGoogle Maps APIとJAVAで使い慣れているわけではありません。 –