Skip to content

Commit

Permalink
Set z-order of map features
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Sep 12, 2023
1 parent 30fd1a1 commit f767059
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ fun LatLng.toCoordinates(): Coordinates = Coordinates(latitude, longitude)

fun Coordinates.toLatLng(): LatLng = LatLng(lat, lng)

fun List<Coordinates>.toLatLngList(): List<LatLng> = map { it.toLatLng() }

fun Location.toCoordinates(): Coordinates = Coordinates(latitude, longitude)
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class FeatureClusterRenderer(

/** Sets appropriate styling for clustered markers prior to rendering. */
override fun onBeforeClusterItemRendered(item: FeatureClusterItem, markerOptions: MarkerOptions) {
markerOptions.icon(getMarkerIcon(item.isSelected(), item.style.color))
with(markerOptions) {
icon(getMarkerIcon(item.isSelected(), item.style.color))
zIndex(MARKER_Z)
}
}

override fun onClusterItemUpdated(item: FeatureClusterItem, marker: Marker) {
Expand All @@ -74,7 +77,7 @@ class FeatureClusterRenderer(
* Creates the marker with a label indicating the number of jobs with submissions over the total
* number of jobs in the cluster.
*/
private fun createMarker(cluster: Cluster<FeatureClusterItem>): BitmapDescriptor {
private fun createMarkerIcon(cluster: Cluster<FeatureClusterItem>): BitmapDescriptor {
val totalWithData = cluster.items.count { it.feature.tag.flag }
return markerIconFactory.getClusterIcon(
markerColor,
Expand All @@ -89,12 +92,15 @@ class FeatureClusterRenderer(
) {
super.onBeforeClusterRendered(cluster, markerOptions)
Timber.d("MARKER_RENDER: onBeforeClusterRendered")
markerOptions.icon(createMarker(cluster))
with(markerOptions) {
icon(createMarkerIcon(cluster))
zIndex(CLUSTER_Z)
}
}

override fun onClusterUpdated(cluster: Cluster<FeatureClusterItem>, marker: Marker) {
super.onClusterUpdated(cluster, marker)
marker.setIcon(createMarker(cluster))
marker.setIcon(createMarkerIcon(cluster))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toPersistentList
import timber.log.Timber

const val TILE_OVERLAY_Z = 0f
const val POLYGON_Z = 1f
const val CLUSTER_Z = 2f
const val MARKER_Z = 3f
/**
* Customization of Google Maps API Fragment that automatically adjusts the Google watermark based
* on window insets.
Expand Down Expand Up @@ -373,7 +377,7 @@ class GoogleMapsFragment : Hilt_GoogleMapsFragment(), Map {
}

private fun addTileOverlay(tileProvider: TileProvider) {
val tileOverlay = map.addTileOverlay(TileOverlayOptions().tileProvider(tileProvider))
val tileOverlay = map.addTileOverlay(TileOverlayOptions().tileProvider(tileProvider).zIndex(TILE_OVERLAY_Z))
if (tileOverlay == null) {
Timber.e("Unable to add tile overlay $tileProvider")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import com.google.android.ground.model.geometry.Polygon
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.ui.map.Feature
import com.google.android.ground.ui.map.FeatureType
import com.google.android.ground.ui.map.gms.POLYGON_Z
import com.google.android.ground.ui.map.gms.toLatLng
import com.google.android.ground.ui.map.gms.toLatLngList
import timber.log.Timber

class PolygonRenderer(
Expand All @@ -51,21 +53,22 @@ class PolygonRenderer(
Timber.d("Adding polygon $feature")

val options = PolygonOptions()
options.clickable(false)

val shellVertices = polygon.getShellCoordinates().map { it.toLatLng() }
options.addAll(shellVertices)
with(options) {
clickable(false)
addAll(polygon.getShellCoordinates().map { it.toLatLng() })
}

val holes = polygon.holes.map { hole -> hole.coordinates.map { it.toLatLng() } }
holes.forEach { options.addHole(it) }
polygon.holes.forEach { options.addHole(it.coordinates.toLatLngList()) }

val mapsPolygon = map.addPolygon(options)
mapsPolygon.tag = Pair(feature.tag.id, LocationOfInterest::javaClass)
mapsPolygon.strokeWidth = strokeWidth
mapsPolygon.fillColor = fillColor
mapsPolygon.strokeColor = strokeColor
mapsPolygon.strokeJointType = JointType.ROUND

with(mapsPolygon) {
tag = Pair(feature.tag.id, LocationOfInterest::javaClass)
strokeWidth = strokeWidth
fillColor = fillColor
strokeColor = strokeColor
strokeJointType = JointType.ROUND
zIndex = POLYGON_Z
}
polygons.getOrPut(feature) { mutableListOf() }.add(mapsPolygon)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.google.android.ground.model.geometry.Coordinates
import com.google.android.ground.model.geometry.LineString
import com.google.android.ground.model.geometry.LinearRing
import com.google.android.ground.ui.map.Feature
import com.google.android.ground.ui.map.gms.toLatLng
import com.google.android.ground.ui.map.gms.toLatLngList
import timber.log.Timber

class PolylineRenderer(
Expand All @@ -51,18 +51,19 @@ class PolylineRenderer(
Timber.d("Adding Polyline $feature")

val options = PolylineOptions()
options.clickable(false)

val shellVertices = points.map { it.toLatLng() }
options.addAll(shellVertices)

val polyline: Polyline = map.addPolyline(options)
polyline.tag = feature.tag
polyline.startCap = customCap
polyline.endCap = customCap
polyline.width = strokeWidth
polyline.color = strokeColor
polyline.jointType = JointType.ROUND
with(options) {
clickable(false)
addAll(points.toLatLngList())
}
val polyline = map.addPolyline(options)
with(polyline) {
tag = feature.tag
startCap = customCap
endCap = customCap
width = strokeWidth
color = strokeColor
jointType = JointType.ROUND
}

polylines.getOrPut(feature) { mutableListOf() }.add(polyline)
}
Expand Down

0 comments on commit f767059

Please sign in to comment.