2016-08-01 9 views
0

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キーの到達制限のメッセージが表示されます。私が同じことをすることができる他の方法はありますか?

+0

おそらく、この関数を呼び出すたびに接続を禁止するためにsigletonなどを使用する必要があります。そして、あなたはOOPについて何かを読んでください。 –

+0

Googleサービスに定期購読して、必要なすべてのデータをリクエストできます。無料のAPIキーは開発目的であり、要求の制限があります。 –

+0

@ shutdown-hnowもう少し説明してください。私はGoogle Maps APIとJAVAで使い慣れているわけではありません。 –

答えて

0

「APIキーの到達制限のメッセージが届きます。」

Usage limits

GoogleプレイスAPI Webサービスを使用すると、無料で増やすことができる24時間あたり1つの000 要求のデフォルトの制限が適用されます。 アプリが制限を超えると、アプリの起動に失敗します。 身元を確認して、Google APIコンソールで の請求を有効にして、24時間に150,000件のリクエストを取得してください。 確認にはクレジットカードが必要です。純粋にお客様のクレジットカードに の身元を確認するようお願いいたします。 Googleプレイスの APIウェブサービスをご利用の場合、カードに請求されることはありません。

無料使用の制限は、24時間あたり150,000リクエストです。 アプリが制限を超えると、アプリは再び失敗します。 GoogleマップAPIプレミアムプランのライセンスを購入して、24時間に150,000個以上の リクエストを取得してください。

無料の使用方法はこれまでのところあなたを得ることができます。もっと必要な場合は、Premium Licenseを支払うことを検討してください。

追加情報: 使い方と制限の詳細については、hereを参照してください。

関連する問題