Skip to content

Commit

Permalink
- Implementing new way for map.addCircle()
Browse files Browse the repository at this point in the history
- Bug fix: bindTo()
  • Loading branch information
wf9a5m75 committed Mar 31, 2018
1 parent 3a3e207 commit cb72246
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 70 deletions.
12 changes: 6 additions & 6 deletions src/android/plugin/google/maps/PluginCircle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/android/plugin/google/maps/PluginMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion www/BaseClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
Expand Down
48 changes: 4 additions & 44 deletions www/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
};
Expand Down
30 changes: 15 additions & 15 deletions www/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

//-------------
Expand All @@ -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() {
Expand All @@ -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;
};
Expand Down
40 changes: 37 additions & 3 deletions www/Overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -37,6 +47,7 @@ var Overlay = function(map, id, className, _exec) {
writable: false
});

className = className.toLowerCase();
Object.defineProperty(self, "_isReady", {
value: false,
writable: true
Expand All @@ -46,15 +57,14 @@ 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", {
value: className,
writable: false
});

className = className.toLowerCase();
Object.defineProperty(self, "getPluginName", {
writable: false,
value: function() {
Expand All @@ -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, {
Expand Down

0 comments on commit cb72246

Please sign in to comment.