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
Expand Up @@ -198,6 +198,7 @@ internal data class RenderedGeoJson(
internal fun GoogleMap.renderGeoJsonLayer(
layerData: GeoJsonLayer,
clusterSettings: ClusterSettings,
onMarkerClick: ((Marker) -> Unit)?,
): RenderedGeoJson? {
val json =
runCatching { JSONObject(layerData.geoJson) }
Expand All @@ -213,42 +214,52 @@ internal fun GoogleMap.renderGeoJsonLayer(

if (clusterSettings.enabled) {
for (feature in layer.features) {
if (feature.geometry !is GeoJsonPoint) continue

val point = feature.geometry as GeoJsonPoint

val title = feature.getProperty("title")
val snippet = feature.getProperty("snippet")

val anchor = feature.parseGeoJsonAnchor()
val draggable = feature.getProperty("draggable")?.toBoolean() ?: false
val zIndex = feature.getProperty("zIndex")?.toFloatOrNull()

val marker =
Marker(
coordinates =
Coordinates(point.coordinates.latitude, point.coordinates.longitude),
title = title,
androidMarkerOptions =
AndroidMarkerOptions(
snippet = snippet,
anchor = anchor,
draggable = draggable,
zIndex = zIndex,
),
)
extractedMarkers.add(marker)
if (feature.geometry is GeoJsonPoint) {
val point = feature.geometry as GeoJsonPoint
val marker = point.toMarker(feature)

extractedMarkers.add(marker)

val hiddenStyle = GeoJsonPointStyle()
hiddenStyle.isVisible = false
feature.pointStyle = hiddenStyle
val hiddenStyle = GeoJsonPointStyle()
hiddenStyle.isVisible = false
feature.pointStyle = hiddenStyle
}
}
} else {
layer.setOnFeatureClickListener { feature ->
if (feature.geometry is GeoJsonPoint) {
val point = feature.geometry as GeoJsonPoint
val marker = point.toMarker(feature as GeoJsonFeature)
onMarkerClick?.invoke(marker)
}
}
}
layer.addLayerToMap()

return RenderedGeoJson(layer, extractedMarkers)
}

private fun GeoJsonPoint.toMarker(feature: GeoJsonFeature): Marker {
val title = feature.getProperty("title")
val snippet = feature.getProperty("snippet")

val anchor = feature.parseGeoJsonAnchor()
val draggable = feature.getProperty("draggable")?.toBoolean() == true
val zIndex = feature.getProperty("zIndex")?.toFloatOrNull()

return Marker(
coordinates = Coordinates(coordinates.latitude, coordinates.longitude),
title = title,
androidMarkerOptions =
AndroidMarkerOptions(
snippet = snippet,
anchor = anchor,
draggable = draggable,
zIndex = zIndex,
),
)
}

private fun GeoJsonFeature.parseGeoJsonAnchor(): GoogleMapsAnchor? {
val anchorStr = getProperty("anchor")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public actual fun Map(

androidGeoJsonLayers[index]?.removeLayerFromMap()

map.renderGeoJsonLayer(geo, clusterSettings)?.let {
map.renderGeoJsonLayer(geo, clusterSettings, onMarkerClick)?.let {
androidGeoJsonLayers = androidGeoJsonLayers + (index to it.layer)
geoJsonExtractedMarkers =
geoJsonExtractedMarkers + (index to it.extractedMarkers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,6 @@ internal fun MKMapView.updateRenderedGeoJsonLayers(
rendered.pointStyles.forEach { (pt, s) -> geoJsonPointStyles[pt] = s }

rendered.overlays.forEach(this::addOverlay)
rendered.annotations.forEach(this::addAnnotation)

allExtractedMarkers.addAll(rendered.extractedMarkers)

this.reapplyCorePolylineStyles(polylineStyles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public data class AppleMapsGeoJsonPointStyle(val visible: Boolean = true)
*/
public class MKGeoJsonRenderedLayer(
internal val overlays: List<MKOverlayProtocol>,
internal val annotations: List<MKAnnotationProtocol>,
internal val extractedMarkers: List<Marker>,
internal val polylineStyles: Map<MKOverlayProtocol, AppleMapsGeoJsonLineStyle> = emptyMap(),
internal val polygonStyles: Map<MKOverlayProtocol, AppleMapsGeoJsonPolygonStyle> = emptyMap(),
Expand All @@ -56,7 +55,6 @@ public class MKGeoJsonRenderedLayer(
@OptIn(ExperimentalForeignApi::class)
public fun clear(from: MKMapView) {
if (overlays.isNotEmpty()) from.removeOverlays(overlays)
if (annotations.isNotEmpty()) from.removeAnnotations(annotations)
}
}

Expand All @@ -76,7 +74,6 @@ public fun MKMapView.renderGeoJson(
val decoder = MKGeoJSONDecoder()
val objects = decoder.geoJSONObjectsWithData(data, error = null) ?: return null

val annotations = mutableListOf<MKAnnotationProtocol>()
val overlays = mutableListOf<MKOverlayProtocol>()
val extractedMarkers = mutableListOf<Marker>()

Expand All @@ -89,7 +86,6 @@ public fun MKMapView.renderGeoJson(
obj = obj,
mapView = this,
overlays = overlays,
annotations = annotations,
extractedMarkers = extractedMarkers,
polylineStyles = polylineStyles,
polygonStyles = polygonStyles,
Expand All @@ -102,7 +98,6 @@ public fun MKMapView.renderGeoJson(

return MKGeoJsonRenderedLayer(
overlays = overlays,
annotations = annotations,
extractedMarkers = extractedMarkers,
polylineStyles = polylineStyles,
polygonStyles = polygonStyles,
Expand All @@ -117,7 +112,6 @@ public fun MKMapView.renderGeoJson(
* @param obj GeoJSON object (feature, geometry, or array of geometries).
* @param mapView Target map view (passed for API parity if needed).
* @param overlays Destination list for overlays.
* @param annotations Destination list for annotations.
* @param polylineStyles Destination map for polyline styles.
* @param polygonStyles Destination map for polygon styles.
* @param pointStyles Destination map for point styles.
Expand All @@ -129,7 +123,6 @@ private fun collectAndAdd(
obj: Any?,
mapView: MKMapView,
overlays: MutableList<MKOverlayProtocol>,
annotations: MutableList<MKAnnotationProtocol>,
extractedMarkers: MutableList<Marker>,
polylineStyles: MutableMap<MKOverlayProtocol, AppleMapsGeoJsonLineStyle>,
polygonStyles: MutableMap<MKOverlayProtocol, AppleMapsGeoJsonPolygonStyle>,
Expand All @@ -146,7 +139,6 @@ private fun collectAndAdd(
g,
mapView,
overlays,
annotations,
extractedMarkers,
polylineStyles,
polygonStyles,
Expand All @@ -168,29 +160,11 @@ private fun collectAndAdd(
polylineStyles[obj] = buildLineStyle(defaults, featureProps)
}
is MKPointAnnotation -> {
if (clusterSettings.enabled) {
val coordinates = obj.coordinate.useContents { Coordinates(latitude, longitude) }
val title = featureProps?.string("title")
val coordinates = obj.coordinate.useContents { Coordinates(latitude, longitude) }
val title = featureProps?.string("title")

val marker = Marker(coordinates = coordinates, title = title)
extractedMarkers.add(marker)
} else {
val title =
featureProps?.string("title")
?: featureProps?.string("name")
?: defaults?.pointStyle?.pointTitle
val subtitle =
featureProps?.string("snippet")
?: featureProps?.string("description")
?: defaults?.pointStyle?.snippet
if (title != null) obj.setTitle(title)
if (subtitle != null) obj.setSubtitle(subtitle)

val style = buildPointStyle(defaults, featureProps)
pointStyles[obj] = style

annotations += obj
}
val marker = Marker(coordinates = coordinates, title = title)
extractedMarkers.add(marker)
}
is NSArray -> {
val n = obj.count.toInt()
Expand All @@ -200,7 +174,6 @@ private fun collectAndAdd(
any,
mapView,
overlays,
annotations,
extractedMarkers,
polylineStyles,
polygonStyles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import cocoapods.Google_Maps_iOS_Utils.GMULineString
import cocoapods.Google_Maps_iOS_Utils.GMUPoint
import cocoapods.Google_Maps_iOS_Utils.GMUPolygon
import cocoapods.Google_Maps_iOS_Utils.GMUStyle
import com.swmansion.kmpmaps.core.ClusterSettings
import com.swmansion.kmpmaps.core.Coordinates
import com.swmansion.kmpmaps.core.GeoJsonLayer
import com.swmansion.kmpmaps.core.Marker
Expand Down Expand Up @@ -40,7 +39,7 @@ internal class GeoJsonRendererManager {
renderers = emptyMap()
}

fun render(layers: List<GeoJsonLayer>, clusterSettings: ClusterSettings): List<Marker> {
fun render(layers: List<GeoJsonLayer>): List<Marker> {
val view = mapView ?: return emptyList()
val allExtractedMarkers = mutableListOf<Marker>()

Expand Down Expand Up @@ -78,7 +77,7 @@ internal class GeoJsonRendererManager {
val dict = feature.properties as? NSDictionary
val geometry = feature.geometry

if (geometry is GMUPoint && clusterSettings.enabled) {
if (geometry is GMUPoint) {
val coordinates =
geometry.coordinate.useContents { Coordinates(latitude, longitude) }
val title = getString(dict, "title")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public actual fun Map(

LaunchedEffect(mapView, geoJsonLayers) {
if (mapView == null) return@LaunchedEffect
geoJsonExtractedMarkers = geoJsonManager.render(geoJsonLayers, clusterSettings)
geoJsonExtractedMarkers = geoJsonManager.render(geoJsonLayers)
}

LaunchedEffect(cameraPosition) {
Expand Down
Loading