Skip to content

Commit 8c1b99b

Browse files
committed
refactor(heatmaps): Improve API clarity and documentation
Addresses feedback to make the heatmap API more intuitive and robust. - Renames `setWeightedData` and `setData` in `HeatmapTileProvider` to `updateData` and `updateLatLngs` respectively. The `update` prefix better communicates that these methods perform expensive operations that rebuild internal state, rather than just setting a property. - The old method names are preserved and marked as `@Deprecated` to provide a smooth migration path for existing users and avoid a breaking change. - Adds a default value of 0.7 to the `opacity` parameter in `Gradient.generateColorMap`. This aligns with the default in `HeatmapTileProvider` and simplifies common use cases. - Annotates `generateColorMap` with `@JvmOverloads` to ensure the new default parameter is exposed correctly to Java clients. - Improves KDoc for all modified methods to be more descriptive, explaining the purpose of the code and the reasoning behind the design choices.
1 parent 3f461e6 commit 8c1b99b

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

library/src/main/java/com/google/maps/android/heatmaps/Gradient.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,20 @@ class Gradient @JvmOverloads constructor(
7979
}
8080

8181
/**
82-
* Generates the color map to use with a provided gradient.
82+
* Generates a color map array from the gradient's colors and start points. This map is a key
83+
* component for rendering the heatmap, where each color corresponds to a different intensity
84+
* level.
8385
*
84-
* @param opacity Overall opacity of entire image: every individual alpha value will be
85-
* multiplied by this opacity.
86-
* @return the generated color map based on the gradient
86+
* The process involves interpolating between the specified colors in the HSV color space to create
87+
* a smooth transition.
88+
*
89+
* @param opacity The overall opacity of the entire color map. Each color's alpha value will be
90+
* multiplied by this factor. The default value is 0.7, chosen for consistency with the
91+
* default opacity of [HeatmapTileProvider].
92+
* @return An integer array representing the color map, where each element is a color integer.
8793
*/
88-
fun generateColorMap(opacity: Double): IntArray {
94+
@JvmOverloads
95+
fun generateColorMap(opacity: Double = 0.7): IntArray {
8996
val colorIntervals = generateColorIntervals()
9097
val colorMap = IntArray(colorMapSize)
9198
var interval = colorIntervals[0]

library/src/main/java/com/google/maps/android/heatmaps/HeatmapTileProvider.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class HeatmapTileProvider private constructor(builder: Builder) : TileProvider {
5555
// Don't compute anything till data is set
5656
kernel = generateKernel(radius, radius / 3.0)
5757
setGradient(gradient)
58-
setWeightedData(data)
58+
updateData(data)
5959
}
6060

6161
/**
@@ -143,7 +143,21 @@ class HeatmapTileProvider private constructor(builder: Builder) : TileProvider {
143143
}
144144
}
145145

146+
@Deprecated("Use updateData(Collection<WeightedLatLng>) instead.", ReplaceWith("updateData(data)"))
146147
fun setWeightedData(data: Collection<WeightedLatLng>) {
148+
updateData(data)
149+
}
150+
151+
/**
152+
* Refreshes the heatmap with a new collection of weighted data points.
153+
*
154+
* This is an expensive operation. It involves rebuilding the quadtree index and recalculating
155+
* the bounds and maximum intensity values for the new dataset. This method should be used when
156+
* the underlying data for the heatmap has changed.
157+
*
158+
* @param data The new collection of [WeightedLatLng] points.
159+
*/
160+
fun updateData(data: Collection<WeightedLatLng>) {
147161
this.data = data
148162
require(this.data.isNotEmpty()) { "No input points." }
149163
this.bounds = getBounds(this.data)
@@ -154,8 +168,22 @@ class HeatmapTileProvider private constructor(builder: Builder) : TileProvider {
154168
this.maxIntensity = getMaxIntensities(this.radius)
155169
}
156170

171+
@Deprecated("Use updateLatLngs(Collection<LatLng>) instead.", ReplaceWith("updateLatLngs(latLngs)"))
157172
fun setData(latLngs: Collection<LatLng>) {
158-
setWeightedData(wrapData(latLngs))
173+
updateLatLngs(latLngs)
174+
}
175+
176+
/**
177+
* Refreshes the heatmap with a new collection of unweighted data points.
178+
* Each point is assigned a default weight of 1.0.
179+
*
180+
* This is a convenience method that wraps the data in [WeightedLatLng] objects before
181+
* calling [updateData].
182+
*
183+
* @param latLngs The new collection of [LatLng] points.
184+
*/
185+
fun updateLatLngs(latLngs: Collection<LatLng>) {
186+
updateData(wrapData(latLngs))
159187
}
160188

161189
fun setGradient(gradient: Gradient) {
@@ -176,7 +204,7 @@ class HeatmapTileProvider private constructor(builder: Builder) : TileProvider {
176204

177205
fun setMaxIntensity(intensity: Double) {
178206
this.customMaxIntensity = intensity
179-
setWeightedData(this.data)
207+
updateData(this.data)
180208
}
181209

182210
override fun getTile(x: Int, y: Int, zoom: Int): Tile {

0 commit comments

Comments
 (0)