update: prevent `cordova is undefined` error
This error occurs when you move to another page during the map is still working
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170804-1452" 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.0.0-beta3-20170804-1546" 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/AsyncKmlParser.java
src/android/plugin/google/maps/AsyncKmlParser.java
@@ -509,7 +509,7 @@ public class AsyncKmlParser extends AsyncTask<String, Void, Bundle> {
@Override
public void onResult(PluginResult pluginResult) {
String jsStr = String.format(Locale.ENGLISH,
- "javascript:cordova.fireDocumentEvent('%s_%s', {class:'%s', placeMarkId:'%s', options:%s})",
+ "javascript:if(window.cordova){cordova.fireDocumentEvent('%s_%s', {class:'%s', placeMarkId:'%s', options:%s});}",
mPluginMap.mapId, kmlId, className, placeMarkId, options.toString());
Log.d("AsyncKmlParse", "--> " + jsStr);

MODIFY
src/android/plugin/google/maps/MyPluginLayout.java
src/android/plugin/google/maps/MyPluginLayout.java
@@ -577,7 +577,7 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
if (!isMapAction) {
browserView.requestFocus(View.FOCUS_DOWN);
- webView.loadUrl("javascript:(cordova && cordova.fireDocumentEvent('plugin_touch', {}));");
+ webView.loadUrl("javascript:if(window.cordova){cordova.fireDocumentEvent('plugin_touch', {});}");
}
MyPluginLayout.this.stopFlag = false;

MODIFY
src/android/plugin/google/maps/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
@@ -2131,7 +2131,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
String tmp[] = markerTag.split("_");
tmp = markerTag.split("-");
String markerId = tmp[tmp.length - 1];
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMarkerEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMarkerEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]});}",
mapId, eventName, markerId, latLng.latitude, latLng.longitude);
jsCallback(js);
}
@@ -2145,7 +2145,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
String tmp[] = markerTag.split("-");
String clusterId = tmp[0];
String markerId = tmp[1];
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onClusterEvent', args:['%s', '%s', new plugin.google.maps.LatLng(%f, %f)]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onClusterEvent', args:['%s', '%s', new plugin.google.maps.LatLng(%f, %f)]});}",
mapId, eventName, clusterId, markerId, latLng.latitude, latLng.longitude);
jsCallback(js);
}
@@ -2157,13 +2157,13 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
LatLng latLng = activeMarker.getPosition();
Point point = projection.toScreenLocation(latLng);
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: 'syncPosition', callback:'_onSyncInfoWndPosition', args:[{'x': %d, 'y': %d}]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: 'syncPosition', callback:'_onSyncInfoWndPosition', args:[{'x': %d, 'y': %d}]});}",
mapId, (int)(point.x / density), (int)(point.y / density));
jsCallback(js);
}
public void onOverlayEvent(String eventName, String overlayId, LatLng point) {
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onOverlayEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onOverlayEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]});}",
mapId, eventName, overlayId, point.latitude, point.longitude);
jsCallback(js);
}
@@ -2189,7 +2189,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
* @param eventName
*/
public void onMapEvent(final String eventName) {
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[]});}",
mapId, eventName);
jsCallback(js);
}
@@ -2200,7 +2200,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
* @param point
*/
public void onMapEvent(final String eventName, final LatLng point) {
- String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[new plugin.google.maps.LatLng(%f, %f)]})",
+ String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[new plugin.google.maps.LatLng(%f, %f)]});}",
mapId, eventName, point.latitude, point.longitude);
jsCallback(js);
}
@@ -2355,7 +2355,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public boolean onMyLocationButtonClick() {
- jsCallback(String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s',{evtName: 'my_location_button_click', callback:'_onMapEvent'})", mapId));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s',{evtName: 'my_location_button_click', callback:'_onMapEvent'});}", mapId));
return false;
}
@@ -2422,7 +2422,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsCallback(
String.format(
Locale.ENGLISH,
- "javascript:cordova.fireDocumentEvent('%s', {evtName:'%s', callback:'_onCameraEvent', args: [%s]})",
+ "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'%s', callback:'_onCameraEvent', args: [%s]});}",
mapId, eventName, jsonStr));
}
});
@@ -2487,7 +2487,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsonStr = result.toString();
}
}
- jsCallback(String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName:'indoor_building_focused', callback:'_onMapEvent', args: [%s]})", mapId, jsonStr));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'indoor_building_focused', callback:'_onMapEvent', args: [%s]});}", mapId, jsonStr));
}
@Override
@@ -2499,7 +2499,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsonStr = result.toString();
}
}
- jsCallback(String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s', {evtName:'indoor_level_activated', callback:'_onMapEvent', args: [%s]})", mapId, jsonStr));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'indoor_level_activated', callback:'_onMapEvent', args: [%s]});}", mapId, jsonStr));
}
private void jsCallback(final String js) {

MODIFY
src/android/plugin/google/maps/PluginTileProvider.java
src/android/plugin/google/maps/PluginTileProvider.java
@@ -136,7 +136,7 @@ public class PluginTileProvider implements TileProvider {
String originalUrlStr = null;
final String urlKey = String.format(Locale.US, "%s-%s-%d-%d-%d", mapId, pluginId, x, y, zoom);
synchronized (semaphore) {
- final String js = String.format(Locale.ENGLISH, "javascript:cordova.fireDocumentEvent('%s-%s-tileoverlay', {key: \"%s\", x: %d, y: %d, zoom: %d})",
+ final String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s-%s-tileoverlay', {key: \"%s\", x: %d, y: %d, zoom: %d});}",
mapId, pluginId, urlKey, x, y, zoom);
handler.post(new Runnable() {

update: prevent `cordova is undefined` error

This error occurs when you move to another page during the map is still working