Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Adds support for polygon overlays to the Google Maps plugin (#1551)
Browse files Browse the repository at this point in the history
This is relatively trivial, requiring only some additional logic
to disambiguate click events between the various possible overlays.

Also adds a page to the example app demonstrating polygons,
which I tested on iOS and Android.
  • Loading branch information
matthewlloyd authored and iskakaushik committed May 30, 2019
1 parent 9baf6ec commit 4900e75
Show file tree
Hide file tree
Showing 25 changed files with 1,432 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/google_maps_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.15

* Add support for Polygons.

## 0.5.14+1

* Example app update(comment out usage of the ImageStreamListener API which has a breaking change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ static Object markerIdToJson(String markerId) {
return data;
}

static Object polygonIdToJson(String polygonId) {
if (polygonId == null) {
return null;
}
final Map<String, Object> data = new HashMap<>(1);
data.put("polygonId", polygonId);
return data;
}

static Object polylineIdToJson(String polylineId) {
if (polylineId == null) {
return null;
Expand Down Expand Up @@ -364,6 +373,48 @@ private static void interpretInfoWindowOptions(
}
}

static String interpretPolygonOptions(Object o, PolygonOptionsSink sink) {
final Map<?, ?> data = toMap(o);
final Object consumeTapEvents = data.get("consumeTapEvents");
if (consumeTapEvents != null) {
sink.setConsumeTapEvents(toBoolean(consumeTapEvents));
}
final Object geodesic = data.get("geodesic");
if (geodesic != null) {
sink.setGeodesic(toBoolean(geodesic));
}
final Object visible = data.get("visible");
if (visible != null) {
sink.setVisible(toBoolean(visible));
}
final Object fillColor = data.get("fillColor");
if (fillColor != null) {
sink.setFillColor(toInt(fillColor));
}
final Object strokeColor = data.get("strokeColor");
if (strokeColor != null) {
sink.setStrokeColor(toInt(strokeColor));
}
final Object strokeWidth = data.get("strokeWidth");
if (strokeWidth != null) {
sink.setStrokeWidth(toInt(strokeWidth));
}
final Object zIndex = data.get("zIndex");
if (zIndex != null) {
sink.setZIndex(toFloat(zIndex));
}
final Object points = data.get("points");
if (points != null) {
sink.setPoints(toPoints(points));
}
final String polygonId = (String) data.get("polygonId");
if (polygonId == null) {
throw new IllegalArgumentException("polygonId was null");
} else {
return polygonId;
}
}

static String interpretPolylineOptions(Object o, PolylineOptionsSink sink) {
final Map<?, ?> data = toMap(o);
final Object consumeTapEvents = data.get("consumeTapEvents");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GoogleMapBuilder implements GoogleMapOptionsSink {
private boolean myLocationEnabled = false;
private boolean myLocationButtonEnabled = false;
private Object initialMarkers;
private Object initialPolygons;
private Object initialPolylines;
private Object initialCircles;

Expand All @@ -29,6 +30,7 @@ GoogleMapController build(
controller.setMyLocationButtonEnabled(myLocationButtonEnabled);
controller.setTrackCameraPosition(trackCameraPosition);
controller.setInitialMarkers(initialMarkers);
controller.setInitialPolygons(initialPolygons);
controller.setInitialPolylines(initialPolylines);
controller.setInitialCircles(initialCircles);
return controller;
Expand Down Expand Up @@ -103,6 +105,11 @@ public void setInitialMarkers(Object initialMarkers) {
this.initialMarkers = initialMarkers;
}

@Override
public void setInitialPolygons(Object initialPolygons) {
this.initialPolygons = initialPolygons;
}

@Override
public void setInitialPolylines(Object initialPolylines) {
this.initialPolylines = initialPolylines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.Polyline;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -50,6 +51,7 @@ final class GoogleMapController
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnPolygonClickListener,
GoogleMap.OnPolylineClickListener,
GoogleMap.OnCircleClickListener,
GoogleMapOptionsSink,
Expand All @@ -75,9 +77,11 @@ final class GoogleMapController
private final int registrarActivityHashCode;
private final Context context;
private final MarkersController markersController;
private final PolygonsController polygonsController;
private final PolylinesController polylinesController;
private final CirclesController circlesController;
private List<Object> initialMarkers;
private List<Object> initialPolygons;
private List<Object> initialPolylines;
private List<Object> initialCircles;

Expand All @@ -98,6 +102,7 @@ final class GoogleMapController
methodChannel.setMethodCallHandler(this);
this.registrarActivityHashCode = registrar.activity().hashCode();
this.markersController = new MarkersController(methodChannel);
this.polygonsController = new PolygonsController(methodChannel);
this.polylinesController = new PolylinesController(methodChannel);
this.circlesController = new CirclesController(methodChannel);
}
Expand Down Expand Up @@ -169,15 +174,18 @@ public void onMapReady(GoogleMap googleMap) {
googleMap.setOnCameraMoveListener(this);
googleMap.setOnCameraIdleListener(this);
googleMap.setOnMarkerClickListener(this);
googleMap.setOnPolygonClickListener(this);
googleMap.setOnPolylineClickListener(this);
googleMap.setOnCircleClickListener(this);
googleMap.setOnMapClickListener(this);
googleMap.setOnMapLongClickListener(this);
updateMyLocationSettings();
markersController.setGoogleMap(googleMap);
polygonsController.setGoogleMap(googleMap);
polylinesController.setGoogleMap(googleMap);
circlesController.setGoogleMap(googleMap);
updateInitialMarkers();
updateInitialPolygons();
updateInitialPolylines();
updateInitialCircles();
}
Expand Down Expand Up @@ -238,6 +246,17 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
result.success(null);
break;
}
case "polygons#update":
{
Object polygonsToAdd = call.argument("polygonsToAdd");
polygonsController.addPolygons((List<Object>) polygonsToAdd);
Object polygonsToChange = call.argument("polygonsToChange");
polygonsController.changePolygons((List<Object>) polygonsToChange);
Object polygonIdsToRemove = call.argument("polygonIdsToRemove");
polygonsController.removePolygons((List<Object>) polygonIdsToRemove);
result.success(null);
break;
}
case "polylines#update":
{
Object polylinesToAdd = call.argument("polylinesToAdd");
Expand Down Expand Up @@ -350,6 +369,11 @@ public boolean onMarkerClick(Marker marker) {
return markersController.onMarkerTap(marker.getId());
}

@Override
public void onPolygonClick(Polygon polygon) {
polygonsController.onPolygonTap(polygon.getId());
}

@Override
public void onPolylineClick(Polyline polyline) {
polylinesController.onPolylineTap(polyline.getId());
Expand Down Expand Up @@ -514,6 +538,18 @@ private void updateInitialMarkers() {
markersController.addMarkers(initialMarkers);
}

@Override
public void setInitialPolygons(Object initialPolygons) {
this.initialPolygons = (List<Object>) initialPolygons;
if (googleMap != null) {
updateInitialPolygons();
}
}

private void updateInitialPolygons() {
polygonsController.addPolygons(initialPolygons);
}

@Override
public void setInitialPolylines(Object initialPolylines) {
this.initialPolylines = (List<Object>) initialPolylines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public PlatformView create(Context context, int id, Object args) {
if (params.containsKey("markersToAdd")) {
builder.setInitialMarkers(params.get("markersToAdd"));
}
if (params.containsKey("polygonsToAdd")) {
builder.setInitialPolygons(params.get("polygonsToAdd"));
}
if (params.containsKey("polylinesToAdd")) {
builder.setInitialPolylines(params.get("polylinesToAdd"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface GoogleMapOptionsSink {

void setInitialMarkers(Object initialMarkers);

void setInitialPolygons(Object initialPolygons);

void setInitialPolylines(Object initialPolylines);

void setInitialCircles(Object initialCircles);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolygonOptions;
import java.util.List;

class PolygonBuilder implements PolygonOptionsSink {
private final PolygonOptions polygonOptions;
private boolean consumeTapEvents;

PolygonBuilder() {
this.polygonOptions = new PolygonOptions();
}

PolygonOptions build() {
return polygonOptions;
}

boolean consumeTapEvents() {
return consumeTapEvents;
}

@Override
public void setFillColor(int color) {
polygonOptions.fillColor(color);
}

@Override
public void setStrokeColor(int color) {
polygonOptions.strokeColor(color);
}

@Override
public void setPoints(List<LatLng> points) {
polygonOptions.addAll(points);
}

@Override
public void setConsumeTapEvents(boolean consumeTapEvents) {
this.consumeTapEvents = consumeTapEvents;
polygonOptions.clickable(consumeTapEvents);
}

@Override
public void setGeodesic(boolean geodisc) {
polygonOptions.geodesic(geodisc);
}

@Override
public void setVisible(boolean visible) {
polygonOptions.visible(visible);
}

@Override
public void setStrokeWidth(float width) {
polygonOptions.strokeWidth(width);
}

@Override
public void setZIndex(float zIndex) {
polygonOptions.zIndex(zIndex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polygon;
import java.util.List;

/** Controller of a single Polygon on the map. */
class PolygonController implements PolygonOptionsSink {
private final Polygon polygon;
private final String googleMapsPolygonId;
private boolean consumeTapEvents;

PolygonController(Polygon polygon, boolean consumeTapEvents) {
this.polygon = polygon;
this.consumeTapEvents = consumeTapEvents;
this.googleMapsPolygonId = polygon.getId();
}

void remove() {
polygon.remove();
}

@Override
public void setConsumeTapEvents(boolean consumeTapEvents) {
this.consumeTapEvents = consumeTapEvents;
polygon.setClickable(consumeTapEvents);
}

@Override
public void setFillColor(int color) {
polygon.setFillColor(color);
}

@Override
public void setStrokeColor(int color) {
polygon.setStrokeColor(color);
}

@Override
public void setGeodesic(boolean geodesic) {
polygon.setGeodesic(geodesic);
}

@Override
public void setPoints(List<LatLng> points) {
polygon.setPoints(points);
}

@Override
public void setVisible(boolean visible) {
polygon.setVisible(visible);
}

@Override
public void setStrokeWidth(float width) {
polygon.setStrokeWidth(width);
}

@Override
public void setZIndex(float zIndex) {
polygon.setZIndex(zIndex);
}

String getGoogleMapsPolygonId() {
return googleMapsPolygonId;
}

boolean consumeTapEvents() {
return consumeTapEvents;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.LatLng;
import java.util.List;

/** Receiver of Polygon configuration options. */
interface PolygonOptionsSink {

void setConsumeTapEvents(boolean consumetapEvents);

void setFillColor(int color);

void setStrokeColor(int color);

void setGeodesic(boolean geodesic);

void setPoints(List<LatLng> points);

void setVisible(boolean visible);

void setStrokeWidth(float width);

void setZIndex(float zIndex);
}

0 comments on commit 4900e75

Please sign in to comment.