Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.18.9

* Updates heatmaps passed between Dart and native to use typed data.

## 2.18.8

* Bumps com.google.maps.android:android-maps-utils from 3.6.0 to 3.19.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/** Conversions between JSON-like values and GoogleMaps data types. */
class Convert {
// These constants must match the corresponding constants in serialization.dart
public static final String HEATMAP_ID_KEY = "heatmapId";
public static final String HEATMAP_DATA_KEY = "data";
public static final String HEATMAP_GRADIENT_KEY = "gradient";
public static final String HEATMAP_MAX_INTENSITY_KEY = "maxIntensity";
public static final String HEATMAP_OPACITY_KEY = "opacity";
public static final String HEATMAP_RADIUS_KEY = "radius";
public static final String HEATMAP_GRADIENT_COLORS_KEY = "colors";
public static final String HEATMAP_GRADIENT_START_POINTS_KEY = "startPoints";
public static final String HEATMAP_GRADIENT_COLOR_MAP_SIZE_KEY = "colorMapSize";

private static BitmapDescriptor toBitmapDescriptor(
Messages.PlatformBitmap platformBitmap, AssetManager assetManager, float density) {
return toBitmapDescriptor(
Expand Down Expand Up @@ -328,14 +316,6 @@ static CameraUpdate cameraUpdateFromPigeon(Messages.PlatformCameraUpdate update,
"PlatformCameraUpdate's cameraUpdate field must be one of the PlatformCameraUpdate... case classes.");
}

private static double toDouble(Object o) {
return ((Number) o).doubleValue();
}

private static float toFloat(Object o) {
return ((Number) o).floatValue();
}

private static @Nullable Float nullableDoubleToFloat(@Nullable Double d) {
return (d == null) ? null : d.floatValue();
}
Expand Down Expand Up @@ -437,20 +417,15 @@ static Messages.PlatformCluster clusterToPigeon(
.build();
}

static LatLng toLatLng(Object o) {
final List<?> data = toList(o);
return new LatLng(toDouble(data.get(0)), toDouble(data.get(1)));
}

/**
* Converts a list of serialized weighted lat/lng to a list of WeightedLatLng.
* Converts a Pigeon weighted lat/lng to a WeightedLatLng.
*
* @param o The serialized list of weighted lat/lng.
* @param weightedLatLng The Pigeon weighted lat/lng.
* @return The list of WeightedLatLng.
*/
static WeightedLatLng toWeightedLatLng(Object o) {
final List<?> data = toList(o);
return new WeightedLatLng(toLatLng(data.get(0)), toDouble(data.get(1)));
static WeightedLatLng weightedLatLngFromPigeon(Messages.PlatformWeightedLatLng weightedLatLng) {
return new WeightedLatLng(
latLngFromPigeon(weightedLatLng.getPoint()), weightedLatLng.getWeight());
}

static Point pointFromPigeon(Messages.PlatformPoint point) {
Expand All @@ -469,14 +444,6 @@ static Messages.PlatformPoint pointToPigeon(Point point) {
return new Messages.PlatformPoint.Builder().setX((long) point.x).setY((long) point.y).build();
}

private static List<?> toList(Object o) {
return (List<?>) o;
}

private static Map<?, ?> toMap(Object o) {
return (Map<?, ?>) o;
}

private static Bitmap toBitmap(byte[] bmpData) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bmpData, 0, bmpData.length);
if (bitmap == null) {
Expand Down Expand Up @@ -606,7 +573,7 @@ static void interpretMarkerOptions(
sink.setFlat(marker.getFlat());
sink.setIcon(toBitmapDescriptor(marker.getIcon(), assetManager, density, wrapper));
interpretInfoWindowOptions(sink, marker.getInfoWindow());
sink.setPosition(toLatLng(marker.getPosition().toList()));
sink.setPosition(latLngFromPigeon(marker.getPosition()));
sink.setRotation(marker.getRotation().floatValue());
sink.setVisible(marker.getVisible());
sink.setZIndex(marker.getZIndex().floatValue());
Expand Down Expand Up @@ -673,7 +640,7 @@ static String interpretCircleOptions(Messages.PlatformCircle circle, CircleOptio
sink.setStrokeColor(circle.getStrokeColor().getArgbValue().intValue());
sink.setStrokeWidth(circle.getStrokeWidth());
sink.setZIndex(circle.getZIndex().floatValue());
sink.setCenter(toLatLng(circle.getCenter().toList()));
sink.setCenter(latLngFromPigeon(circle.getCenter()));
sink.setRadius(circle.getRadius());
sink.setVisible(circle.getVisible());
return circle.getCircleId();
Expand All @@ -699,33 +666,19 @@ static String interpretCircleOptions(Messages.PlatformCircle circle, CircleOptio
* @return the heatmapId.
* @throws IllegalArgumentException if heatmapId is null.
*/
static String interpretHeatmapOptions(Map<String, ?> data, HeatmapOptionsSink sink) {
final Object rawWeightedData = data.get(HEATMAP_DATA_KEY);
if (rawWeightedData != null) {
sink.setWeightedData(toWeightedData(rawWeightedData));
}
final Object gradient = data.get(HEATMAP_GRADIENT_KEY);
static String interpretHeatmapOptions(Messages.PlatformHeatmap heatmap, HeatmapOptionsSink sink) {
sink.setWeightedData(weightedDataFromPigeon(heatmap.getData()));
final Messages.PlatformHeatmapGradient gradient = heatmap.getGradient();
if (gradient != null) {
sink.setGradient(toGradient(gradient));
sink.setGradient(gradientFromPigeon(gradient));
}
final Object maxIntensity = data.get(HEATMAP_MAX_INTENSITY_KEY);
final Double maxIntensity = heatmap.getMaxIntensity();
if (maxIntensity != null) {
sink.setMaxIntensity(toDouble(maxIntensity));
}
final Object opacity = data.get(HEATMAP_OPACITY_KEY);
if (opacity != null) {
sink.setOpacity(toDouble(opacity));
}
final Object radius = data.get(HEATMAP_RADIUS_KEY);
if (radius != null) {
sink.setRadius(toInt(radius));
}
final String heatmapId = (String) data.get(HEATMAP_ID_KEY);
if (heatmapId == null) {
throw new IllegalArgumentException("heatmapId was null");
} else {
return heatmapId;
sink.setMaxIntensity(maxIntensity);
}
sink.setOpacity(heatmap.getOpacity());
sink.setRadius(heatmap.getRadius().intValue());
return heatmap.getHeatmapId();
}

static List<LatLng> pointsFromPigeon(List<Messages.PlatformLatLng> data) {
Expand All @@ -740,57 +693,40 @@ static List<LatLng> pointsFromPigeon(List<Messages.PlatformLatLng> data) {
/**
* Converts the given object to a list of WeightedLatLng objects.
*
* @param o the object to convert. The object is expected to be a List of serialized weighted
* lat/lng.
* @param data the list of Pigeon weighted lat/lng objects to convert.
* @return a list of WeightedLatLng objects.
*/
@VisibleForTesting
static List<WeightedLatLng> toWeightedData(Object o) {
final List<?> data = toList(o);
static List<WeightedLatLng> weightedDataFromPigeon(List<Messages.PlatformWeightedLatLng> data) {
final List<WeightedLatLng> weightedData = new ArrayList<>(data.size());

for (Object rawWeightedPoint : data) {
weightedData.add(toWeightedLatLng(rawWeightedPoint));
for (Messages.PlatformWeightedLatLng rawWeightedPoint : data) {
weightedData.add(weightedLatLngFromPigeon(rawWeightedPoint));
}
return weightedData;
}

/**
* Converts the given object to a Gradient object.
*
* @param o the object to convert. The object is expected to be a Map containing the gradient
* options. The gradient map is expected to have the following structure:
* <pre>{@code
* {
* "colors": List<Integer>,
* "startPoints": List<Float>,
* "colorMapSize": Integer
* }
* }</pre>
* Converts the given Pigeon gradient to a Gradient object.
*
* @param gradient the Pigeon gradient to convert.
* @return a Gradient object.
*/
@VisibleForTesting
static Gradient toGradient(Object o) {
final Map<?, ?> data = toMap(o);

final List<?> colorData = toList(data.get(HEATMAP_GRADIENT_COLORS_KEY));
assert colorData != null;
static Gradient gradientFromPigeon(Messages.PlatformHeatmapGradient gradient) {
final List<Messages.PlatformColor> colorData = gradient.getColors();
final int[] colors = new int[colorData.size()];
for (int i = 0; i < colorData.size(); i++) {
colors[i] = toInt(colorData.get(i));
colors[i] = colorData.get(i).getArgbValue().intValue();
}

final List<?> startPointData = toList(data.get(HEATMAP_GRADIENT_START_POINTS_KEY));
assert startPointData != null;
final List<Double> startPointData = gradient.getStartPoints();
final float[] startPoints = new float[startPointData.size()];
for (int i = 0; i < startPointData.size(); i++) {
startPoints[i] = toFloat(startPointData.get(i));
startPoints[i] = startPointData.get(i).floatValue();
}

final int colorMapSize = toInt(data.get(HEATMAP_GRADIENT_COLOR_MAP_SIZE_KEY));

return new Gradient(colors, startPoints, colorMapSize);
return new Gradient(colors, startPoints, gradient.getColorMapSize().intValue());
}

private static List<List<LatLng>> toHoles(List<List<Messages.PlatformLatLng>> data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package io.flutter.plugins.googlemaps;

import static io.flutter.plugins.googlemaps.Convert.HEATMAP_ID_KEY;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.maps.GoogleMap;
Expand Down Expand Up @@ -36,14 +34,22 @@ void setGoogleMap(GoogleMap googleMap) {
/** Adds heatmaps to the map. */
void addHeatmaps(@NonNull List<Messages.PlatformHeatmap> heatmapsToAdd) {
for (Messages.PlatformHeatmap heatmapToAdd : heatmapsToAdd) {
addJsonHeatmap(heatmapToAdd.getJson());
HeatmapBuilder heatmapBuilder = new HeatmapBuilder();
String heatmapId = Convert.interpretHeatmapOptions(heatmapToAdd, heatmapBuilder);
HeatmapTileProvider options = buildHeatmap(heatmapBuilder);
addHeatmap(heatmapId, options);
}
}

/** Updates the given heatmaps on the map. */
void changeHeatmaps(@NonNull List<Messages.PlatformHeatmap> heatmapsToChange) {
for (Messages.PlatformHeatmap heatmapToChange : heatmapsToChange) {
changeJsonHeatmap(heatmapToChange.getJson());
String heatmapId = heatmapToChange.getHeatmapId();
HeatmapController heatmapController = heatmapIdToController.get(heatmapId);
if (heatmapController != null) {
Convert.interpretHeatmapOptions(heatmapToChange, heatmapController);
heatmapController.clearTileCache();
}
}
}

Expand All @@ -64,40 +70,11 @@ void removeHeatmaps(@NonNull List<String> heatmapIdsToRemove) {
return builder.build();
}

/** Adds a heatmap to the map from json data. */
private void addJsonHeatmap(Map<String, ?> heatmap) {
if (heatmap == null) {
return;
}
HeatmapBuilder heatmapBuilder = new HeatmapBuilder();
String heatmapId = Convert.interpretHeatmapOptions(heatmap, heatmapBuilder);
HeatmapTileProvider options = buildHeatmap(heatmapBuilder);
addHeatmap(heatmapId, options);
}

/** Adds a heatmap to the map. */
private void addHeatmap(String heatmapId, HeatmapTileProvider options) {
TileOverlay heatmapTileOverlay =
googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(options));
HeatmapController heatmapController = new HeatmapController(options, heatmapTileOverlay);
heatmapIdToController.put(heatmapId, heatmapController);
}

/** Updates the given heatmap on the map. */
private void changeJsonHeatmap(Map<String, ?> heatmap) {
if (heatmap == null) {
return;
}
String heatmapId = getHeatmapId(heatmap);
HeatmapController heatmapController = heatmapIdToController.get(heatmapId);
if (heatmapController != null) {
Convert.interpretHeatmapOptions(heatmap, heatmapController);
heatmapController.clearTileCache();
}
}

/** Returns the heatmap id from the given heatmap data. */
private static String getHeatmapId(Map<String, ?> heatmap) {
return (String) heatmap.get(HEATMAP_ID_KEY);
}
}
Loading
Loading