Geocoding API for Java

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

These are the lines of code that I wrote, and it works great 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 he does is what I give in the text, for example, a new York, and he discovers the longitude and latitude of this area.

However, when I put the same lines of code in GAE, sometimes when I run this code, I get a validation constraint exception located in " GeocoderResult data = someList.get(0) ";

Sometimes I don’t get an error and correctly displays the coordinates on a web page. So, I'm a bit confused, on the website, does it show that it supports GAE, or is there something wrong with the geocoder provided by Google itself, has some problems?

This usually does not work in the afternoon or at midnight Eastern time.

+6
source share
2 answers

I suggest that you use the API limits here. API speed limits are set for each IP address, and infrastructures such as the Google App Engine usually use a common IP address to request a server, therefore, while your application itself does not affect these restrictions, for the general infrastructure, it probably falls under these restrictions, and you will not get any results.

+4
source

Your answer GeocodeResponse geocoderResponse has a status that is always useful to check in code. Usually the status should always be returned, otherwise their system does not work.

Possible status:

  • GeocoderStatus.OK (this is obvious)
  • GeocoderStatus.ZERO_RESULTS (not found for your address)
  • ... (there are several more possible conditions)
  • GeocoderStatus.OVER_QUERY_LIMIT

The latter may be important to you, because it indicates that you send a lot of requests per second or you have reached your daily limit (you can pay for the key and update this daily limit, nothing is really free)

To avoid problems with many requests 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’s good to have this code anyway

+3
source

Source: https://habr.com/ru/post/943854/


All Articles