2016-08-01 110 views
0

我有一个JAVA程序从URL中提取数据。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(); 
    } 
} 

然后我还呼吁Java类经纬度呼吁从谷歌地图API的经度和纬度。

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

支付订阅谷歌服务,所以你可以要求所有你需要的数据。免费的API密钥用于开发目的,并有一个请求限制。 –

+0

@ shutdown-hnow可否请您解释一下。我不是那种使用Google Map API和JAVA的expoertise –

回答

0

“它给我提供了API密钥请求限制的消息。”

Usage limits

的谷歌Places API的Web服务强制执行每24小时1个000 请求,这可以增加免费的默认限制。如果您的应用超出限制,该应用将启动失败。通过在Google API控制台上启用 结算功能,验证您的身份,从而每24小时内最多可获得150 000个请求。 验证需要信用卡。我们要求您的信用卡纯粹是为了验证您的 身份。您的卡不会因使用Google Places API Web服务而收取费用。

免费使用的限制是每24小时150 000个请求。如果你的应用程序超出限制,该应用程序将再次启动失败。购买 Google Maps APIs Premium Plan许可证,每24小时可获得超过150 000个 的请求。

免费使用只会让你到目前为止。考虑支付Premium License如果你想要更多。

附加阅读: 有关使用和限制的完整信息,请参阅here