From cb722464a705782c24dde2b3f4f4689dd07c7aac Mon Sep 17 00:00:00 2001 From: wf9a5m75 Date: Fri, 30 Mar 2018 20:05:44 -0700 Subject: [PATCH] - Implementing new way for map.addCircle() - Bug fix: bindTo() --- .../plugin/google/maps/PluginCircle.java | 12 ++--- .../plugin/google/maps/PluginMarker.java | 2 +- www/BaseClass.js | 3 +- www/Circle.js | 48 ++----------------- www/Map.js | 30 ++++++------ www/Overlay.js | 40 ++++++++++++++-- 6 files changed, 65 insertions(+), 70 deletions(-) diff --git a/src/android/plugin/google/maps/PluginCircle.java b/src/android/plugin/google/maps/PluginCircle.java index c4b554a64..f3b9758a7 100644 --- a/src/android/plugin/google/maps/PluginCircle.java +++ b/src/android/plugin/google/maps/PluginCircle.java @@ -24,6 +24,7 @@ public void create(final JSONArray args, final CallbackContext callbackContext) final JSONObject properties = new JSONObject(); JSONObject opts = args.getJSONObject(1); + final String hashCode = args.getString(2); if (opts.has("center")) { JSONObject center = opts.getJSONObject("center"); circleOptions.center(new LatLng(center.getDouble("lat"), center.getDouble("lng"))); @@ -63,19 +64,18 @@ public void create(final JSONArray args, final CallbackContext callbackContext) @Override public void run() { Circle circle = map.addCircle(circleOptions); - String id = circle.getId(); - pluginMap.objects.put("circle_" + id, circle); + pluginMap.objects.put("circle_" + hashCode, circle); - pluginMap.objects.put("circle_property_" + id, properties); + pluginMap.objects.put("circle_property_" + hashCode, properties); // Recalculate the circle bounds LatLngBounds bounds = PluginUtil.getBoundsFromCircle(circleOptions.getCenter(), circleOptions.getRadius()); - pluginMap.objects.put("circle_bounds_" + id, bounds); + pluginMap.objects.put("circle_bounds_" + hashCode, bounds); JSONObject result = new JSONObject(); try { - result.put("hashCode", circle.hashCode()); - result.put("id", "circle_" + id); + result.put("hashCode", hashCode); + result.put("id", "circle_" + hashCode); callbackContext.success(result); } catch (JSONException e) { e.printStackTrace(); diff --git a/src/android/plugin/google/maps/PluginMarker.java b/src/android/plugin/google/maps/PluginMarker.java index 9cfca2882..bbb03ad77 100644 --- a/src/android/plugin/google/maps/PluginMarker.java +++ b/src/android/plugin/google/maps/PluginMarker.java @@ -197,7 +197,7 @@ public void create(final JSONArray args, final CallbackContext callbackContext) // Create an instance of Marker class JSONObject opts = args.getJSONObject(1); - final String markerId = args.getString(2); + final String markerId = "marker_" + args.getString(2); final JSONObject result = new JSONObject(); result.put("id", markerId); diff --git a/www/BaseClass.js b/www/BaseClass.js index b38203d45..047989ff8 100644 --- a/www/BaseClass.js +++ b/www/BaseClass.js @@ -42,9 +42,10 @@ BaseClass.prototype = { // If `noNotify` is true, prevent `(targetKey)_changed` event occurrs, // when bind the value for the first time only. // (Same behaviour as Google Maps JavaScript v3) - target.set(targetKey, value, noNotify); + target.set(targetKey, target.get(targetKey), noNotify); this.on(key + '_changed', function(oldValue, value) { +console.log(key, value); target.set(targetKey, value); }); }, diff --git a/www/Circle.js b/www/Circle.js index d0d95c78f..06f504aa9 100644 --- a/www/Circle.js +++ b/www/Circle.js @@ -2,43 +2,17 @@ var argscheck = require('cordova/argscheck'), utils = require('cordova/utils'), common = require('./Common'), LatLngBounds = require('./LatLngBounds'), - BaseClass = require('./BaseClass'); + Overlay = require('./Overlay'); /***************************************************************************** * Circle Class *****************************************************************************/ var exec; -var Circle = function(map, circleId, circleOptions, _exec) { +var Circle = function(map, circleOptions, _exec) { exec = _exec; - BaseClass.apply(this); + Overlay.call(this, map, circleOptions, _exec); var self = this; - Object.defineProperty(self, "_isReady", { - value: true, - writable: false - }); - Object.defineProperty(self, "map", { - value: map, - writable: false - }); - Object.defineProperty(self, "id", { - value: circleId, - writable: false - }); - Object.defineProperty(self, "type", { - value: "Circle", - writable: false - }); - - //----------------------------------------------- - // Sets the initialize option to each property - //----------------------------------------------- - var ignores = ["map", "id", "hashCode", "type"]; - for (var key in circleOptions) { - if (ignores.indexOf(key) === -1) { - self.set(key, circleOptions[key]); - } - } //----------------------------------------------- // Sets event listeners @@ -78,22 +52,8 @@ var Circle = function(map, circleId, circleOptions, _exec) { }; -utils.extend(Circle, BaseClass); - -Circle.prototype.getPluginName = function() { - return this.map.getId() + "-circle"; -}; - -Circle.prototype.getHashCode = function() { - return this.hashCode; -}; +utils.extend(Circle, Overlay); -Circle.prototype.getMap = function() { - return this.map; -}; -Circle.prototype.getId = function() { - return this.id; -}; Circle.prototype.getCenter = function() { return this.get('center'); }; diff --git a/www/Map.js b/www/Map.js index f07abda33..71bbaa922 100644 --- a/www/Map.js +++ b/www/Map.js @@ -1129,19 +1129,18 @@ Map.prototype.addCircle = function(circleOptions, callback) { circleOptions.zIndex = circleOptions.zIndex || 0; circleOptions.radius = "radius" in circleOptions ? circleOptions.radius : 1; - exec.call(this, function(result) { - var circle = new Circle(self, result.id, circleOptions, exec); - self.OVERLAYS[result.id] = circle; + var circle = new Circle(self, circleOptions, "Circle", exec); + var circleId = circle.getId(); + self.OVERLAYS[circleId] = circle; + circle.one(circleId + "_remove", function() { + circle.off(); + delete self.OVERLAYS[circleId]; + circle = undefined; + }); - circle.one(result.id + "_remove", function() { - circle.off(); - delete self.OVERLAYS[result.id]; - circle = undefined; - }); - if (typeof callback === "function") { - callback.call(self, circle); - } - }, self.errorHandler, self.id, 'loadPlugin', ['Circle', circleOptions]); + exec.call(this, callback, self.errorHandler, self.id, 'loadPlugin', ['Circle', circleOptions, circle.hashCode]); + + return circle; }; //------------- @@ -1163,8 +1162,9 @@ Map.prototype.addMarker = function(markerOptions, callback) { }; } - var markerId = "marker_" + (Math.floor(Date.now() * Math.random())); - var marker = new Marker(self, markerId, markerOptions, "Marker", exec); + var marker = new Marker(self, markerOptions, "Marker", exec); + var markerId = marker.getId(); + self.MARKERS[markerId] = marker; self.OVERLAYS[markerId] = marker; marker.one(markerId + "_remove", function() { @@ -1190,7 +1190,7 @@ Map.prototype.addMarker = function(markerOptions, callback) { if (typeof callback === "function") { callback.call(self, marker); } - }, self.errorHandler, self.id, 'loadPlugin', ['Marker', markerOptions, markerId]); + }, self.errorHandler, self.id, 'loadPlugin', ['Marker', markerOptions, marker.hashCode]); return marker; }; diff --git a/www/Overlay.js b/www/Overlay.js index d6fba572c..5661a1b1a 100644 --- a/www/Overlay.js +++ b/www/Overlay.js @@ -6,11 +6,21 @@ var BaseClass = require('./BaseClass'), /***************************************************************************** * Overlay Class *****************************************************************************/ -var Overlay = function(map, id, className, _exec) { +var Overlay = function(map, options, className, _exec) { BaseClass.apply(this); var self = this; + //----------------------------------------------- + // Sets the initialize option to each property + //----------------------------------------------- + var ignores = ["map", "id", "hashCode", "type"]; + for (var key in options) { + if (ignores.indexOf(key) === -1) { + self.set(key, options[key]); + } + } + //------------------------------------------------------------------------------- // If app code wants to execute some method before `_isReady = true`, // just stack in to the internal queue. @@ -37,6 +47,7 @@ var Overlay = function(map, id, className, _exec) { writable: false }); + className = className.toLowerCase(); Object.defineProperty(self, "_isReady", { value: false, writable: true @@ -46,7 +57,7 @@ var Overlay = function(map, id, className, _exec) { writable: false }); Object.defineProperty(self, "id", { - value: id, + value: className + "_" + this.hashCode, writable: false }); Object.defineProperty(self, "type", { @@ -54,7 +65,6 @@ var Overlay = function(map, id, className, _exec) { writable: false }); - className = className.toLowerCase(); Object.defineProperty(self, "getPluginName", { writable: false, value: function() { @@ -65,6 +75,30 @@ var Overlay = function(map, id, className, _exec) { utils.extend(Overlay, BaseClass); +Overlay.prototype._privateInitialize = function(options) { + var self = this; + //----------------------------------------------- + // Sets the initialize option to each property + //----------------------------------------------- + if (options) { + var ignores = ["map", "id", "hashCode", "type"]; + for (var key in options) { + if (ignores.indexOf(key) === -1) { + self.set(key, options[key], true); + } + } + } + + //----------------------------------------------- + // Trigger internal command queue + //----------------------------------------------- + Object.defineProperty(self, "_isReady", { + value: true, + writable: false + }); + self.exec("nop"); +}; + Overlay.prototype.exec = function() { this._cmdQueue.push.call(this._cmdQueue, {