(update) stop DOM inspection when map is not displayed
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta2-20170625-1805" 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-20170705-1320" 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
@@ -259,6 +259,8 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
try {
if (action.equals("putHtmlElements")) {
CordovaGoogleMaps.this.putHtmlElements(args, callbackContext);
+ } else if ("clearHtmlElements".equals(action)) {
+ CordovaGoogleMaps.this.clearHtmlElements(args, callbackContext);
} else if ("getMyLocation".equals(action)) {
CordovaGoogleMaps.this.getMyLocation(args, callbackContext);
} else if ("getMap".equals(action)) {
@@ -304,6 +306,10 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
CordovaGoogleMaps.this.getMyLocation(_saveArgs, _saveCallbackContext);
}
+ public void clearHtmlElements(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+ mPluginLayout.clearHtmlElements();
+ callbackContext.success();
+ }
public void putHtmlElements(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
final JSONObject elements = args.getJSONObject(0);

MODIFY
src/android/plugin/google/maps/MyPluginLayout.java
src/android/plugin/google/maps/MyPluginLayout.java
@@ -226,6 +226,19 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
+ public void clearHtmlElements() {
+ Bundle bundle;
+ RectF rectF;
+ String[] keys = HTMLNodes.keySet().toArray(new String[HTMLNodes.size()]);
+ for (int i = 0; i < HTMLNodes.size(); i++) {
+ bundle = HTMLNodes.remove(keys[i]);
+ bundle = null;
+ rectF = HTMLNodeRectFs.remove(keys[i]);
+ rectF = null;
+ }
+ keys = null;
+ }
+
public void putHTMLElements(JSONObject elements) {
@@ -501,7 +514,7 @@ public class MyPluginLayout extends FrameLayout implements ViewTreeObserver.OnSc
// Is the clicked point is in the map rectangle?
//------------------------------------------------
drawRect = HTMLNodeRectFs.get(pluginMap.mapDivId);
- if (!drawRect.contains(clickPoint.x, clickPoint.y)) {
+ if (drawRect == null || !drawRect.contains(clickPoint.x, clickPoint.y)) {
continue;
}
isMapAction = true;

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

MODIFY
src/ios/GoogleMaps/CordovaGoogleMaps.m
src/ios/GoogleMaps/CordovaGoogleMaps.m
}
}
+- (void)clearHtmlElements:(CDVInvokedUrlCommand *)command {
+ [self.executeQueue addOperationWithBlock:^{
+ [self.pluginLayer clearHTMLElements];
+ }];
+}
- (void)putHtmlElements:(CDVInvokedUrlCommand *)command {
[self.executeQueue addOperationWithBlock:^{

MODIFY
src/ios/GoogleMaps/MyPluginLayer.h
src/ios/GoogleMaps/MyPluginLayer.h
//
// MyPluginLayer.h
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by masashi.
//
- (id)initWithWebView:(UIView *)webView;
- (void)resizeTask:(NSTimer *)timer;
+- (void)clearHTMLElements;
- (void)putHTMLElements:(NSDictionary *)elementsDic;
- (void)addMapView:(GoogleMapsViewController *)mapCtrl;
- (void)removeMapView:(GoogleMapsViewController *)mapCtrl;

MODIFY
src/ios/GoogleMaps/MyPluginLayer.m
src/ios/GoogleMaps/MyPluginLayer.m
//
// DummyView.m
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by masashi.
//
@@ -75,6 +75,28 @@ BOOL hasCordovaStatusBar = NO; // YES if the app has cordova-plugin-statusbar
[self.pluginScrollView.debugView setNeedsDisplay];
}
}
+- (void)clearHTMLElements {
+ @synchronized(self.pluginScrollView.debugView.HTMLNodes) {
+ NSMutableDictionary *domInfo;
+ NSString *domId;
+ NSArray *keys=[self.pluginScrollView.debugView.HTMLNodes allKeys];
+ NSArray *keys2;
+ int i, j;
+ for (i = 0; i < [keys count]; i++) {
+ domId = [keys objectAtIndex:i];
+ domInfo = [self.pluginScrollView.debugView.HTMLNodes objectForKey:domId];
+ if (domInfo) {
+ keys2 = [domInfo allKeys];
+ for (j = 0; j < [keys2 count]; j++) {
+ [domInfo removeObjectForKey:[keys2 objectAtIndex:j]];
+ }
+ domInfo = nil;
+ }
+ [self.pluginScrollView.debugView.HTMLNodes removeObjectForKey:domId];
+ }
+ }
+
+}
- (void)putHTMLElements:(NSDictionary *)elementsDic {

MODIFY
src/ios/GoogleMaps/MyPluginLayerDebugView.h
src/ios/GoogleMaps/MyPluginLayerDebugView.h
//
// MyPluginLayerDebugView.h
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by Katsumata Masashi.
//

MODIFY
src/ios/GoogleMaps/MyPluginLayerDebugView.m
src/ios/GoogleMaps/MyPluginLayerDebugView.m
//
// MyPluginLayerDebugView.m
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by Katsumata Masashi.
//

MODIFY
src/ios/GoogleMaps/MyPluginScrollView.h
src/ios/GoogleMaps/MyPluginScrollView.h
//
// MyPluginScrollView.h
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by masashi.
//

MODIFY
src/ios/GoogleMaps/MyPluginScrollView.m
src/ios/GoogleMaps/MyPluginScrollView.m
//
// MyPluginScrollView.m
-// DevApp
+// cordova-googlemaps-plugin v2
//
// Created by masashi.
//

MODIFY
www/Common.js
www/Common.js
@@ -233,7 +233,7 @@ function getAllChildren(root) {
list = allClickableElements.filter(function(node) {
var tagName = node.tagName.toLowerCase();
if (node !== root &&
- _shouldWatchByNative(node) &&
+ shouldWatchByNative(node) &&
ignoreTags.indexOf(tagName) == -1) {
var classNames = (node.className || "").split(" ");
@@ -254,7 +254,7 @@ function getAllChildren(root) {
for (i = 0; i < clickableElements.length; i++) {
node = clickableElements[i];
tagName = node.tagName.toLowerCase();
- if (_shouldWatchByNative(node) &&
+ if (shouldWatchByNative(node) &&
ignoreTags.indexOf(tagName) == -1) {
classNames = (node.className || "").split(" ");
hit = false;
@@ -274,8 +274,8 @@ function getAllChildren(root) {
return list;
}
-function _shouldWatchByNative(node) {
- if (node.nodeType !== Node.ELEMENT_NODE) {
+function shouldWatchByNative(node) {
+ if (node.nodeType !== Node.ELEMENT_NODE || !node.offsetParent) {
return;
}
var visibilityCSS = getStyle(node, 'visibility');
@@ -294,11 +294,17 @@ function _shouldWatchByNative(node) {
// Get z-index order
// http://stackoverflow.com/a/24136505
+var internalCache = {};
function getZIndex(dom) {
+ if (dom === document.body) {
+ internalCache = undefined;
+ internalCache = {};
+ }
var z = null;
if (!dom) {
return 0;
}
+
if (window.getComputedStyle) {
try {
z = parseInt(document.defaultView.getComputedStyle(dom, null).getPropertyValue('z-index'), 10);
@@ -313,7 +319,29 @@ function getZIndex(dom) {
if (isNaN(z)) {
z = 0;
}
- z += getZIndex(dom.parentNode);
+ var parentNode = dom.parentNode;
+ if (parentNode && parentNode.nodeType === Node.ELEMENT_NODE) {
+ var parentElemId = parentNode.getAttribute("__pluginDomId");
+ if (!parentElemId) {
+ parentElemId = "pgm" + Math.floor(Math.random() * Date.now());
+ dom.setAttribute("__pluginDomId", parentElemId);
+ }
+ if (parentElemId in internalCache) {
+ z += internalCache[parentElemId];
+ } else {
+ var parentZIndex = getZIndex(dom.parentNode);
+ internalCache[parentElemId] = parentZIndex;
+ z += parentZIndex;
+ }
+ }
+
+ var elemId = dom.getAttribute("__pluginDomId");
+ if (!elemId) {
+ elemId = "pgm" + Math.floor(Math.random() * Date.now());
+ dom.setAttribute("__pluginDomId", elemId);
+ }
+ internalCache[elemId] = z;
+
return z;
}
@@ -574,5 +602,6 @@ module.exports = {
createMvcArray: createMvcArray,
getStyle: getStyle,
convertToPositionArray: convertToPositionArray,
- getLatLng: getLatLng
+ getLatLng: getLatLng,
+ shouldWatchByNative: shouldWatchByNative
};

MODIFY
www/Map.js
www/Map.js
@@ -65,7 +65,7 @@ Map.prototype.getMap = function(mapId, div, options) {
args = [mapId];
if (!common.isDom(div)) {
- options = div;
+ self.set("visible", false);
options = options || {};
if (options.camera) {
if (options.camera.latLng) {
@@ -90,6 +90,7 @@ Map.prototype.getMap = function(mapId, div, options) {
} else {
var currentDiv = self.get("div");
+ self.set("visible", true);
options = options || {};
if (options.camera) {
if (options.camera.latLng) {
@@ -441,9 +442,13 @@ Map.prototype.getMyLocation = function(params, success_callback, error_callback)
Map.prototype.getFocusedBuilding = function(callback) {
exec(callback, this.errorHandler, this.id, 'getFocusedBuilding', []);
};
+Map.prototype.getVisible = function() {
+ return this.get("visible");
+};
Map.prototype.setVisible = function(isVisible) {
var self = this;
isVisible = common.parseBoolean(isVisible);
+ self.set("visible", isVisible);
exec(null, self.errorHandler, this.id, 'setVisible', [isVisible]);
return this;
};

MODIFY
www/googlemaps-cdv-plugin.js
www/googlemaps-cdv-plugin.js
@@ -90,9 +90,41 @@ var saltHash = Math.floor(Math.random() * Date.now());
function putHtmlElements() {
var mapIDs = Object.keys(MAPS);
if (isChecking || mapIDs.length === 0) {
+ cordova.exec(null, null, 'CordovaGoogleMaps', 'clearHtmlElements', []);
return;
}
isChecking = true;
+
+ //-------------------------------------------
+ // If there is no visible map, stop checking
+ //-------------------------------------------
+ var visibleMapDivList, i, mapId, map;
+ if (window.document.querySelectorAll) {
+ // Android 4.4 and above
+ visibleMapList = mapIDs.filter(function(mapId) {
+ var map = MAPS[mapId];
+ return (map && map.getVisible() && map.getDiv() && common.shouldWatchByNative(map.getDiv()));
+ });
+ } else {
+ // for older versions than Android 4.4
+ visibleMapList = [];
+ for (i = 0; i < mapIDs.length; i++) {
+ mapId = mapIDs[i];
+ map = MAPS[mapId];
+ if (map && map.getVisible() && map.getDiv() && common.shouldWatchByNative(map.getDiv())) {
+ visibleMapList.push(mapId);
+ }
+ }
+ }
+ if (visibleMapList.length === 0) {
+ isChecking = false;
+ cordova.exec(null, null, 'CordovaGoogleMaps', 'clearHtmlElements', []);
+ return;
+ }
+
+ //-------------------------------------------
+ // Should the plugin update the map positions?
+ //-------------------------------------------
//baseRect = common.getDivRect(baseDom);
var children = common.getAllChildren(document.body);
var bodyRect = common.getDivRect(document.body);
@@ -104,7 +136,7 @@ var saltHash = Math.floor(Math.random() * Date.now());
}
var domPositions = {};
- var size, elemId, i, child, parentNode;
+ var size, elemId, child, parentNode;
var shouldUpdate = false;
children.unshift(document.body);
@@ -115,10 +147,6 @@ var saltHash = Math.floor(Math.random() * Date.now());
for (i = 0; i < children.length; i++) {
child = children[i];
elemId = child.getAttribute("__pluginDomId");
- if (!elemId) {
- elemId = "pgm" + Math.floor(Math.random() * Date.now());
- child.setAttribute("__pluginDomId", elemId);
- }
//domPositions[elemId] = common.getDomInfo(child);
var depth = cacheDepth[elemId];
var zIndex = common.getZIndex(child);
@@ -287,7 +315,9 @@ module.exports = {
getMap: function(div) {
var navDecor = document.getElementsByClassName("nav-decor");
if (navDecor && navDecor.length > 0) {
- navDecor[0].style.backgroundColor = "rgba(0,0,0,0)";
+ for (var i = 0; i < navDecor.length; i++) {
+ navDecor[i].style.display = "none !important";
+ }
}
var mapId;
if (common.isDom(div)) {

(update) stop DOM inspection when map is not displayed (#1522)