(JS) Accept promise / subscribe especially for plugin
(Android) small fixes
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170807-1753" 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-beta3-20170808-1950" 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/PluginCircle.java
src/android/plugin/google/maps/PluginCircle.java
@@ -40,7 +40,7 @@ public class PluginCircle extends MyPlugin implements MyPluginInterface {
circleOptions.fillColor(color);
}
if (opts.has("strokeWidth")) {
- circleOptions.strokeWidth(opts.getInt("strokeWidth") * this.density);
+ circleOptions.strokeWidth((int)(opts.getDouble("strokeWidth") * density));
}
if (opts.has("visible")) {
circleOptions.visible(opts.getBoolean("visible"));
@@ -148,8 +148,8 @@ public class PluginCircle extends MyPlugin implements MyPluginInterface {
@SuppressWarnings("unused")
public void setStrokeWidth(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String id = args.getString(0);
- float width = (float) args.getDouble(1) * this.density;
- this.setFloat("setStrokeWidth", id, width, callbackContext);
+ int width = (int)(args.getDouble(1) * density);
+ this.setInt("setStrokeWidth", id, width, callbackContext);
}
/**

MODIFY
src/android/plugin/google/maps/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
@@ -95,7 +95,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
public final String TAG = mapId;
public String mapDivId;
public HashMap<String, PluginEntry> plugins = new HashMap<String, PluginEntry>();
- private final int DEFAULT_CAMERA_PADDING = 20;
+ private final float DEFAULT_CAMERA_PADDING = 20;
private Projection projection = null;
public Marker activeMarker = null;
private boolean isDragging = false;
@@ -115,14 +115,14 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
CameraUpdate cameraUpdate;
int durationMS;
LatLngBounds cameraBounds;
- int cameraPadding;
+ double cameraPadding;
}
private class AsyncSetOptionsResult {
int MAP_TYPE_ID;
CameraPosition cameraPosition;
LatLngBounds cameraBounds;
- int cameraPadding;
+ double cameraPadding;
String styles;
}
@@ -401,18 +401,18 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public void run() {
- int CAMERA_PADDING = DEFAULT_CAMERA_PADDING;
+ double CAMERA_PADDING = DEFAULT_CAMERA_PADDING;
try {
if (mParams.has("camera")) {
JSONObject camera = mParams.getJSONObject("camera");
if (camera.has("padding")) {
- CAMERA_PADDING = camera.getInt("padding");
+ CAMERA_PADDING = camera.getDouble("padding");
}
}
} catch (Exception e) {
e.printStackTrace();
}
- map.moveCamera(CameraUpdateFactory.newLatLngBounds(initCameraBounds, CAMERA_PADDING * (int) density));
+ map.moveCamera(CameraUpdateFactory.newLatLngBounds(initCameraBounds, (int) (CAMERA_PADDING * density)));
CameraPosition.Builder builder = CameraPosition.builder(map.getCameraPosition());
@@ -1027,7 +1027,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
}
if (camera.has("padding")) {
- results.cameraPadding = camera.getInt("padding");
+ results.cameraPadding = camera.getDouble("padding");
}
if (camera.has("target")) {
@@ -1084,7 +1084,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
e.printStackTrace();
}
if (results.cameraBounds != null) {
- fitBounds(results.cameraBounds, results.cameraPadding);
+ fitBounds(results.cameraBounds, (int)(results.cameraPadding * density));
}
}
@@ -1338,7 +1338,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
result.durationMS = cameraPos.getInt("duration");
}
if (cameraPos.has("padding")) {
- result.cameraPadding = cameraPos.getInt("padding");
+ result.cameraPadding = cameraPos.getDouble("padding");
}
if (!cameraPos.has("target")) {
@@ -1426,7 +1426,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
}
}
- CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(finalCameraPosition.cameraBounds, finalCameraPosition.cameraPadding * (int)density);
+ CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(finalCameraPosition.cameraBounds, (int)(finalCameraPosition.cameraPadding * density));
try {
map.moveCamera(cameraUpdate);
} catch (Exception e) {
@@ -1525,8 +1525,8 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
public void panBy(JSONArray args, CallbackContext callbackContext) throws JSONException {
int x = args.getInt(0);
int y = args.getInt(1);
- float xPixel = -x * this.density;
- float yPixel = -y * this.density;
+ float xPixel = -x * density;
+ float yPixel = -y * density;
final CameraUpdate cameraUpdate = CameraUpdateFactory.scrollBy(xPixel, yPixel);
this.activity.runOnUiThread(new Runnable() {

MODIFY
src/android/plugin/google/maps/PluginMarker.java
src/android/plugin/google/maps/PluginMarker.java
@@ -1087,8 +1087,8 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
private void _setIconAnchor(final Marker marker, double anchorX, double anchorY, final int imageWidth, final int imageHeight) {
// The `anchor` of the `icon` property
- anchorX = anchorX * this.density;
- anchorY = anchorY * this.density;
+ anchorX = anchorX * density;
+ anchorY = anchorY * density;
final double fAnchorX = anchorX;
final double fAnchorY = anchorY;
cordova.getActivity().runOnUiThread(new Runnable() {
@@ -1100,8 +1100,8 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
}
private void _setInfoWindowAnchor(final Marker marker, double anchorX, double anchorY, final int imageWidth, final int imageHeight) {
// The `anchor` of the `icon` property
- anchorX = anchorX * this.density;
- anchorY = anchorY * this.density;
+ anchorX = anchorX * density;
+ anchorY = anchorY * density;
final double fAnchorX = anchorX;
final double fAnchorY = anchorY;
cordova.getActivity().runOnUiThread(new Runnable() {

MODIFY
src/android/plugin/google/maps/PluginPolygon.java
src/android/plugin/google/maps/PluginPolygon.java
@@ -80,7 +80,7 @@ public class PluginPolygon extends MyPlugin implements MyPluginInterface {
polygonOptions.fillColor(color);
}
if (opts.has("strokeWidth")) {
- polygonOptions.strokeWidth(opts.getInt("strokeWidth") * this.density);
+ polygonOptions.strokeWidth((int)(opts.getDouble("strokeWidth") * density));
}
if (opts.has("visible")) {
polygonOptions.visible(opts.getBoolean("visible"));
@@ -188,8 +188,8 @@ public class PluginPolygon extends MyPlugin implements MyPluginInterface {
@SuppressWarnings("unused")
public void setStrokeWidth(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String id = args.getString(0);
- float width = (float) args.getDouble(1) * this.density;
- this.setFloat("setStrokeWidth", id, width, callbackContext);
+ int width = (int)(args.getDouble(1) * density);
+ this.setInt("setStrokeWidth", id, width, callbackContext);
}
/**

MODIFY
src/android/plugin/google/maps/PluginPolyline.java
src/android/plugin/google/maps/PluginPolyline.java
@@ -46,7 +46,7 @@ public class PluginPolyline extends MyPlugin implements MyPluginInterface {
polylineOptions.color(color);
}
if (opts.has("width")) {
- polylineOptions.width(opts.getInt("width") * density);
+ polylineOptions.width((int)(opts.getDouble("width") * density));
}
if (opts.has("visible")) {
polylineOptions.visible(opts.getBoolean("visible"));
@@ -216,8 +216,8 @@ public class PluginPolyline extends MyPlugin implements MyPluginInterface {
*/
public void setStrokeWidth(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String id = args.getString(0);
- float width = (float) args.getDouble(1) * this.density;
- this.setFloat("setWidth", id, width, callbackContext);
+ int width = (int)(args.getDouble(1) * density);
+ this.setInt("setWidth", id, width, callbackContext);
}
/**

MODIFY
www/BaseClass.js
www/BaseClass.js
var VARS_FIELD = typeof Symbol === 'undefined' ? '__vars' + Date.now() : Symbol.for('vars');
var SUBSCRIPTIONS_FIELD = typeof Symbol === 'undefined' ? '__subs' + Date.now() : Symbol.for('subscriptions');
+function FakeObservable() {
+ this.next = undefined;
+ this.error = undefined;
+ this.complete = undefined;
+ this.done = false;
+ this.values = undefined;
+}
+FakeObservable.prototype.subscribe = function(next, error, complete) {
+ this.next = next;
+ this.error = error;
+ this.complete = complete;
+ if (this.done && typeof next === "function") {
+ next(this.values);
+ }
+};
+
+FakeObservable.prototype.keepValues = function(values) {
+ this.values = values;
+ this.done = true;
+};
+
function BaseClass() {
this[VARS_FIELD] = {};
this[SUBSCRIPTIONS_FIELD] = {};
@@ -59,12 +80,29 @@ BaseClass.prototype = {
},
on: function(eventName, listener) {
+ var topic;
+ if (listener && eventName.indexOf('promise-') === -1) {
this[SUBSCRIPTIONS_FIELD][eventName] = this[SUBSCRIPTIONS_FIELD][eventName] || [];
- var topic = this[SUBSCRIPTIONS_FIELD][eventName];
- var index = topic.push(listener);
- var self = this;
-
+ topic = this[SUBSCRIPTIONS_FIELD][eventName];
+ topic.push(listener);
return this;
+ }
+
+ eventName = eventName.replace("promise-", "");
+ this[SUBSCRIPTIONS_FIELD][eventName] = this[SUBSCRIPTIONS_FIELD][eventName] || [];
+ topic = this[SUBSCRIPTIONS_FIELD][eventName];
+
+ var self = this;
+ var fakeObservable = new FakeObservable();
+ topic.push(function() {
+ var args = Array.prototype.slice.call(arguments, 0);
+ if (typeof fakeObservable.next === 'function') {
+ fakeObservable.next.apply(self, args);
+ } else {
+ fakeObservable.keepValues(args);
+ }
+ });
+ return fakeObservable;
},
off: function(eventName, listener) {
@@ -84,10 +122,28 @@ BaseClass.prototype = {
},
one: function(eventName, listener) {
- var unlisten = this.on(eventName, function() {
- unlisten();
- listener.apply(this, arguments);
+
+ var self = this;
+
+ if (!listener || eventName.indexOf("promise-") === 0) {
+ eventName = eventName.replace("promise-", "");
+ return new window.Promise(function(resolve, reject) {
+ var callback = function() {
+ self.off(eventName, arguments.callee);
+
+ var args = Array.prototype.slice.call(arguments, 0);
+ resolve.apply(self, [args]);
+ };
+ self.on(eventName, callback);
+
});
+ }
+
+ var callback = function() {
+ self.off(eventName, arguments.callee);
+ listener.apply(self, arguments);
+ };
+ this.on(eventName, callback);
return this;
},
@@ -99,7 +155,11 @@ BaseClass.prototype = {
errorHandler: function(error) {
if (error) {
+ if (typeof console.error === "function") {
+ console.error(error);
+ } else {
console.log(error);
+ }
this.trigger('error', error instanceof Error ? error : createError(error));
}

(JS) Accept promise / subscribe especially for @ionic-native/google-maps plugin
(Android) small fixes