update: prevent forever looping
MODIFY
src/android/plugin/google/maps/PluginGeocoder.java
src/android/plugin/google/maps/PluginGeocoder.java
@@ -4,6 +4,7 @@ import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
+import android.util.Log;
import com.google.android.gms.maps.model.LatLngBounds;
@@ -69,6 +70,7 @@ public class PluginGeocoder extends CordovaPlugin {
List<Address> geoResults = null;
JSONArray results = new JSONArray();
Iterator<Address> iterator = null;
+ int retryLimit = 10;
// Geocoding
if (!opts.has("position") && opts.has("address")) {
@@ -79,7 +81,7 @@ public class PluginGeocoder extends CordovaPlugin {
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
boolean retry = true;
- while (retry) {
+ while (retry && retryLimit > 0) {
retry = false;
try {
geoResults = geocoder.getFromLocationName(address, 5,
@@ -88,6 +90,7 @@ public class PluginGeocoder extends CordovaPlugin {
} catch (IOException e) {
if ("Timed out waiting for response from server".equals(e.getMessage())) {
retry = true;
+ retryLimit--;
try {
Thread.sleep((int)(150 + Math.random() * 100)); // wait (150 + random)ms before retry
} catch (InterruptedException e1) {
@@ -130,7 +133,7 @@ public class PluginGeocoder extends CordovaPlugin {
e.printStackTrace();
}
}
- if (geoResults != null && geoResults.size() == 0) {
+ if (geoResults == null || geoResults.size() == 0) {
JSONObject methodResult = new JSONObject();
methodResult.put("idx", opts.getInt("idx"));
methodResult.put("results", new JSONArray());
@@ -149,7 +152,7 @@ public class PluginGeocoder extends CordovaPlugin {
JSONObject position = opts.getJSONObject("position");
boolean retry = true;
- while (retry) {
+ while (retry && retryLimit > 0) {
retry = false;
try {
geoResults = geocoder.getFromLocation(
@@ -158,6 +161,7 @@ public class PluginGeocoder extends CordovaPlugin {
} catch (IOException e) {
if ("Timed out waiting for response from server".equals(e.getMessage())) {
retry = true;
+ retryLimit--;
try {
Thread.sleep((int)(150 + Math.random() * 100)); // wait (150 + random)ms before retry
} catch (InterruptedException e1) {
@@ -169,7 +173,10 @@ public class PluginGeocoder extends CordovaPlugin {
}
}
- if (geoResults != null && geoResults.size() == 0) {
+ if (geoResults == null || geoResults.size() == 0) {
+ //-------------
+ // No results
+ //-------------
JSONObject methodResult = new JSONObject();
methodResult.put("idx", opts.getInt("idx"));
methodResult.put("results", new JSONArray());

update: prevent forever looping