Skip to content
Open
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
Expand Up @@ -16,7 +16,6 @@
package com.google.maps.android.clustering

import android.content.Context
import android.os.AsyncTask
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.google.maps.android.geometry.Bounds
import com.google.maps.android.geometry.Point
import com.google.maps.android.projection.SphericalMercatorProjection
import com.google.maps.android.quadtree.PointQuadTree
import java.util.ArrayList
import java.util.Collections
import java.util.HashMap
import java.util.HashSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.locks.ReadWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.withLock

/**
* Optimistically fetch clusters for adjacent zoom levels, caching them as necessary.
Expand Down Expand Up @@ -107,24 +108,18 @@ class PreCachingAlgorithmDecorator<T : ClusterItem>(
}

private fun getClustersInternal(discreteZoom: Int): Set<Cluster<T>> {
var results: Set<Cluster<T>>?
mCacheLock.readLock().lock()
results = mCache.get(discreteZoom)
mCacheLock.readLock().unlock()
val cached = mCacheLock.readLock().withLock {
mCache.get(discreteZoom)
}
if (cached != null) {
return cached
}

if (results == null) {
mCacheLock.writeLock().lock()
try {
results = mCache.get(discreteZoom)
if (results == null) {
results = algorithm.getClusters(discreteZoom.toFloat())
mCache.put(discreteZoom, results)
}
} finally {
mCacheLock.writeLock().unlock()
return mCacheLock.writeLock().withLock {
mCache.get(discreteZoom) ?: algorithm.getClusters(discreteZoom.toFloat()).also {
mCache.put(discreteZoom, it)
}
}
return results!!
}

private inner class PrecacheRunnable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.google.maps.android.clustering.view
import androidx.annotation.StyleRes
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.ClusterManager.OnClusterClickListener
import com.google.maps.android.clustering.ClusterManager.OnClusterInfoWindowClickListener
import com.google.maps.android.clustering.ClusterManager.OnClusterInfoWindowLongClickListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import java.util.Queue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock
import kotlin.math.abs
Expand Down Expand Up @@ -325,17 +324,19 @@ open class ClusterRendererMultipleItems<T : ClusterItem> @JvmOverloads construct
}
val projection = mMap.projection

var renderTask: RenderTask?
synchronized(this) {
renderTask = mNextClusters
val renderTask = synchronized(this) {
val task = mNextClusters
mNextClusters = null
mViewModificationInProgress = true
task
}

renderTask!!.setCallback { sendEmptyMessage(TASK_FINISHED) }
renderTask!!.setProjection(projection)
renderTask!!.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(renderTask)
renderTask?.let {
it.setCallback { sendEmptyMessage(TASK_FINISHED) }
it.setProjection(projection)
it.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(it)
}
}

fun queue(clusters: Set<Cluster<T>>) {
Expand Down Expand Up @@ -475,14 +476,15 @@ open class ClusterRendererMultipleItems<T : ClusterItem> @JvmOverloads construct
}

for (marker in markersToRemove) {
val onScreen = marker.position?.let { visibleBounds.contains(it) } ?: false
val position = marker.position
val onScreen = position?.let { visibleBounds.contains(it) } ?: false

if (onScreen && mAnimate) {
val point = mSphericalMercatorProjection!!.toPoint(marker.position!!)
val point = mSphericalMercatorProjection!!.toPoint(position)
val closest = findClosestCluster(newClustersOnScreen, point)
if (closest != null) {
val animateTo = mSphericalMercatorProjection!!.toLatLng(closest)
markerModifier.animateThenRemove(marker, marker.position!!, animateTo!!)
markerModifier.animateThenRemove(marker, position, animateTo)
RendererLogger.d("ClusterRenderer", "Animating then removing marker at position: " + marker.position)
} else if (mClusterMarkerCache.mCache.keys
.iterator()
Expand Down Expand Up @@ -1143,7 +1145,7 @@ open class ClusterRendererMultipleItems<T : ClusterItem> @JvmOverloads construct
val markerWithPosition: MarkerWithPosition<T>
if (marker == null) {
RendererLogger.d("ClusterRenderer", "Creating new cluster marker")
val markerOptions = MarkerOptions().position(if (animateFrom == null) cluster.position else animateFrom)
val markerOptions = MarkerOptions().position(animateFrom ?: cluster.position)
onBeforeClusterRendered(cluster, markerOptions)
marker = mClusterManager.clusterMarkerCollection.addMarker(markerOptions)
mClusterMarkerCache.put(cluster, marker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import java.util.Queue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import kotlin.math.abs
import kotlin.math.min
Expand Down Expand Up @@ -269,17 +268,19 @@ open class DefaultAdvancedMarkersClusterRenderer<T : ClusterItem> @JvmOverloads
}
val projection = mMap.projection

var renderTask: RenderTask?
synchronized(this) {
renderTask = mNextClusters
val renderTask = synchronized(this) {
val task = mNextClusters
mNextClusters = null
mViewModificationInProgress = true
task
}

renderTask!!.setCallback { sendEmptyMessage(TASK_FINISHED) }
renderTask!!.setProjection(projection)
renderTask!!.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(renderTask)
renderTask?.let {
it.setCallback { sendEmptyMessage(TASK_FINISHED) }
it.setProjection(projection)
it.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(it)
}
}

fun queue(clusters: Set<Cluster<T>>) {
Expand Down Expand Up @@ -473,7 +474,7 @@ open class DefaultAdvancedMarkersClusterRenderer<T : ClusterItem> @JvmOverloads
val closest = findClosestCluster(newClustersOnScreen, point)
if (closest != null) {
val animateTo = mSphericalMercatorProjection!!.toLatLng(closest)
markerModifier.animateThenRemove(marker, marker.position, animateTo!!)
markerModifier.animateThenRemove(marker, marker.position, animateTo)
} else {
markerModifier.remove(true, marker.marker)
}
Expand Down Expand Up @@ -1004,7 +1005,7 @@ open class DefaultAdvancedMarkersClusterRenderer<T : ClusterItem> @JvmOverloads
if (!shouldRenderAsCluster(cluster)) {
for (item in cluster.items) {
var marker = mMarkerCache[item] as AdvancedMarker?
var markerWithPosition: MarkerWithPosition
val markerWithPosition: MarkerWithPosition
if (marker == null) {
val advancedMarkerOptions = AdvancedMarkerOptions()
if (animateFrom != null) {
Expand All @@ -1016,9 +1017,10 @@ open class DefaultAdvancedMarkersClusterRenderer<T : ClusterItem> @JvmOverloads
}
}
onBeforeClusterItemRendered(item, advancedMarkerOptions)
marker = mClusterManager.markerCollection.addMarker(advancedMarkerOptions) as AdvancedMarker?
markerWithPosition = MarkerWithPosition(marker!!)
mMarkerCache.put(item, marker!!)
val newMarker = mClusterManager.markerCollection.addMarker(advancedMarkerOptions) as AdvancedMarker
marker = newMarker
markerWithPosition = MarkerWithPosition(newMarker)
mMarkerCache.put(item, newMarker)
if (animateFrom != null) {
markerModifier.animate(markerWithPosition, animateFrom, item.position)
}
Expand All @@ -1033,22 +1035,22 @@ open class DefaultAdvancedMarkersClusterRenderer<T : ClusterItem> @JvmOverloads
}

var marker = mClusterMarkerCache[cluster] as AdvancedMarker?
var markerWithPosition: MarkerWithPosition
val markerWithPosition: MarkerWithPosition
if (marker == null) {
val advancedMarkerOptions = AdvancedMarkerOptions().position(if (animateFrom == null) cluster.position else animateFrom)
val advancedMarkerOptions = AdvancedMarkerOptions().position(animateFrom ?: cluster.position)
onBeforeClusterRendered(cluster, advancedMarkerOptions)
val `object` = mClusterManager.clusterMarkerCollection.addMarker(advancedMarkerOptions)
marker = `object` as AdvancedMarker?
mClusterMarkerCache.put(cluster, marker!!)
markerWithPosition = MarkerWithPosition(marker)
val newMarker = mClusterManager.clusterMarkerCollection.addMarker(advancedMarkerOptions) as AdvancedMarker
marker = newMarker
mClusterMarkerCache.put(cluster, newMarker)
markerWithPosition = MarkerWithPosition(newMarker)
if (animateFrom != null) {
markerModifier.animate(markerWithPosition, animateFrom, cluster.position)
}
} else {
markerWithPosition = MarkerWithPosition(marker)
onClusterUpdated(cluster, marker)
}
onClusterRendered(cluster, marker!!)
onClusterRendered(cluster, marker)
newMarkers.add(markerWithPosition)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import java.util.Queue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import kotlin.math.abs
import kotlin.math.min
Expand Down Expand Up @@ -268,17 +267,19 @@ open class DefaultClusterRenderer<T : ClusterItem> @JvmOverloads constructor(
}
val projection = mMap.projection

var renderTask: RenderTask?
synchronized(this) {
renderTask = mNextClusters
val renderTask = synchronized(this) {
val task = mNextClusters
mNextClusters = null
mViewModificationInProgress = true
task
}

renderTask!!.setCallback { sendEmptyMessage(TASK_FINISHED) }
renderTask!!.setProjection(projection)
renderTask!!.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(renderTask)
renderTask?.let {
it.setCallback { sendEmptyMessage(TASK_FINISHED) }
it.setProjection(projection)
it.setMapZoom(mMap.cameraPosition.zoom)
mExecutor.execute(it)
}
}

fun queue(clusters: Set<Cluster<T>>) {
Expand Down Expand Up @@ -472,7 +473,7 @@ open class DefaultClusterRenderer<T : ClusterItem> @JvmOverloads constructor(
val closest = findClosestCluster(newClustersOnScreen, point)
if (closest != null) {
val animateTo = mSphericalMercatorProjection!!.toLatLng(closest)
markerModifier.animateThenRemove(marker, marker.position, animateTo!!)
markerModifier.animateThenRemove(marker, marker.position, animateTo)
} else {
markerModifier.remove(true, marker.marker)
}
Expand Down Expand Up @@ -1013,7 +1014,7 @@ open class DefaultClusterRenderer<T : ClusterItem> @JvmOverloads constructor(
if (!shouldRenderAsCluster(cluster)) {
for (item in cluster.items) {
var marker = mMarkerCache[item]
var markerWithPosition: MarkerWithPosition
val markerWithPosition: MarkerWithPosition
if (marker == null) {
val markerOptions = MarkerOptions()
if (animateFrom != null) {
Expand Down Expand Up @@ -1042,9 +1043,9 @@ open class DefaultClusterRenderer<T : ClusterItem> @JvmOverloads constructor(
}

var marker = mClusterMarkerCache[cluster]
var markerWithPosition: MarkerWithPosition
val markerWithPosition: MarkerWithPosition
if (marker == null) {
val markerOptions = MarkerOptions().position(if (animateFrom == null) cluster.position else animateFrom)
val markerOptions = MarkerOptions().position(animateFrom ?: cluster.position)
onBeforeClusterRendered(cluster, markerOptions)
marker = mClusterManager.clusterMarkerCollection.addMarker(markerOptions)
mClusterMarkerCache.put(cluster, marker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import kotlin.math.*
class SphericalMercatorProjection(
private val worldWidth: Double,
) {
fun toPoint(latLng: LatLng): Point {
fun toPoint(latLng: LatLng): com.google.maps.android.geometry.Point {
val x = latLng.longitude / 360 + .5
val siny = sin(Math.toRadians(latLng.latitude))
val y = 0.5 * ln((1 + siny) / (1 - siny)) / -(2 * PI) + .5

return Point(x * worldWidth, y * worldWidth)
return com.google.maps.android.geometry.Point(x * worldWidth, y * worldWidth)
}

fun toLatLng(point: com.google.maps.android.geometry.Point): LatLng {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ class GoogleMapRenderer(

override fun addLayer(layer: Layer) {
if (layers.add(layer)) {
layer.mapObjects.forEach { renderObject(it) }
for (mapObject in layer.mapObjects) {
renderObject(mapObject)
}
}
}

override fun removeLayer(layer: Layer): Boolean {
if (layers.remove(layer)) {
layer.mapObjects.forEach { removeRenderedObject(it) }
for (mapObject in layer.mapObjects) {
removeRenderedObject(mapObject)
}
return true
}
return false
Expand All @@ -55,8 +59,10 @@ class GoogleMapRenderer(
override fun getLayers(): Collection<Layer> = layers

override fun clear() {
layers.forEach { layer ->
layer.mapObjects.forEach { removeRenderedObject(it) }
for (layer in layers) {
for (mapObject in layer.mapObjects) {
removeRenderedObject(mapObject)
}
}
layers.clear()
}
Expand Down Expand Up @@ -135,7 +141,9 @@ class GoogleMapRenderer(
visible(polygon.isVisible)
zIndex(polygon.zIndex)
strokeJointType(polygon.strokeJointType)
polygon.holes.forEach { addHole(it) }
for (hole in polygon.holes) {
addHole(hole)
}
polygon.strokePattern?.let { strokePattern(it) }
}
val sdkPolygon = map.addPolygon(options)
Expand Down
Loading