Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geocoder API for Java

I'm trying to use the Geocoder API for java located at: http://code.google.com/p/geocoder-java/

These are the lines of code I've written, and it works perfectly when I run it as a GAE web application.

    final Geocoder geocoder = new Geocoder();
    GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(req.getParameter("location").toString()).setLanguage("en").getGeocoderRequest();
    GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
    List<GeocoderResult> someList = geocoderResponse.getResults();
    GeocoderResult data = someList.get(0);
    GeocoderGeometry data_2 = data.getGeometry();
    BigDecimal Latitude = data_2.getLocation().getLat();
    BigDecimal Longitude = data_2.getLocation().getLng();

What it does is that I give in a text, for example new york, and it finds out the longitude and the latitude of that area.

However, when I put the same lines of code into the GAE, sometimes when I run this code, I get check bounds exception located at "GeocoderResult data = someList.get(0)";

Sometimes I don't get the error and it displays the coordinates on the webpage correctly. So I'm a bit confused, on the website, it shows that it supports GAE, or is there something wrong with the geocoder provided by google itself has some problem?

Usually it doesn't work during afternoon or midnight at eastern time.

like image 495
user1157751 Avatar asked Apr 29 '13 05:04

user1157751


2 Answers

I believe you are hitting API limits here. The API rate limits are set per IP Address and infrastructures such as Google App Engine commonly use a shared IP to make the request to the server, hence while your application per se does not hit those limits, because of the shared infrastructure, it probably hits those limits and you do not get any results.

like image 138
Romin Avatar answered Sep 27 '22 21:09

Romin


Your response GeocodeResponse geocoderResponse has a status, which is always usefull to verify in your code. Normally the status should always be returned, otherwise their system is down.

Possible status:

  • GeocoderStatus.OK (this is obvious)
  • GeocoderStatus.ZERO_RESULTS (no result could be found for your address)
  • ... (there are a few other possible status)
  • GeocoderStatus.OVER_QUERY_LIMIT

The last one is possibly important for you, since it indicates you are sending to many request per second or you have reached your daily limit (you can pay for a key and upgrade that daily limit, nothing is really for free)

To avoid the problem of to many request per second:

if (firstTry && GeocoderStatus.OVER_QUERY_LIMIT.equals(geocodeResponse.getStatus()) {
     Thread.sleep(2000);
     // try again..
} else { logger.error("You reached your daily limit of requests"); }

It is usefull to have this code anyway

like image 42
Fico Avatar answered Sep 27 '22 19:09

Fico