Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
地理编码:如何避免谷歌地图api-java中的超查询限制_Java_Google Maps_Geocoding - Fatal编程技术网

地理编码:如何避免谷歌地图api-java中的超查询限制

地理编码:如何避免谷歌地图api-java中的超查询限制,java,google-maps,geocoding,Java,Google Maps,Geocoding,我有一个函数,它可以给我一个lat long from地址: public static String[] getLatLongPositions(String address) throws Exception { int responseCode = 0; String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + URLEncoder.encode(address, "UTF-8"

我有一个函数,它可以给我一个lat long from地址:

 public static String[] getLatLongPositions(String address) throws Exception
  {
    int responseCode = 0;
    String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=true";
    //System.out.println("URL : "+api);
    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("/GeocodeResponse/status");
      String status = (String)expr.evaluate(document, XPathConstants.STRING);
      if(status.equals("OK"))
      {
         expr = xpath.compile("//geometry/location/lat");
         String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
         expr = xpath.compile("//geometry/location/lng");
         String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
         return new String[] {latitude, longitude};
      }
      else
      {
         throw new Exception("Error from the API - response status: "+status+"street "+ address);
      }
    }
    return null;
  }
但我经常会超过查询限制,所以我会尝试睡眠片刻,然后再次运行我的函数,添加以下内容:

if(status.equals("OVER_QUERY_LIMIT")){
              Thread.sleep(1000);
              getLatLongPositions(address);
          }
但是当getLatLongPositions再次递归运行时,我在这一行得到null
responseCode=httpConnection.getResponseCode()
我怎样才能避免这个问题?
此外,在这个函数中,一些地址没有错误,但当我再次运行这个函数时,我发现这些地址有错误,而这些地址以前很好。

这是我在使用这个API时学到的:

  • 这是“慢”。我的意思是,这可能是一个昂贵的操作
  • 谷歌建议不要每次都进行地理编码,而是在创建点时存储(lat,long)点
  • 谷歌有每天最多2500个地理编码请求的配额
  • 在一个循环中发送超过10个地理编码请求后,GoogleAPI抛出了一个OVER_QUERY_LIMIT异常

  • 请阅读,您可以在github上找到Google地图web服务的Java客户端:

    虽然这是一个开源项目,但它是由Google编写的,提供了许多您可以使用的功能。特别是,您可以在GeoApiContext中为您的请求设置速率限制:
    queryRateLimit(int-maxQps)

    此客户端库将限制您的请求,使其保持在允许的QPS限制内

    阅读docs和javadoc并尝试一下

    我希望这有帮助

    只需返回值:

    else if(status.equals("OVER_QUERY_LIMIT"))
    {
        Thread.sleep(1000);
        return getLatLongPositions(address);
    }
    
    为我工作