Wait the MAP_READY event until getVisibleRegion() is worked correctly.
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.2.8-beta-20180314-1540" 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.2.8-beta-20180314-2231" 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/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
@@ -2297,8 +2297,8 @@ 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: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);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onMarkerEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]});}",
+ mapId, mapId, eventName, markerId, latLng.latitude, latLng.longitude);
jsCallback(js);
}
public void onClusterEvent(String eventName, Marker marker) {
@@ -2311,8 +2311,8 @@ 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: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);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onClusterEvent', args:['%s', '%s', new plugin.google.maps.LatLng(%f, %f)]});}",
+ mapId, mapId, eventName, clusterId, markerId, latLng.latitude, latLng.longitude);
jsCallback(js);
}
public void syncInfoWndPosition() {
@@ -2323,14 +2323,14 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
LatLng latLng = activeMarker.getPosition();
Point point = projection.toScreenLocation(latLng);
- 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));
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: 'syncPosition', callback:'_onSyncInfoWndPosition', args:[{'x': %d, 'y': %d}]});}",
+ mapId, 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: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);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onOverlayEvent', args:['%s', new plugin.google.maps.LatLng(%f, %f)]});}",
+ mapId, mapId, eventName, overlayId, point.latitude, point.longitude);
jsCallback(js);
}
public void onPolylineClick(Polyline polyline, LatLng point) {
@@ -2355,8 +2355,8 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
* @param eventName
*/
public void onMapEvent(final String eventName) {
- String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[]});}",
- mapId, eventName);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onMapEvent', args:[]});}",
+ mapId, mapId, eventName);
jsCallback(js);
}
@@ -2366,8 +2366,8 @@ 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:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:[new plugin.google.maps.LatLng(%f, %f)]});}",
- mapId, eventName, point.latitude, point.longitude);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onMapEvent', args:[new plugin.google.maps.LatLng(%f, %f)]});}",
+ mapId, mapId, eventName, point.latitude, point.longitude);
jsCallback(js);
}
@@ -2521,7 +2521,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public boolean onMyLocationButtonClick() {
- jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: 'my_location_button_click', callback:'_onMapEvent'});}", mapId));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: 'my_location_button_click', callback:'_onMapEvent'});}",mapId, mapId));
return false;
}
@@ -2530,7 +2530,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
PluginLocationService.setLastLocation(location);
try {
JSONObject result = PluginUtil.location2Json(location);
- jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: 'my_location_click', callback:'_onMapEvent', args: [%s]});}", mapId, result.toString(0)));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: 'my_location_click', callback:'_onMapEvent', args: [%s]});}", mapId, mapId, result.toString(0)));
} catch (JSONException e) {
e.printStackTrace();
}
@@ -2598,8 +2598,8 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsCallback(
String.format(
Locale.ENGLISH,
- "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'%s', callback:'_onCameraEvent', args: [%s]});}",
- mapId, eventName, jsonStr));
+ "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName:'%s', callback:'_onCameraEvent', args: [%s]});}",
+ mapId, mapId, eventName, jsonStr));
}
});
@@ -2663,7 +2663,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsonStr = result.toString();
}
}
- jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'indoor_building_focused', callback:'_onMapEvent', args: [%s]});}", mapId, jsonStr));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName:'indoor_building_focused', callback:'_onMapEvent', args: [%s]});}", mapId, mapId, jsonStr));
}
@Override
@@ -2675,13 +2675,13 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
jsonStr = result.toString();
}
}
- jsCallback(String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName:'indoor_level_activated', callback:'_onMapEvent', args: [%s]});}", mapId, jsonStr));
+ jsCallback(String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName:'indoor_level_activated', callback:'_onMapEvent', args: [%s]});}", mapId, mapId, jsonStr));
}
@Override
public void onPoiClick(PointOfInterest pointOfInterest) {
- String js = String.format(Locale.ENGLISH, "javascript:if(window.cordova){cordova.fireDocumentEvent('%s', {evtName: '%s', callback:'_onMapEvent', args:['%s', '%s', new plugin.google.maps.LatLng(%f, %f)]});}",
- mapId, "poi_click", pointOfInterest.placeId, pointOfInterest.name, pointOfInterest.latLng.latitude, pointOfInterest.latLng.longitude);
+ String js = String.format(Locale.ENGLISH, "javascript:if('%s' in plugin.google.maps){plugin.google.maps['%s']({evtName: '%s', callback:'_onMapEvent', args:['%s', '%s', new plugin.google.maps.LatLng(%f, %f)]});}",
+ mapId, mapId, "poi_click", pointOfInterest.placeId, pointOfInterest.name, pointOfInterest.latLng.latitude, pointOfInterest.latLng.longitude);
jsCallback(js);
}

MODIFY
www/Map.js
www/Map.js
@@ -226,14 +226,25 @@ Map.prototype.getMap = function(mapId, div, options) {
//------------------------------------------------------------------------
// In order to work map.getVisibleRegion() correctly, wait a little.
//------------------------------------------------------------------------
+ var waitCnt = 0;
+ var waitCameraSync = function() {
+ if (!self.getVisibleRegion() && (waitCnt++ < 10)) {
setTimeout(function() {
+ common.nextTick(waitCameraSync);
+ }, 100);
+ return;
+ }
+
Object.defineProperty(self, "_isReady", {
value: true,
writable: false
});
self.refreshLayout();
self.trigger(event.MAP_READY, self);
- }, 250);
+ };
+ setTimeout(function() {
+ common.nextTick(waitCameraSync);
+ }, 100);
}, self.errorHandler, 'CordovaGoogleMaps', 'getMap', args, {
sync: true
});
@@ -793,7 +804,7 @@ Map.prototype.setDiv = function(div) {
Map.prototype.getVisibleRegion = function(callback) {
var self = this;
var cameraPosition = self.get("camera");
- if (!cameraPosition) {
+ if (!cameraPosition || !cameraPosition.southwest || !cameraPosition.northeast) {
return null;
}

Wait the MAP_READY event until getVisibleRegion() is worked correctly. (#2126)