Je fonction qui me donne LAT longue de l'adresse:GÉOCODAGE: Comment éviter OVER_QUERY_LIMIT sur la carte google api -java
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;
}
Mais très souvent, je reçois OVER_QUERY_LIMIT donc j'essayer dormir un instant et courir à nouveau ma fonction ajoutant ceci:
if(status.equals("OVER_QUERY_LIMIT")){
Thread.sleep(1000);
getLatLongPositions(address);
}
Mais quand getLatLongPositions est en cours d'exécution à nouveau récursive je reçois null à cette ligne responseCode = httpConnection.getResponseCode();
Comment puis-je éviter ce problème? Aussi dans cette fonction certaines adresses sont sans erreur mais quand je réexécute cette fonction j'obtiens l'erreur avec ces adresses qui étaient bonnes avant.