30
30
import com .google .android .gms .maps .model .SquareCap ;
31
31
import com .google .android .gms .maps .model .Tile ;
32
32
import com .google .maps .android .clustering .Cluster ;
33
+ import com .google .maps .android .heatmaps .Gradient ;
34
+ import com .google .maps .android .heatmaps .WeightedLatLng ;
33
35
import io .flutter .FlutterInjector ;
34
36
import java .io .IOException ;
35
37
import java .io .InputStream ;
41
43
42
44
/** Conversions between JSON-like values and GoogleMaps data types. */
43
45
class Convert {
46
+ // These constants must match the corresponding constants in serialization.dart
47
+ public static final String HEATMAPS_TO_ADD_KEY = "heatmapsToAdd" ;
48
+ public static final String HEATMAP_ID_KEY = "heatmapId" ;
49
+ public static final String HEATMAP_DATA_KEY = "data" ;
50
+ public static final String HEATMAP_GRADIENT_KEY = "gradient" ;
51
+ public static final String HEATMAP_MAX_INTENSITY_KEY = "maxIntensity" ;
52
+ public static final String HEATMAP_OPACITY_KEY = "opacity" ;
53
+ public static final String HEATMAP_RADIUS_KEY = "radius" ;
54
+ public static final String HEATMAP_GRADIENT_COLORS_KEY = "colors" ;
55
+ public static final String HEATMAP_GRADIENT_START_POINTS_KEY = "startPoints" ;
56
+ public static final String HEATMAP_GRADIENT_COLOR_MAP_SIZE_KEY = "colorMapSize" ;
44
57
45
58
private static BitmapDescriptor toBitmapDescriptor (
46
59
Object o , AssetManager assetManager , float density ) {
@@ -465,6 +478,17 @@ static LatLng toLatLng(Object o) {
465
478
return new LatLng (toDouble (data .get (0 )), toDouble (data .get (1 )));
466
479
}
467
480
481
+ /**
482
+ * Converts a list of serialized weighted lat/lng to a list of WeightedLatLng.
483
+ *
484
+ * @param o The serialized list of weighted lat/lng.
485
+ * @return The list of WeightedLatLng.
486
+ */
487
+ static WeightedLatLng toWeightedLatLng (Object o ) {
488
+ final List <?> data = toList (o );
489
+ return new WeightedLatLng (toLatLng (data .get (0 )), toDouble (data .get (1 )));
490
+ }
491
+
468
492
static Point pointFromPigeon (Messages .PlatformPoint point ) {
469
493
return new Point (point .getX ().intValue (), point .getY ().intValue ());
470
494
}
@@ -842,6 +866,55 @@ static String interpretCircleOptions(Map<String, ?> data, CircleOptionsSink sink
842
866
}
843
867
}
844
868
869
+ /**
870
+ * Set the options in the given heatmap object to the given sink.
871
+ *
872
+ * @param o the object expected to be a Map containing the heatmap options. The options map is
873
+ * expected to have the following structure:
874
+ * <pre>{@code
875
+ * {
876
+ * "heatmapId": String,
877
+ * "data": List, // List of serialized weighted lat/lng
878
+ * "gradient": Map, // Serialized heatmap gradient
879
+ * "maxIntensity": Double,
880
+ * "opacity": Double,
881
+ * "radius": Integer
882
+ * }
883
+ * }</pre>
884
+ *
885
+ * @param sink the HeatmapOptionsSink where the options will be set.
886
+ * @return the heatmapId.
887
+ * @throws IllegalArgumentException if heatmapId is null.
888
+ */
889
+ static String interpretHeatmapOptions (Map <String , ?> data , HeatmapOptionsSink sink ) {
890
+ final Object rawWeightedData = data .get (HEATMAP_DATA_KEY );
891
+ if (rawWeightedData != null ) {
892
+ sink .setWeightedData (toWeightedData (rawWeightedData ));
893
+ }
894
+ final Object gradient = data .get (HEATMAP_GRADIENT_KEY );
895
+ if (gradient != null ) {
896
+ sink .setGradient (toGradient (gradient ));
897
+ }
898
+ final Object maxIntensity = data .get (HEATMAP_MAX_INTENSITY_KEY );
899
+ if (maxIntensity != null ) {
900
+ sink .setMaxIntensity (toDouble (maxIntensity ));
901
+ }
902
+ final Object opacity = data .get (HEATMAP_OPACITY_KEY );
903
+ if (opacity != null ) {
904
+ sink .setOpacity (toDouble (opacity ));
905
+ }
906
+ final Object radius = data .get (HEATMAP_RADIUS_KEY );
907
+ if (radius != null ) {
908
+ sink .setRadius (toInt (radius ));
909
+ }
910
+ final String heatmapId = (String ) data .get (HEATMAP_ID_KEY );
911
+ if (heatmapId == null ) {
912
+ throw new IllegalArgumentException ("heatmapId was null" );
913
+ } else {
914
+ return heatmapId ;
915
+ }
916
+ }
917
+
845
918
@ VisibleForTesting
846
919
static List <LatLng > toPoints (Object o ) {
847
920
final List <?> data = toList (o );
@@ -854,6 +927,62 @@ static List<LatLng> toPoints(Object o) {
854
927
return points ;
855
928
}
856
929
930
+ /**
931
+ * Converts the given object to a list of WeightedLatLng objects.
932
+ *
933
+ * @param o the object to convert. The object is expected to be a List of serialized weighted
934
+ * lat/lng.
935
+ * @return a list of WeightedLatLng objects.
936
+ */
937
+ @ VisibleForTesting
938
+ static List <WeightedLatLng > toWeightedData (Object o ) {
939
+ final List <?> data = toList (o );
940
+ final List <WeightedLatLng > weightedData = new ArrayList <>(data .size ());
941
+
942
+ for (Object rawWeightedPoint : data ) {
943
+ weightedData .add (toWeightedLatLng (rawWeightedPoint ));
944
+ }
945
+ return weightedData ;
946
+ }
947
+
948
+ /**
949
+ * Converts the given object to a Gradient object.
950
+ *
951
+ * @param o the object to convert. The object is expected to be a Map containing the gradient
952
+ * options. The gradient map is expected to have the following structure:
953
+ * <pre>{@code
954
+ * {
955
+ * "colors": List<Integer>,
956
+ * "startPoints": List<Float>,
957
+ * "colorMapSize": Integer
958
+ * }
959
+ * }</pre>
960
+ *
961
+ * @return a Gradient object.
962
+ */
963
+ @ VisibleForTesting
964
+ static Gradient toGradient (Object o ) {
965
+ final Map <?, ?> data = toMap (o );
966
+
967
+ final List <?> colorData = toList (data .get (HEATMAP_GRADIENT_COLORS_KEY ));
968
+ assert colorData != null ;
969
+ final int [] colors = new int [colorData .size ()];
970
+ for (int i = 0 ; i < colorData .size (); i ++) {
971
+ colors [i ] = toInt (colorData .get (i ));
972
+ }
973
+
974
+ final List <?> startPointData = toList (data .get (HEATMAP_GRADIENT_START_POINTS_KEY ));
975
+ assert startPointData != null ;
976
+ final float [] startPoints = new float [startPointData .size ()];
977
+ for (int i = 0 ; i < startPointData .size (); i ++) {
978
+ startPoints [i ] = toFloat (startPointData .get (i ));
979
+ }
980
+
981
+ final int colorMapSize = toInt (data .get (HEATMAP_GRADIENT_COLOR_MAP_SIZE_KEY ));
982
+
983
+ return new Gradient (colors , startPoints , colorMapSize );
984
+ }
985
+
857
986
private static List <List <LatLng >> toHoles (Object o ) {
858
987
final List <?> data = toList (o );
859
988
final List <List <LatLng >> holes = new ArrayList <>(data .size ());
0 commit comments