3
votes

I am working on play framework 2.1.1 (java) and googlemap v3 (local website).

I want to get latitude and longitude using googlemap url. I want this url return me json response to parse it later and insert lat and long in my database. It works fine when it's not a googlemap url but i have the famous "request_denied" error when I use this kind of url in my code:

http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=false

I don't know why, I read this error come from the lack of the sensor parameter but as you can see it's in my url.

About my code :

public static Result displaySomeJson(){
    Promise<WS.Response> request= WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=false").get();
    JsonNode json = request.asJson();
    return ok(json.toString())
}

it gives me : {"results" : [],"status" : "REQUEST_DENIED"}

  • I also tried with http ://maps.GOOGLE.com instead of http ://maps.GOOGLEAPIS.com but i got the same result.
  • I also tried using an api key but it failed again.
  • I didn't send to much request.

If you have any suggestion, please share.

2

2 Answers

2
votes

ok i found this way :

Promise<WS.Response> request =
WS.url("http://maps.googleapis.com/maps/api/geocode/json")
.setQueryParameter("address",adresse).setQueryParameter("sensor", "true")
.post("content");

Then i parsed it :

String lat = request.get().asJson().path("results").get(0).get("geometry").get("location").get("lat").toString();
String long = request.get().asJson().path("results").get(0).get("geometry").get("location").get("lng").toString();

I don't know if it's a "clear" way to do that, but it works fine. :)

0
votes

I had the same problem with Play 2. After changing the WS.url to the old fashioned way with a HttpUrlConnection it worked. Don't know why the WS.url is not working.

URL theUrl = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=false");
HttpURLConnection urlConn = (HttpURLConnection) theUrl.openConnection();
...