update: suspend the view positioning when the map div(s) is/are not displayed
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta2-20170717-1045" 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-beta2-20170718-1155" 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/CordovaGoogleMaps.java
src/android/plugin/google/maps/CordovaGoogleMaps.java
@@ -261,6 +261,10 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
CordovaGoogleMaps.this.putHtmlElements(args, callbackContext);
} else if ("clearHtmlElements".equals(action)) {
CordovaGoogleMaps.this.clearHtmlElements(args, callbackContext);
+ } else if ("pause".equals(action)) {
+ CordovaGoogleMaps.this.pause(args, callbackContext);
+ } else if ("resume".equals(action)) {
+ CordovaGoogleMaps.this.resume(args, callbackContext);
} else if ("getMyLocation".equals(action)) {
CordovaGoogleMaps.this.getMyLocation(args, callbackContext);
} else if ("getMap".equals(action)) {
@@ -306,6 +310,14 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
CordovaGoogleMaps.this.getMyLocation(_saveArgs, _saveCallbackContext);
}
+ public void pause(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+ mPluginLayout.isSuspended = true;
+ callbackContext.success();
+ }
+ public void resume(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+ mPluginLayout.isSuspended = false;
+ callbackContext.success();
+ }
public void clearHtmlElements(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
mPluginLayout.clearHtmlElements();
callbackContext.success();

MODIFY
src/android/plugin/google/maps/MyPluginLayout.java
src/android/plugin/google/maps/MyPluginLayout.java
@@ -34,6 +34,7 @@ import java.util.TimerTask;
@SuppressWarnings("deprecation")
public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnScrollChangedListener, ViewTreeObserver.OnGlobalLayoutListener {
+ private static final String TAG = "MyPluginLayout";
private CordovaWebView webView;
private View browserView;
private ViewGroup root;
@@ -51,6 +52,7 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
private Paint debugPaint = new Paint();
public boolean stopFlag = false;
public boolean needUpdatePosition = false;
+ public boolean isSuspended = false;
private float zoomScale;
public Timer redrawTimer;
@@ -71,6 +73,9 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
private class ResizeTask extends TimerTask {
@Override
public void run() {
+ if (isSuspended) {
+ return;
+ }
//final PluginMap pluginMap = pluginMaps.get(mapId);
//if (pluginMap.mapDivId == null) {
// return;
@@ -467,7 +472,8 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
- if (pluginMaps == null || pluginMaps.size() == 0) {
+ if (isSuspended || pluginMaps == null || pluginMaps.size() == 0) {
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('plugin_touch', {});");
return false;
}
MyPluginLayout.this.stopFlag = true;
@@ -574,7 +580,7 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
@Override
protected void onDraw(Canvas canvas) {
- if (HTMLNodes.isEmpty() || !isDebug) {
+ if (isSuspended || HTMLNodes.isEmpty() || !isDebug) {
return;
}

MODIFY
src/ios/GoogleMaps/CordovaGoogleMaps.h
src/ios/GoogleMaps/CordovaGoogleMaps.h
- (void)clearHtmlElements:(CDVInvokedUrlCommand *)command;
- (void)putHtmlElements:(CDVInvokedUrlCommand *)command;
- (void)removeMap:(CDVInvokedUrlCommand *)command;
+- (void)pause:(CDVInvokedUrlCommand *)command;
+- (void)resume:(CDVInvokedUrlCommand *)command;
@end

MODIFY
src/ios/GoogleMaps/CordovaGoogleMaps.m
src/ios/GoogleMaps/CordovaGoogleMaps.m
}];
}
+- (void)resume:(CDVInvokedUrlCommand *)command {
+ self.pluginLayer.isSuspended = NO;
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+
+}
+- (void)pause:(CDVInvokedUrlCommand *)command {
+ self.pluginLayer.isSuspended = YES;
+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+}
+
- (void)putHtmlElements:(CDVInvokedUrlCommand *)command {
[self.executeQueue addOperationWithBlock:^{

MODIFY
src/ios/GoogleMaps/MyPluginLayer.h
src/ios/GoogleMaps/MyPluginLayer.h
@property (nonatomic) UIView *webView;
@property (nonatomic) MyPluginScrollView *pluginScrollView;
@property (nonatomic) NSTimer *redrawTimer;
+@property (nonatomic) BOOL *isSuspended;
- (id)initWithWebView:(UIView *)webView;
- (void)resizeTask:(NSTimer *)timer;

MODIFY
src/ios/GoogleMaps/MyPluginLayer.m
src/ios/GoogleMaps/MyPluginLayer.m
@@ -19,6 +19,7 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
self = [super initWithFrame:[webView frame]];
self.webView = webView;
+ self.isSuspended = NO;
self.opaque = NO;
[self.webView removeFromSuperview];
// prevent webView from bouncing
@@ -178,7 +179,10 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
}
- (void)resizeTask:(NSTimer *)timer {
-
+ if (self.isSuspended) {
+ // Assumes all touches for the browser
+ return;
+ }
if (stopFlag) {
return;
}
@@ -283,8 +287,19 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
});
}
-
+- (void)execJS: (NSString *)jsString {
+ if ([self.webView respondsToSelector:@selector(stringByEvaluatingJavaScriptFromString:)]) {
+ [self.webView performSelector:@selector(stringByEvaluatingJavaScriptFromString:) withObject:jsString];
+ } else if ([self.webView respondsToSelector:@selector(evaluateJavaScript:completionHandler:)]) {
+ [self.webView performSelector:@selector(evaluateJavaScript:completionHandler:) withObject:jsString withObject:nil];
+ }
+}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
+ if (self.isSuspended) {
+ // Assumes all touches for the browser
+ [self execJS:@"javascript:cordova.fireDocumentEvent('plugin_touch', {});"];
+ return [self.webView hitTest:point withEvent:event];
+ }
float offsetX = self.webView.scrollView.contentOffset.x;
float offsetY = self.webView.scrollView.contentOffset.y;
@@ -407,7 +422,7 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
//NSLog(@"--> (hit test) point = %f, %f / hit = %@", clickPointAsHtml.x, clickPointAsHtml.y, hitView.class);
if (isMapAction == NO) {
- [mapCtrl execJS:@"javascript:cordova.fireDocumentEvent('plugin_touch', {});"];
+ [self execJS:@"javascript:cordova.fireDocumentEvent('plugin_touch', {});"];
}
return hitView;
@@ -416,7 +431,7 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
UIView *hitView =[self.webView hitTest:point withEvent:event];
//NSLog(@"--> (hit test) hit = %@", hitView.class);
- [mapCtrl execJS:@"javascript:cordova.fireDocumentEvent('plugin_touch', {});"];
+ [self execJS:@"javascript:cordova.fireDocumentEvent('plugin_touch', {});"];
return hitView;
}

MODIFY
www/googlemaps-cdv-plugin.js
www/googlemaps-cdv-plugin.js
@@ -99,6 +99,7 @@ document.head.appendChild(navDecorBlocker);
var prevDomPositions = {};
var prevChildrenCnt = 0;
var idlingCnt = -1;
+ var isSuspended = false;
var isChecking = false;
var cacheDepth = {};
@@ -141,12 +142,20 @@ document.head.appendChild(navDecorBlocker);
}
if (visibleMapList.length === 0) {
idlingCnt++;
- if (idlingCnt < 10) {
- setTimeout(putHtmlElements, idlingCnt < 5 ? 50 : 200);
+ if (!isSuspended) {
+ cordova.exec(null, null, 'CordovaGoogleMaps', 'pause', []);
+ isSuspended = true;
+ }
+ if (idlingCnt < 5) {
+ setTimeout(putHtmlElements, 50);
}
isChecking = false;
return;
}
+ if (isSuspended) {
+ isSuspended = false;
+ cordova.exec(null, null, 'CordovaGoogleMaps', 'resume', []);
+ }
//-------------------------------------------
// Should the plugin update the map positions?

update: suspend the view positioning when the map div(s) is/are not displayed (#1522)