Ich habe ein JAVA-Programme beim Extrahieren von Daten von URL.Java im Erhalten von Orten Informationen von Google Map
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();
}
}
Dann rufe ich auch eine Java-Klasse LatLng für Breiten- und Längengrad von Google Karte api zu nennen.
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;
}
}
Allerdings gibt es mir die Nachricht, die Anforderungsbeschränkung für den API-Schlüssel zu erreichen. Gibt es noch eine andere Methode, die ich genauso machen kann?
vielleicht sollten Sie zum Beispiel Sigleton verwenden, um zu verhindern, dass jedes Mal, wenn Sie diese Funktion aufrufen, eine Verbindung hergestellt wird. Und sicher solltest du etwas über OOP lesen. –
Bezahlen Sie ein Abonnement für Google-Dienste, damit Sie alle benötigten Daten anfordern können. Kostenlose API KEYs sind für Entwicklungszwecke und haben ein Anforderungslimit. –
@ shutdown-hnow Können Sie bitte ein bisschen mehr erklären. Ich bin nicht diese Expoertise in der Verwendung mit Google Map API und JAVA –