-1

I have function that give me lat long from address:

 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;
  }

But very often i get OVER_QUERY_LIMIT so i try sleep for a moment and run my function again adding this:

if(status.equals("OVER_QUERY_LIMIT")){
              Thread.sleep(1000);
              getLatLongPositions(address);
          }

But when getLatLongPositions is running again recursively i get null at this line responseCode = httpConnection.getResponseCode();
How can i avoid this problem? Also in this function some addresses are without error but when i run again this function i get error with these addresses which were good before.

JavaCoder
  • 135
  • 2
  • 8

3 Answers3

1

This is what I learned when I worked with this API:

  1. It is "slow". I mean, this can be a costly operation.
  2. Google recommends not to geocode every time, but to store the (lat, long) points when they are created.
  3. Google has a quota of 2500 daily maximum geocoding requests.
  4. The Google API throws a OVER_QUERY_LIMIT exception after sending more than 10 geocoding requests in a loop.

Please read OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down? and How do I Geocode 20 addresses without receiving an OVER_QUERY_LIMIT response?

Sergio Muriel
  • 1,039
  • 9
  • 15
1

There is a Java client for Google Maps web services that you can find on github:

https://github.com/googlemaps/google-maps-services-java

Although this is an open source project, it was written by Googlers and provides many features that you can use. Particularly you can set a rate limit for your requests in the GeoApiContext: queryRateLimit(int maxQps)

https://googlemaps.github.io/google-maps-services-java/v0.2.3/javadoc/

This client library will throttle your requests to stay within the allowed QPS limits.

Read docs and javadoc and try it.

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117
0

Just return the value:

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

Worked for me!