Skip to content

Commit

Permalink
[google_maps_flutter] add tile overlays (flutter#3434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Yang authored Feb 3, 2021
1 parent 47a5ea7 commit 37d658e
Show file tree
Hide file tree
Showing 24 changed files with 1,505 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ android {
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.android.gms.maps.model.PatternItem;
import com.google.android.gms.maps.model.RoundCap;
import com.google.android.gms.maps.model.SquareCap;
import com.google.android.gms.maps.model.Tile;
import io.flutter.view.FlutterMain;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -78,7 +79,8 @@ private static BitmapDescriptor getBitmapFromBytes(List<?> data) {
}
} else {
throw new IllegalArgumentException(
"fromBytes should have exactly one argument, the bytes. Got: " + data.size());
"fromBytes should have exactly one argument, interpretTileOverlayOptions the bytes. Got: "
+ data.size());
}
}

Expand Down Expand Up @@ -200,6 +202,20 @@ static Object circleIdToJson(String circleId) {
return data;
}

static Map<String, Object> tileOverlayArgumentsToJson(
String tileOverlayId, int x, int y, int zoom) {

if (tileOverlayId == null) {
return null;
}
final Map<String, Object> data = new HashMap<>(4);
data.put("tileOverlayId", tileOverlayId);
data.put("x", x);
data.put("y", y);
data.put("zoom", zoom);
return data;
}

static Object latLngToJson(LatLng latLng) {
return Arrays.asList(latLng.latitude, latLng.longitude);
}
Expand Down Expand Up @@ -645,4 +661,39 @@ private static Cap toCap(Object o) {
throw new IllegalArgumentException("Cannot interpret " + o + " as Cap");
}
}

static String interpretTileOverlayOptions(Map<String, ?> data, TileOverlaySink sink) {
final Object fadeIn = data.get("fadeIn");
if (fadeIn != null) {
sink.setFadeIn(toBoolean(fadeIn));
}
final Object transparency = data.get("transparency");
if (transparency != null) {
sink.setTransparency(toFloat(transparency));
}
final Object zIndex = data.get("zIndex");
if (zIndex != null) {
sink.setZIndex(toFloat(zIndex));
}
final Object visible = data.get("visible");
if (visible != null) {
sink.setVisible(toBoolean(visible));
}
final String tileOverlayId = (String) data.get("tileOverlayId");
if (tileOverlayId == null) {
throw new IllegalArgumentException("tileOverlayId was null");
} else {
return tileOverlayId;
}
}

