Bug fix: Polyline.setPoints(), Polygon.setPoints() , Polygon.setHoles() do not work sometime
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta2-20170718-1602" 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-1900" 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/PluginPolygon.java
src/android/plugin/google/maps/PluginPolygon.java
@@ -309,6 +309,54 @@ public class PluginPolygon extends MyPlugin implements MyPluginInterface {
* @param callbackContext
* @throws JSONException
*/
+ public void setPoints(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+
+ String id = args.getString(0);
+ final JSONArray positionList = args.getJSONArray(1);
+
+
+ final Polygon polygon = this.getPolygon(id);
+
+ //------------------------
+ // Update the hole list
+ //------------------------
+ String propertyId = "polygon_path_" + polygon.getId();
+ final ArrayList<LatLng> path = (ArrayList<LatLng>)self.objects.get(propertyId);
+ path.clear();
+ JSONObject position;
+ for (int i = 0; i < positionList.length(); i++) {
+ position = positionList.getJSONObject(i);
+ path.add(new LatLng(position.getDouble("lat"), position.getDouble("lng")));
+ }
+ self.objects.put(propertyId, path);
+
+ //-----------------------------------
+ // Recalculate the polygon bounds
+ //-----------------------------------
+ propertyId = "polygon_bounds_" + polygon.getId();
+ self.objects.put(propertyId, PluginUtil.getBoundsFromPath(path));
+
+ cordova.getActivity().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ // Update the polygon
+ polygon.setPoints(path);
+ if (path.size() > 0) {
+ polygon.setVisible(true);
+ } else {
+ polygon.setVisible(false);
+ }
+ sendNoResult(callbackContext);
+ }
+ });
+ }
+
+ /**
+ * Insert a point
+ * @param args
+ * @param callbackContext
+ * @throws JSONException
+ */
public void insertPointAt(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String id = args.getString(0);
final int index = args.getInt(1);
@@ -390,6 +438,50 @@ public class PluginPolygon extends MyPlugin implements MyPluginInterface {
});
}
+ /**
+ * Set points
+ * @param args
+ * @param callbackContext
+ * @throws JSONException
+ */
+ public void setHoles(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+ String id = args.getString(0);
+ final JSONArray holeList = args.getJSONArray(1);
+ final Polygon polygon = this.getPolygon(id);
+
+ //------------------------
+ // Update the hole list
+ //------------------------
+ String propertyId = "polygon_holePaths_" + polygon.getId();
+ final ArrayList<ArrayList<LatLng>> holes = (ArrayList<ArrayList<LatLng>>) self.objects.get(propertyId);
+ for (int i = 0; i < holes.size(); i++) {
+ holes.get(i).clear();
+ }
+ holes.clear();
+
+ JSONObject position;
+ for (int i = 0; i < holeList.length(); i++) {
+ ArrayList<LatLng> hole = new ArrayList<LatLng>();
+ JSONArray holePositions = holeList.getJSONArray(i);
+ for (int j = 0; j < holePositions.length(); j++) {
+ position = holePositions.getJSONObject(j);
+ hole.add(new LatLng(position.getDouble("lat"), position.getDouble("lng")));
+ }
+ holes.add(hole);
+ }
+
+
+ self.objects.put(propertyId, holes);
+
+ cordova.getActivity().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ // Update the polygon
+ polygon.setHoles(holes);
+ sendNoResult(callbackContext);
+ }
+ });
+ }
/**
* Set points
* @param args

MODIFY
src/android/plugin/google/maps/PluginPolyline.java
src/android/plugin/google/maps/PluginPolyline.java
package plugin.google.maps;
import android.graphics.Color;
+import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
@@ -258,6 +259,36 @@ public class PluginPolyline extends MyPlugin implements MyPluginInterface {
}
});
}
+ public void setPoints(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
+
+ String id = args.getString(0);
+ final JSONArray positionList = args.getJSONArray(1);
+
+ final Polyline polyline = this.getPolyline(id);
+ // Recalculate the polygon bounds
+ final String propertyId = "polyline_bounds_" + polyline.getId();
+
+ cordova.getActivity().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+
+ try {
+ List<LatLng> path = polyline.getPoints();
+ path.clear();
+ JSONObject position;
+ for (int i = 0; i < positionList.length(); i++) {
+ position = positionList.getJSONObject(i);
+ path.add(new LatLng(position.getDouble("lat"), position.getDouble("lng")));
+ }
+ polyline.setPoints(path);
+ self.objects.put(propertyId, PluginUtil.getBoundsFromPath(path));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ sendNoResult(callbackContext);
+ }
+ });
+ }
public void removePointAt(final JSONArray args, CallbackContext callbackContext) throws JSONException {
String id = args.getString(0);
@@ -302,7 +333,7 @@ public class PluginPolyline extends MyPlugin implements MyPluginInterface {
@Override
public void run() {
List<LatLng> path = polyline.getPoints();
- if (path.size() > index) {
+ if (path.size() >= index) {
path.add(index, latLng);
polyline.setPoints(path);
self.objects.put(propertyId, PluginUtil.getBoundsFromPath(path));

MODIFY
src/ios/GoogleMaps/PluginPolygon.h
src/ios/GoogleMaps/PluginPolygon.h
- (void)setStrokeWidth:(CDVInvokedUrlCommand*)command;
- (void)removePointAt:(CDVInvokedUrlCommand*)command;
- (void)setPointAt:(CDVInvokedUrlCommand*)command;
+- (void)setPoints:(CDVInvokedUrlCommand*)command;
- (void)insertPointAt:(CDVInvokedUrlCommand*)command;
- (void)setPointOfHoleAt:(CDVInvokedUrlCommand*)command;
- (void)removePointOfHoleAt:(CDVInvokedUrlCommand*)command;
- (void)insertPointOfHoleAt:(CDVInvokedUrlCommand*)command;
+- (void)setHoles:(CDVInvokedUrlCommand*)command;
- (void)setZIndex:(CDVInvokedUrlCommand*)command;
- (void)setClickable:(CDVInvokedUrlCommand*)command;
- (void)setVisible:(CDVInvokedUrlCommand*)command;

MODIFY
src/ios/GoogleMaps/PluginPolygon.m
src/ios/GoogleMaps/PluginPolygon.m
}];
}
+-(void)setHoles:(CDVInvokedUrlCommand *)command
+{
+
+ [self.executeQueue addOperationWithBlock:^{
+
+ NSString *polygonKey = [command.arguments objectAtIndex:0];
+ NSArray *holeList = [command.arguments objectAtIndex:1];
+ GMSMutablePath *hole;
+
+ GMSPolygon *polygon = (GMSPolygon *)[self.objects objectForKey:polygonKey];
+ NSDictionary *latLng;
+
+
+ // Get properties
+ NSString *propertyId = [NSString stringWithFormat:@"polygon_property_%lu", (unsigned long)polygon.hash];
+ NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:
+ [self.objects objectForKey:propertyId]];
+
+ // Remove current holes
+ NSMutableArray *holePaths = (NSMutableArray *)[properties objectForKey:@"holePaths"];
+ for (int i = 0; i < holePaths.count; i++) {
+ hole = [holePaths objectAtIndex:i];
+ [hole removeAllCoordinates];
+ }
+ [holePaths removeAllObjects];
+
+ // update all holes
+ GMSMutablePath *path;
+ NSArray *holePositions;
+ for (int i = 0; i < holeList.count; i++) {
+ path = [GMSMutablePath path];
+ holePositions = [holeList objectAtIndex:i];
+ for (int j = 0; j < holePositions.count; j++) {
+ latLng = [holePositions objectAtIndex:j];
+ [path addCoordinate:CLLocationCoordinate2DMake([[latLng objectForKey:@"lat"] floatValue], [[latLng objectForKey:@"lng"] floatValue])];
+ }
+ [holePaths addObject:path];
+ }
+
+ // Save the property
+ [properties setObject:holePaths forKey:@"holePaths"];
+
+
+ // Apply to the polygon on UI thread.
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ polygon.holes = holePaths;
+
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ }];
+
+ }];
+}
+
-(void)insertPointOfHoleAt:(CDVInvokedUrlCommand *)command
{
}
+-(void)setPoints:(CDVInvokedUrlCommand *)command
+{
+
+ [self.executeQueue addOperationWithBlock:^{
+
+ NSString *polygonKey = [command.arguments objectAtIndex:0];
+ NSArray *positionList = [command.arguments objectAtIndex:1];
+ NSDictionary *latLng;
+ GMSPolygon *polygon = (GMSPolygon *)[self.objects objectForKey:polygonKey];
+
+ // Get properties
+ NSString *propertyId = [NSString stringWithFormat:@"polygon_property_%lu", (unsigned long)polygon.hash];
+ NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:
+ [self.objects objectForKey:propertyId]];
+
+ GMSMutablePath *mutablePath = (GMSMutablePath *)[properties objectForKey:@"mutablePath"];
+ [mutablePath removeAllCoordinates];
+ for (int i = 0; i < positionList.count; i++) {
+ latLng = [positionList objectAtIndex:i];
+ [mutablePath addCoordinate:CLLocationCoordinate2DMake([[latLng objectForKey:@"lat"] floatValue], [[latLng objectForKey:@"lng"] floatValue])];
+ }
+
+ // update the property
+ [properties setObject:mutablePath forKey:@"mutablePath"];
+ [properties setObject:[[GMSCoordinateBounds alloc] initWithPath:mutablePath] forKey:@"bounds"];
+ [self.objects setObject:properties forKey:propertyId];
+
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ [polygon setPath:mutablePath];
+
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ }];
+ }];
+
+}
+
+
-(void)insertPointAt:(CDVInvokedUrlCommand *)command
{

MODIFY
src/ios/GoogleMaps/PluginPolyline.h
src/ios/GoogleMaps/PluginPolyline.h
- (void)setStrokeColor:(CDVInvokedUrlCommand*)command;
- (void)setStrokeWidth:(CDVInvokedUrlCommand*)command;
+- (void)setPoints:(CDVInvokedUrlCommand*)command;
- (void)removePointAt:(CDVInvokedUrlCommand*)command;
- (void)setPointAt:(CDVInvokedUrlCommand*)command;
- (void)insertPointAt:(CDVInvokedUrlCommand*)command;

MODIFY
src/ios/GoogleMaps/PluginPolyline.m
src/ios/GoogleMaps/PluginPolyline.m
}];
}
+-(void)setPoints:(CDVInvokedUrlCommand *)command
+{
+
+ [self.executeQueue addOperationWithBlock:^{
+
+ NSString *polylineKey = [command.arguments objectAtIndex:0];
+ NSArray *positionList = [command.arguments objectAtIndex:1];
+ GMSPolyline *polyline = (GMSPolyline *)[self.objects objectForKey:polylineKey];
+
+ // Get properties
+ NSString *propertyId = [NSString stringWithFormat:@"polyline_property_%lu", (unsigned long)polyline.hash];
+ NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:
+ [self.objects objectForKey:propertyId]];
+
+ GMSMutablePath *mutablePath = (GMSMutablePath *)[properties objectForKey:@"mutablePath"];
+ [mutablePath removeAllCoordinates];
+
+ CLLocationCoordinate2D position;
+ NSDictionary *latLng;
+ for (int i = 0; i < positionList.count; i++) {
+ latLng = [positionList objectAtIndex:i];
+ position = CLLocationCoordinate2DMake([[latLng objectForKey:@"lat"] floatValue], [[latLng objectForKey:@"lng"] floatValue]);
+ }
+
+ // update the property
+ [properties setObject:mutablePath forKey:@"mutablePath"];
+ [properties setObject:[[GMSCoordinateBounds alloc] initWithPath:mutablePath] forKey:@"bounds"];
+ [self.objects setObject:properties forKey:propertyId];
+
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ [polyline setPath:mutablePath];
+
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+ }];
+ }];
+
+}
+
-(void)insertPointAt:(CDVInvokedUrlCommand *)command
{

MODIFY
www/Polygon.js
www/Polygon.js
@@ -140,20 +140,26 @@ Polygon.prototype.getId = function() {
};
Polygon.prototype.setPoints = function(points) {
- var mvcArray = this.points;
- mvcArray.empty();
+ var self = this;
+ var mvcArray = self.points;
+ mvcArray.empty(true);
- points.forEach(function(point) {
- mvcArray.push(common.getLatLng(point));
- });
- return this;
+ var i,
+ path = [];
+
+ for (i = 0; i < points.length; i++) {
+ mvcArray.push(common.getLatLng(points[i]), true);
+ }
+ exec(null, self.errorHandler, self.getPluginName(), 'setPoints', [self.id, mvcArray.getArray()]);
+ return self;
};
Polygon.prototype.getPoints = function() {
return this.points;
};
Polygon.prototype.setHoles = function(holes) {
+ var self = this;
var mvcArray = this.holes;
- mvcArray.empty();
+ mvcArray.empty(true);
holes = holes || [];
if (holes.length > 0 && !utils.isArray(holes[0])) {
@@ -162,9 +168,16 @@ Polygon.prototype.setHoles = function(holes) {
holes.forEach(function(hole) {
if (!utils.isArray(hole)) {
hole = [hole];
+ mvcArray.push(hole, true);
+ } else {
+ var newHole = [];
+ for (var i = 0; i < hole.length; i++) {
+ newHole.push(common.getLatLng(hole[i]));
+ }
+ mvcArray.push(newHole, true);
}
- mvcArray.push(hole);
});
+ exec(null, self.errorHandler, self.getPluginName(), 'setHoles', [self.id, mvcArray.getArray()]);
return this;
};
Polygon.prototype.getHoles = function() {

MODIFY
www/Polyline.js
www/Polyline.js
@@ -91,8 +91,9 @@ Polyline.prototype.getHashCode = function() {
};
Polyline.prototype.setPoints = function(points) {
- var mvcArray = this.points;
- mvcArray.empty();
+ var self = this;
+ var mvcArray = self.points;
+ mvcArray.empty(true);
var i,
path = [];
@@ -101,9 +102,10 @@ Polyline.prototype.setPoints = function(points) {
mvcArray.push({
"lat": points[i].lat,
"lng": points[i].lng
- });
+ }, true);
}
- return this;
+ exec(null, self.errorHandler, self.getPluginName(), 'setPoints', [self.id, mvcArray.getArray()]);
+ return self;
};
Polyline.prototype.getPoints = function() {
return this.points;

Bug fix: Polyline.setPoints(), Polygon.setPoints() , Polygon.setHoles() do not work sometime (#1547)