Bug fix: addTileOverlay getTile function not always working as expected.
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.1.0-beta-20170925-1216" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+<plugin id="cordova-plugin-googlemaps" version="2.1.0-beta-20170927-1927" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-googlemaps</name>
<js-module name="BaseClass" src="www/BaseClass.js">
<runs />

MODIFY
src/android/plugin/google/maps/PluginTileOverlay.java
src/android/plugin/google/maps/PluginTileOverlay.java
@@ -162,8 +162,8 @@ public class PluginTileOverlay extends MyPlugin implements MyPluginInterface {
* @throws JSONException
*/
public void remove(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
- String id = args.getString(0);
- final TileOverlay tileOverlay = (TileOverlay)pluginMap.objects.get(id);
+ final String id = args.getString(0);
+ TileOverlay tileOverlay = (TileOverlay)pluginMap.objects.get(id);
if (tileOverlay == null) {
callbackContext.success();
return;
@@ -171,19 +171,22 @@ public class PluginTileOverlay extends MyPlugin implements MyPluginInterface {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
-
- tileOverlay.remove();
+ TileOverlay tileOverlay = (TileOverlay)pluginMap.objects.get(id);
tileOverlay.clearTileCache();
+ tileOverlay.remove();
+ tileOverlay = null;
try {
String id = args.getString(0);
pluginMap.objects.remove(id);
id = id.replace("tileoverlay_", "tileprovider_");
+
if (pluginMap.objects.containsKey(id)) {
((PluginTileProvider)(pluginMap.objects.get(id))).remove();
}
//pluginMap.objects.put(id, null);
- pluginMap.objects.remove(id);
+ PluginTileProvider provider = (PluginTileProvider) pluginMap.objects.remove(id);
+ provider = null;
callbackContext.success();
} catch (JSONException e) {
e.printStackTrace();

MODIFY
src/android/plugin/google/maps/PluginTileProvider.java
src/android/plugin/google/maps/PluginTileProvider.java
@@ -15,15 +15,11 @@ import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;
-import android.webkit.JavascriptInterface;
-import android.webkit.WebView;
import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileProvider;
-import org.apache.cordova.CordovaResourceApi;
import org.apache.cordova.CordovaWebView;
-import org.apache.cordova.PluginEntry;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -31,13 +27,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
-import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
public class PluginTileProvider implements TileProvider {
+ private final String TAG = "TileProvider";
private int tileSize = 512;
private Paint tilePaint = new Paint(Paint.FILTER_BITMAP_FLAG);
private Paint debugPaint = null;
@@ -55,6 +51,7 @@ public class PluginTileProvider implements TileProvider {
private final Object semaphore = new Object();
private Bitmap emptyBitmap = null;
private final HashSet<String> cacheKeys = new HashSet<String>();
+ private boolean isRemoved = false;
@SuppressLint({"NewApi", "JavascriptInterface"})
public PluginTileProvider(String mapId, String pluginId, CordovaWebView webView, AssetManager assetManager, String webPageUrl, String userAgent, int tileSize, boolean isDebug) {
@@ -110,6 +107,7 @@ public class PluginTileProvider implements TileProvider {
}
public void remove() {
+ isRemoved = true;
synchronized (cacheKeys) {
Iterator<String> iterator = cacheKeys.iterator();
String cacheKey;
@@ -124,6 +122,7 @@ public class PluginTileProvider implements TileProvider {
}
}
tileCache.evictAll();
+
}
public void setOnCacheClear(OnCacheClear listener) {
this.listener = listener;
@@ -131,6 +130,9 @@ public class PluginTileProvider implements TileProvider {
@Override
public Tile getTile(int x, int y, int zoom) {
+ if (isRemoved) {
+ return null;
+ }
String urlStr = null;
String originalUrlStr = null;
@@ -146,7 +148,7 @@ public class PluginTileProvider implements TileProvider {
}
});
try {
- semaphore.wait(10 * 1000); // Maximum wait 10sec
+ semaphore.wait(1000); // Maximum wait 1sec
} catch (InterruptedException e) {
e.printStackTrace();
return null;
@@ -204,6 +206,8 @@ public class PluginTileProvider implements TileProvider {
redirect = false;
http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("GET");
+ http.setReadTimeout(3000);
+ http.setConnectTimeout(3000);
if (cookies != null) {
http.setRequestProperty("Cookie", cookies);
}

Bug fix: addTileOverlay getTile function not always working as expected. #1743