static Tile interpretTile(Map<String, ?> data) {
int width = toInt(data.get("width"));
int height = toInt(data.get("height"));
byte[] dataArray = null;
if (data.get("data") != null) {
dataArray = (byte[]) data.get("data");
}
return new Tile(width, height, dataArray);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLngBounds;
import io.flutter.plugin.common.BinaryMessenger;
import java.util.List;
import java.util.Map;

class GoogleMapBuilder implements GoogleMapOptionsSink {
private final GoogleMapOptions options = new GoogleMapOptions();
Expand All @@ -23,6 +25,7 @@ class GoogleMapBuilder implements GoogleMapOptionsSink {
private Object initialPolygons;
private Object initialPolylines;
private Object initialCircles;
private List<Map<String, ?>> initialTileOverlays;
private Rect padding = new Rect(0, 0, 0, 0);

GoogleMapController build(
Expand All @@ -44,6 +47,7 @@ GoogleMapController build(
controller.setInitialPolylines(initialPolylines);
controller.setInitialCircles(initialCircles);
controller.setPadding(padding.top, padding.left, padding.bottom, padding.right);
controller.setInitialTileOverlays(initialTileOverlays);
return controller;
}

Expand Down Expand Up @@ -165,4 +169,9 @@ public void setInitialPolylines(Object initialPolylines) {
public void setInitialCircles(Object initialCircles) {
this.initialCircles = initialCircles;
}

@Override
public void setInitialTileOverlays(List<Map<String, ?>> initialTileOverlays) {
this.initialTileOverlays = initialTileOverlays;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ final class GoogleMapController
private final PolygonsController polygonsController;
private final PolylinesController polylinesController;
private final CirclesController circlesController;
private final TileOverlaysController tileOverlaysController;
private List<Object> initialMarkers;
private List<Object> initialPolygons;
private List<Object> initialPolylines;
private List<Object> initialCircles;
private List<Map<String, ?>> initialTileOverlays;

GoogleMapController(
int id,
Expand All @@ -99,6 +101,7 @@ final class GoogleMapController
this.polygonsController = new PolygonsController(methodChannel, density);
this.polylinesController = new PolylinesController(methodChannel, density);
this.circlesController = new CirclesController(methodChannel, density);
this.tileOverlaysController = new TileOverlaysController(methodChannel);
}

@Override
Expand Down Expand Up @@ -140,10 +143,12 @@ public void onMapReady(GoogleMap googleMap) {
polygonsController.setGoogleMap(googleMap);
polylinesController.setGoogleMap(googleMap);
circlesController.setGoogleMap(googleMap);
tileOverlaysController.setGoogleMap(googleMap);
updateInitialMarkers();
updateInitialPolygons();
updateInitialPolylines();
updateInitialCircles();
updateInitialTileOverlays();
}

@Override
Expand Down Expand Up @@ -385,6 +390,30 @@ public void onSnapshotReady(Bitmap bitmap) {
result.success(mapStyleResult);
break;
}
case "tileOverlays#update":
{
List<Map<String, ?>> tileOverlaysToAdd = call.argument("tileOverlaysToAdd");
tileOverlaysController.addTileOverlays(tileOverlaysToAdd);
List<Map<String, ?>> tileOverlaysToChange = call.argument("tileOverlaysToChange");
tileOverlaysController.changeTileOverlays(tileOverlaysToChange);
List<String> tileOverlaysToRemove = call.argument("tileOverlayIdsToRemove");
tileOverlaysController.removeTileOverlays(tileOverlaysToRemove);
result.success(null);
break;
}
case "tileOverlays#clearTileCache":
{
String tileOverlayId = call.argument("tileOverlayId");
tileOverlaysController.clearTileCache(tileOverlayId);
result.success(null);
break;
}
case "map#getTileOverlayInfo":
{
String tileOverlayId = call.argument("tileOverlayId");
result.success(tileOverlaysController.getTileOverlayInfo(tileOverlayId));
break;
}
default:
result.notImplemented();
}
Expand Down Expand Up @@ -732,6 +761,18 @@ private void updateInitialCircles() {
circlesController.addCircles(initialCircles);
}

@Override
public void setInitialTileOverlays(List<Map<String, ?>> initialTileOverlays) {
this.initialTileOverlays = initialTileOverlays;
if (googleMap != null) {
updateInitialTileOverlays();
}
}

private void updateInitialTileOverlays() {
tileOverlaysController.addTileOverlays(initialTileOverlays);
}

@SuppressLint("MissingPermission")
private void updateMyLocationSettings() {
if (hasLocationPermission()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
import java.util.List;
import java.util.Map;

public class GoogleMapFactory extends PlatformViewFactory {
Expand Down Expand Up @@ -46,6 +47,9 @@ public PlatformView create(Context context, int id, Object args) {
if (params.containsKey("circlesToAdd")) {
builder.setInitialCircles(params.get("circlesToAdd"));
}
if (params.containsKey("tileOverlaysToAdd")) {
builder.setInitialTileOverlays((List<Map<String, ?>>) params.get("tileOverlaysToAdd"));
}
return builder.build(id, context, binaryMessenger, lifecycleProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package io.flutter.plugins.googlemaps;

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

/** Receiver of GoogleMap configuration options. */
interface GoogleMapOptionsSink {
Expand Down Expand Up @@ -51,4 +53,6 @@ interface GoogleMapOptionsSink {
void setInitialPolylines(Object initialPolylines);

void setInitialCircles(Object initialCircles);

void setInitialTileOverlays(List<Map<String, ?>> initialTileOverlays);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;

class TileOverlayBuilder implements TileOverlaySink {

private final TileOverlayOptions tileOverlayOptions;

TileOverlayBuilder() {
this.tileOverlayOptions = new TileOverlayOptions();
}

TileOverlayOptions build() {
return tileOverlayOptions;
}

@Override
public void setFadeIn(boolean fadeIn) {
tileOverlayOptions.fadeIn(fadeIn);
}

@Override
public void setTransparency(float transparency) {
tileOverlayOptions.transparency(transparency);
}

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

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

@Override
public void setTileProvider(TileProvider tileProvider) {
tileOverlayOptions.tileProvider(tileProvider);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileProvider;
import java.util.HashMap;
import java.util.Map;

class TileOverlayController implements TileOverlaySink {

private final TileOverlay tileOverlay;

TileOverlayController(TileOverlay tileOverlay) {
this.tileOverlay = tileOverlay;
}

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

void clearTileCache() {
tileOverlay.clearTileCache();
}

Map<String, Object> getTileOverlayInfo() {
Map<String, Object> tileOverlayInfo = new HashMap<>();
tileOverlayInfo.put("fadeIn", tileOverlay.getFadeIn());
tileOverlayInfo.put("transparency", tileOverlay.getTransparency());
tileOverlayInfo.put("id", tileOverlay.getId());
tileOverlayInfo.put("zIndex", tileOverlay.getZIndex());
tileOverlayInfo.put("visible", tileOverlay.isVisible());
return tileOverlayInfo;
}

@Override
public void setFadeIn(boolean fadeIn) {
tileOverlay.setFadeIn(fadeIn);
}

@Override
public void setTransparency(float transparency) {
tileOverlay.setTransparency(transparency);
}

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

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

@Override
public void setTileProvider(TileProvider tileProvider) {
// You can not change tile provider after creation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.googlemaps;

import com.google.android.gms.maps.model.TileProvider;

/** Receiver of TileOverlayOptions configuration. */
interface TileOverlaySink {
void setFadeIn(boolean fadeIn);

void setTransparency(float transparency);

void setZIndex(float zIndex);

void setVisible(boolean visible);

void setTileProvider(TileProvider tileProvider);
}
Loading

0 comments on commit 37d658e

Please sign in to comment.