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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ dependencies {
implementation "com.google.android.gms:play-services-base:${getExtOrDefault('googlePlayServicesBaseVersion')}"
implementation "com.google.android.gms:play-services-maps:${getExtOrDefault('googlePlayServicesMapsVersion')}"
implementation "com.google.android.gms:play-services-location:${getExtOrDefault('googlePlayServicesLocationVersion')}"
implementation 'com.google.maps.android:android-maps-utils:3.10.0'
implementation "com.google.maps.android:android-maps-utils:${getExtOrDefault('mapsUtilsVersion')}"
implementation 'com.caverock:androidsvg-aar:1.4'
}
3 changes: 2 additions & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ RNGoogleMapsPlus_minSdkVersion=26
RNGoogleMapsPlus_targetSdkVersion=36
RNGoogleMapsPlus_compileSdkVersion=36
RNGoogleMapsPlus_ndkVersion=27.1.12297006
RNGoogleMapsPlus_googlePlayServicesBaseVersion=18.8.0
RNGoogleMapsPlus_googlePlayServicesBaseVersion=18.9.0
RNGoogleMapsPlus_googlePlayServicesMapsVersion=19.2.0
RNGoogleMapsPlus_googlePlayServicesLocationVersion=21.3.0
RNGoogleMapsPlus_mapsUtilsVersion=3.19.0
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Color
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.CircleOptions
import com.rngooglemapsplus.extensions.centerEquals
import com.rngooglemapsplus.extensions.onUi
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toLatLng
Expand All @@ -25,9 +26,7 @@ class MapCircleBuilder {
next: RNCircle,
circle: Circle,
) = onUi {
if (prev.center.latitude != next.center.latitude ||
prev.center.longitude != next.center.longitude
) {
if (!prev.centerEquals(next)) {
circle.center = next.center.toLatLng()
}

Expand Down
105 changes: 51 additions & 54 deletions android/src/main/java/com/rngooglemapsplus/MapMarkerBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.rngooglemapsplus.extensions.anchorEquals
import com.rngooglemapsplus.extensions.coordinatesEquals
import com.rngooglemapsplus.extensions.infoWindowAnchorEquals
import com.rngooglemapsplus.extensions.markerInfoWindowStyleEquals
import com.rngooglemapsplus.extensions.markerStyleEquals
import com.rngooglemapsplus.extensions.onUi
import com.rngooglemapsplus.extensions.styleHash
Expand Down Expand Up @@ -73,7 +77,10 @@ class MapMarkerBuilder(
val height = (svg.documentHeight.takeIf { it > 0 } ?: 128f).toInt()

createBitmap(width, height).apply {
Canvas(this).also(svg::renderToCanvas)
density = context.resources.displayMetrics.densityDpi
Canvas(this).also {
svg.renderToCanvas(it)
}
}
}

Expand All @@ -94,10 +101,12 @@ class MapMarkerBuilder(
val innerSvg = SVG.getFromString(svgText)
val w = innerSvg.documentWidth.takeIf { it > 0 } ?: 128f
val h = innerSvg.documentHeight.takeIf { it > 0 } ?: 128f
val bmp = createBitmap(w.toInt(), h.toInt())
val canvas = Canvas(bmp)
innerSvg.renderToCanvas(canvas)
bmp
createBitmap(w.toInt(), h.toInt()).apply {
density = context.resources.displayMetrics.densityDpi
Canvas(this).also {
innerSvg.renderToCanvas(it)
}
}
} else {
conn.inputStream.use { BitmapFactory.decodeStream(it) }
}
Expand Down Expand Up @@ -169,51 +178,39 @@ class MapMarkerBuilder(
next: RNMarker,
marker: Marker,
) = onUi {
if (prev.coordinate.latitude != next.coordinate.latitude ||
prev.coordinate.longitude != next.coordinate.longitude
) {
if (!prev.coordinatesEquals(next)) {
marker.position = next.coordinate.toLatLng()
}

if (!prev.markerStyleEquals(next)) {
buildIconAsync(marker.id, next) { icon ->
buildIconAsync(next) { icon ->
marker.setIcon(icon)
if (prev.infoWindowAnchor?.x != next.infoWindowAnchor?.x ||
prev.infoWindowAnchor?.y != next.infoWindowAnchor?.y
) {
marker.setInfoWindowAnchor(
(next.infoWindowAnchor?.x ?: 0.5f).toFloat(),
(next.infoWindowAnchor?.y ?: 0f).toFloat(),
)
}

if (prev.anchor?.x != next.anchor?.x ||
prev.anchor?.y != next.anchor?.y
) {
if (!prev.anchorEquals(next)) {
marker.setAnchor(
(next.anchor?.x ?: 0.5f).toFloat(),
(next.anchor?.y ?: 1.0f).toFloat(),
)
}
if (!prev.infoWindowAnchorEquals(next)) {
marker.setInfoWindowAnchor(
(next.infoWindowAnchor?.x ?: 0.5f).toFloat(),
(next.infoWindowAnchor?.y ?: 0f).toFloat(),
)
}
}
} else {
if (prev.infoWindowAnchor?.x != next.infoWindowAnchor?.x ||
prev.infoWindowAnchor?.y != next.infoWindowAnchor?.y
) {
marker.setInfoWindowAnchor(
(next.infoWindowAnchor?.x ?: 0.5f).toFloat(),
(next.infoWindowAnchor?.y ?: 0f).toFloat(),
)
}

if (prev.anchor?.x != next.anchor?.x ||
prev.anchor?.y != next.anchor?.y
) {
if (!prev.anchorEquals(next)) {
marker.setAnchor(
(next.anchor?.x ?: 0.5f).toFloat(),
(next.anchor?.y ?: 1.0f).toFloat(),
)
}
if (!prev.infoWindowAnchorEquals(next)) {
marker.setInfoWindowAnchor(
(next.infoWindowAnchor?.x ?: 0.5f).toFloat(),
(next.infoWindowAnchor?.y ?: 0f).toFloat(),
)
}
}

if (prev.title != next.title) {
Expand Down Expand Up @@ -244,17 +241,16 @@ class MapMarkerBuilder(
marker.zIndex = next.zIndex?.toFloat() ?: 0f
}

if (prev.infoWindowIconSvg != next.infoWindowIconSvg) {
if (!prev.markerInfoWindowStyleEquals(next)) {
marker.tag = MarkerTag(id = next.id, iconSvg = next.infoWindowIconSvg)
}
}

fun buildIconAsync(
id: String,
m: RNMarker,
onReady: (BitmapDescriptor?) -> Unit,
) {
jobsById[id]?.cancel()
jobsById[m.id]?.cancel()

m.iconSvg ?: return onReady(null)

Expand Down Expand Up @@ -283,11 +279,11 @@ class MapMarkerBuilder(
iconCache.evictAll()
} catch (_: Throwable) {
} finally {
jobsById.remove(id)
jobsById.remove(m.id)
}
}

jobsById[id] = job
jobsById[m.id] = job
}

fun cancelIconJob(id: String) {
Expand Down Expand Up @@ -337,27 +333,28 @@ class MapMarkerBuilder(
coroutineContext.ensureActive()
val svg = SVG.getFromString(m.iconSvg.svgString)

coroutineContext.ensureActive()
svg.setDocumentWidth(m.iconSvg.width.dpToPx())
svg.setDocumentHeight(m.iconSvg.height.dpToPx())
val wPx =
m.iconSvg.width
.dpToPx()
.toInt()
val hPx =
m.iconSvg.height
.dpToPx()
.toInt()

coroutineContext.ensureActive()
bmp =
createBitmap(
m.iconSvg.width
.dpToPx()
.toInt(),
m.iconSvg.height
.dpToPx()
.toInt(),
Bitmap.Config.ARGB_8888,
)
svg.setDocumentWidth(wPx.toFloat())
svg.setDocumentHeight(hPx.toFloat())

coroutineContext.ensureActive()
val canvas = Canvas(bmp)
svg.renderToCanvas(canvas)
bmp =
createBitmap(wPx, hPx, Bitmap.Config.ARGB_8888).apply {
density = context.resources.displayMetrics.densityDpi
Canvas(this).also {
svg.renderToCanvas(it)
}
}

coroutineContext.ensureActive()
return bmp
} catch (t: Throwable) {
try {
Expand Down
29 changes: 6 additions & 23 deletions android/src/main/java/com/rngooglemapsplus/MapPolygonBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import android.graphics.Color
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import com.rngooglemapsplus.extensions.coordinatesEquals
import com.rngooglemapsplus.extensions.holesEquals
import com.rngooglemapsplus.extensions.onUi
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toLatLng
import com.rngooglemapsplus.extensions.toMapsPolygonHoles

class MapPolygonBuilder {
fun build(poly: RNPolygon): PolygonOptions =
Expand All @@ -32,32 +35,12 @@ class MapPolygonBuilder {
next: RNPolygon,
poly: Polygon,
) = onUi {
val coordsChanged =
prev.coordinates.size != next.coordinates.size ||
!prev.coordinates.zip(next.coordinates).all { (a, b) ->
a.latitude == b.latitude && a.longitude == b.longitude
}

if (coordsChanged) {
if (!prev.coordinatesEquals(next)) {
poly.points = next.coordinates.map { it.toLatLng() }
}

val prevHoles = prev.holes?.toList() ?: emptyList()
val nextHoles = next.holes?.toList() ?: emptyList()
val holesChanged =
prevHoles.size != nextHoles.size ||
!prevHoles.zip(nextHoles).all { (ha, hb) ->
ha.coordinates.size == hb.coordinates.size &&
ha.coordinates.zip(hb.coordinates).all { (a, b) ->
a.latitude == b.latitude && a.longitude == b.longitude
}
}

if (holesChanged) {
poly.holes =
nextHoles.map { hole ->
hole.coordinates.map { it.toLatLng() }
}
if (!prev.holesEquals(next)) {
poly.holes = next.holes?.toMapsPolygonHoles() ?: emptyList()
}

if (prev.fillColor != next.fillColor) {
Expand Down
51 changes: 12 additions & 39 deletions android/src/main/java/com/rngooglemapsplus/MapPolylineBuilder.kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package com.rngooglemapsplus

import android.graphics.Color
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.ButtCap
import com.google.android.gms.maps.model.Cap
import com.google.android.gms.maps.model.JointType
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.RoundCap
import com.google.android.gms.maps.model.SquareCap
import com.rngooglemapsplus.extensions.coordinatesEquals
import com.rngooglemapsplus.extensions.onUi
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toLatLng
import com.rngooglemapsplus.extensions.toMapJointType
import com.rngooglemapsplus.extensions.toMapLineCap

class MapPolylineBuilder {
fun build(pl: RNPolyline): PolylineOptions =
Expand All @@ -21,10 +19,10 @@ class MapPolylineBuilder {
}
pl.width?.let { width(it.dpToPx()) }
pl.lineCap?.let {
startCap(mapLineCap(it))
endCap(mapLineCap(it))
startCap(it.toMapLineCap())
endCap(it.toMapLineCap())
}
pl.lineJoin?.let { jointType(mapLineJoin(it)) }
pl.lineJoin?.let { jointType(it.toMapJointType()) }
pl.color?.let { color(it.toColor()) }
pl.geodesic?.let { geodesic(it) }
pl.pressable?.let { clickable(it) }
Expand All @@ -36,31 +34,21 @@ class MapPolylineBuilder {
next: RNPolyline,
polyline: Polyline,
) = onUi {
val coordsChanged =
prev.coordinates.size != next.coordinates.size ||
!prev.coordinates.zip(next.coordinates).all { (a, b) ->
a.latitude == b.latitude && a.longitude == b.longitude
}

if (coordsChanged) {
if (!prev.coordinatesEquals(next)) {
polyline.points = next.coordinates.map { it.toLatLng() }
}

if (prev.width != next.width) {
polyline.width = next.width?.dpToPx() ?: 1f
}

val newCap = mapLineCap(next.lineCap ?: RNLineCapType.BUTT)
val prevCap = mapLineCap(prev.lineCap ?: RNLineCapType.BUTT)
if (newCap != prevCap) {
polyline.startCap = newCap
polyline.endCap = newCap
if (prev.lineCap != next.lineCap) {
polyline.startCap = next.lineCap.toMapLineCap()
polyline.endCap = next.lineCap.toMapLineCap()
}

val newJoin = mapLineJoin(next.lineJoin ?: RNLineJoinType.MITER)
val prevJoin = mapLineJoin(prev.lineJoin ?: RNLineJoinType.MITER)
if (newJoin != prevJoin) {
polyline.jointType = newJoin
if (prev.lineJoin != next.lineJoin) {
polyline.jointType = next.lineJoin.toMapJointType()
}

if (prev.color != next.color) {
Expand All @@ -79,19 +67,4 @@ class MapPolylineBuilder {
polyline.zIndex = next.zIndex?.toFloat() ?: 0f
}
}

private fun mapLineCap(type: RNLineCapType?): Cap =
when (type) {
RNLineCapType.ROUND -> RoundCap()
RNLineCapType.SQUARE -> SquareCap()
else -> ButtCap()
}

private fun mapLineJoin(type: RNLineJoinType?): Int =
when (type) {
RNLineJoinType.ROUND -> JointType.ROUND
RNLineJoinType.BEVEL -> JointType.BEVEL
RNLineJoinType.MITER -> JointType.DEFAULT
null -> JointType.DEFAULT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.rngooglemapsplus.extensions.markerEquals
import com.rngooglemapsplus.extensions.polygonEquals
import com.rngooglemapsplus.extensions.polylineEquals
import com.rngooglemapsplus.extensions.toCameraPosition
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toCompressFormat
import com.rngooglemapsplus.extensions.toFileExtension
import com.rngooglemapsplus.extensions.toGoogleMapType
Expand Down Expand Up @@ -48,6 +49,7 @@ class RNGoogleMapsPlusView(
initialProps?.mapId?.let { mapId(it) }
initialProps?.liteMode?.let { liteMode(it) }
initialProps?.camera?.let { camera(it.toCameraPosition(current = null)) }
initialProps?.backgroundColor?.let { backgroundColor(it.toColor()) }
}
view.initMapView(options)
}
Expand Down Expand Up @@ -149,7 +151,7 @@ class RNGoogleMapsPlusView(
val prev = prevById[id]
when {
prev == null ->
markerBuilder.buildIconAsync(id, next) { icon ->
markerBuilder.buildIconAsync(next) { icon ->
view.addMarker(
id,
markerBuilder.build(next, icon),
Expand Down
Loading
Loading