Skip to content

Commit

Permalink
Merge pull request #11 from TheMelody/dev_1.0.5
Browse files Browse the repository at this point in the history
合并v1.0.5
  • Loading branch information
TheMelody authored Aug 26, 2023
2 parents d8ed2f7 + d71765f commit 1f5cde3
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 169 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ jobs:
artifactErrorsFailBuild: true
body: |
### 功能更新
- 处理MapView内存泄露问题,onDispose不可靠,Lifecycle.Event.ON_DESTROY中也要做地图销毁处理
- 处理高德地图内存泄露问题
- 百度地图同高德地图在MapApplier中移除对MapView的引用
- 升级Compose版本和JDK版本
token: ${{ secrets.github_token }}
commit: master
# tag名称
Expand Down
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
OmniMap Compose 🗺
===============
<a href="https://github.com/TheMelody/OmniMap-Compose/blob/main/LICENSE"><img alt="LICENSE" src="https://img.shields.io/github/license/TheMelody/OmniMap-Compose"/></a> <img alt="issues" src="https://img.shields.io/github/issues/TheMelody/OmniMap-Compose?color=important"/> <img alt="forks" src="https://img.shields.io/github/forks/TheMelody/OmniMap-Compose?color=blueviolet"/> <img alt="stars" src="https://img.shields.io/github/stars/TheMelody/OmniMap-Compose?color=success"/> <a href="https://juejin.cn/user/8451824316670/posts"><img alt="稀土掘金" src="https://img.shields.io/badge/%E7%A8%80%E5%9C%9F%E6%8E%98%E9%87%91-392-green?labelColor=%231e80FF&color=black" ></a> <a href="https://www.zhihu.com/people/fq_halifax"><img src="https://img.shields.io/badge/dynamic/json?color=282c34&amp;labelColor=0084ff&amp;label=%E7%9F%A5%E4%B9%8E%E5%85%B3%E6%B3%A8&amp;query=%24.data.totalSubs&amp;url=https%3A%2F%2Fapi.spencerwoo.com%2Fsubstats%2F%3Fsource%3Dzhihu%26queryKey%3Dfq_halifax&amp;longCache=true" alt="知乎"></a> <a href="https://blog.csdn.net/logicsboy"><img src="https://img.shields.io/badge/CSDN-3k+-red?labelColor=%231e80FF&color=black" alt="CSDN"></a>
<a href="https://github.com/TheMelody/OmniMap-Compose/blob/main/LICENSE"><img alt="LICENSE" src="https://img.shields.io/github/license/TheMelody/OmniMap-Compose"/></a> <img alt="issues" src="https://img.shields.io/github/issues/TheMelody/OmniMap-Compose?color=important"/> <img alt="forks" src="https://img.shields.io/github/forks/TheMelody/OmniMap-Compose?color=blueviolet"/> <img alt="stars" src="https://img.shields.io/github/stars/TheMelody/OmniMap-Compose?color=success"/> <a href="https://juejin.cn/user/8451824316670/posts"><img alt="稀土掘金" src="https://img.shields.io/badge/%E7%A8%80%E5%9C%9F%E6%8E%98%E9%87%91-432-green?labelColor=%231e80FF&color=black" ></a> <a href="https://www.zhihu.com/people/fq_halifax"><img src="https://img.shields.io/badge/dynamic/json?color=282c34&amp;labelColor=0084ff&amp;label=%E7%9F%A5%E4%B9%8E&amp;query=%24.data.totalSubs&amp;url=https%3A%2F%2Fapi.spencerwoo.com%2Fsubstats%2F%3Fsource%3Dzhihu%26queryKey%3Dfq_halifax&amp;longCache=true" alt="知乎"></a> <a href="https://blog.csdn.net/logicsboy"><img src="https://img.shields.io/badge/CSDN-3k+-red?labelColor=%231e80FF&color=black" alt="CSDN"></a>

**Compose一键集成5大地图神器**

Expand All @@ -10,13 +10,13 @@ OmniMap Compose 🗺
-------
<table>
<tr>
<td>gd_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/gd_compose?versionPrefix=1.0.4"></td>
<td>gd_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/gd_compose?versionPrefix=1.0.5"></td>
</tr>
<tr>
<td>tencent_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/tencent_compose?versionPrefix=1.0.4"></td>
<td>tencent_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/tencent_compose?versionPrefix=1.0.5"></td>
</tr>
<tr>
<td>baidu_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/baidu_compose?versionPrefix=1.0.4"></td>
<td>baidu_compose</td><td><img alt="Maven Central" src="https://img.shields.io/maven-central/v/io.github.TheMelody/baidu_compose?versionPrefix=1.0.5"></td>
</tr>
</table>

Expand Down
14 changes: 8 additions & 6 deletions baidu-map-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
composeOptions {
kotlinCompilerExtensionVersion "1.3.2"
composeOptions {
kotlinCompilerExtensionVersion "1.3.2"
kotlinCompilerVersion "1.7.10"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
}

lint {
checkReleaseBuilds false
abortOnError false
}
}

dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
// 地图组件
api 'com.baidu.lbsyun:BaiduMapSDK_Map:7.5.8'
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ fun BDMap(
val mapView = remember {
MapView(context, bdMapOptionsFactory())
}
AndroidView(modifier = modifier, factory = { mapView })
AndroidView(modifier = modifier, factory = { mapView }, onRelease = {
runCatching { it.onDestroy() }
it.removeAllViews()
})
MapLifecycle(mapView)
val mapClickListeners = remember { MapClickListeners() }.also {
it.onMapLoaded = onMapLoaded
Expand All @@ -111,6 +114,7 @@ fun BDMap(
disposingComposition {
mapView.newComposition(parentComposition) {
MapUpdater(
mapView = mapView,
mapUiSettings = currentUiSettings,
clickListeners = mapClickListeners,
locationSource = currentLocationSource,
Expand Down Expand Up @@ -138,7 +142,7 @@ private suspend inline fun MapView.newComposition(
): Composition {
val map = awaitMap()
return Composition(
MapApplier(map, this), parent
MapApplier(map, this.context.applicationContext), parent
).apply {
setContent(content)
}
Expand All @@ -161,7 +165,7 @@ private fun MapLifecycle(mapView: MapView) {
DisposableEffect(mapView) {
onDispose {
// fix memory leak
mapView.onDestroy()
runCatching { mapView.onDestroy() }
mapView.removeAllViews()
}
}
Expand All @@ -180,11 +184,6 @@ private fun MapView.lifecycleObserver(previousState: MutableState<Lifecycle.Even
}
Lifecycle.Event.ON_RESUME -> this.onResume()
Lifecycle.Event.ON_PAUSE -> this.onPause()
Lifecycle.Event.ON_DESTROY -> {
// fix memory leak
this.onDestroy()
this.removeAllViews()
}
else -> { /* ignore */ }
}
previousState.value = event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.melody.map.baidu_compose

import android.content.Context
import androidx.compose.runtime.*
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapView
Expand Down Expand Up @@ -51,11 +52,11 @@ private object MapNodeRoot : MapNode

internal class MapApplier(
val map: BaiduMap,
val mapView: MapView,
mapContext: Context,
) : AbstractApplier<MapNode>(MapNodeRoot) {
private val decorations = mutableListOf<MapNode>()
private var enableMultipleInfoWindow: Boolean = false
private val infoWindowAdapter = ComposeInfoWindowAdapter(mapView)
private val infoWindowAdapter = ComposeInfoWindowAdapter(mapContext)

init {
attachClickListeners()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener
import com.baidu.mapapi.map.MapPoi
import com.baidu.mapapi.map.MapStatus
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.melody.map.baidu_compose.model.BDCameraPosition
Expand Down Expand Up @@ -118,6 +119,7 @@ internal class MapPropertiesNode(
@Suppress("NOTHING_TO_INLINE")
@Composable
internal inline fun MapUpdater(
mapView: MapView,
mapUiSettings: MapUiSettings,
clickListeners: MapClickListeners,
locationSource: MyLocationData?,
Expand All @@ -126,7 +128,6 @@ internal inline fun MapUpdater(
) {
val mapApplier = currentComposer.applier as MapApplier
val map = mapApplier.map
val mapView = mapApplier.mapView
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
ComposeNode<MapPropertiesNode, MapApplier>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@

package com.melody.map.baidu_compose.adapter

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionContext
import androidx.compose.ui.platform.ComposeView
import com.baidu.mapapi.map.InfoWindow
import com.baidu.mapapi.map.MapView
import com.melody.map.baidu_compose.R
import com.melody.map.baidu_compose.extensions.getInfoWindowYOffset
import com.melody.map.baidu_compose.extensions.getSnippetExt
Expand All @@ -43,18 +44,7 @@ import com.melody.map.baidu_compose.overlay.ClusterOverlayNode
import com.melody.map.baidu_compose.overlay.MarkerNode
import com.melody.map.baidu_compose.utils.clustering.ClusterItem

internal class ComposeInfoWindowAdapter(private val mapView: MapView) {

private val infoWindowView: ComposeView
get() = ComposeView(mapView.context).apply {
mapView.addView(
this,
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
)
}
internal class ComposeInfoWindowAdapter(private val mapContext: Context) {

fun getInfoContents(markerNode: MarkerNode): InfoWindow {
val infoContent = markerNode.infoContent
Expand All @@ -63,7 +53,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
val snippet = markerNode.marker.getSnippetExt()
getDefaultInfoContent(title, snippet)
} else {
infoWindowView.applyAndRemove(false, markerNode.compositionContext) {
applyAndRemove(false, markerNode.compositionContext) {
infoContent(markerNode.marker)
}
}
Expand All @@ -76,7 +66,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {

fun getInfoWindow(markerNode: MarkerNode): InfoWindow {
val infoWindow = markerNode.infoWindow!!
val view = infoWindowView.applyAndRemove(true, markerNode.compositionContext) {
val view = applyAndRemove(true, markerNode.compositionContext) {
infoWindow(markerNode.marker)
}
return InfoWindow(
Expand All @@ -87,7 +77,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
}

fun getInfoWindow(clusterItem: ClusterItem, clusterItemNode: ClusterOverlayNode): InfoWindow {
val view = infoWindowView.applyAndRemove(true, clusterItemNode.compositionContext) {
val view = applyAndRemove(true, clusterItemNode.compositionContext) {
clusterItemNode.onClusterItemInfoWindow?.invoke(clusterItem)
}
return InfoWindow(
Expand All @@ -97,30 +87,38 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
)
}

private fun ComposeView.applyAndRemove(
private fun applyAndRemove(
fromInfoWindow: Boolean,
parentContext: CompositionContext,
content: @Composable () -> Unit
): ComposeView {
val result = this.apply {
setParentCompositionContext(parentContext)
setContent {
): ViewGroup{
return FrameLayout(mapContext).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
if (fromInfoWindow) {
// 去除地图默认气泡背景, 如果是InfoContent,则只定制内容,不修改窗口背景和样式
setBackgroundColor(Color.TRANSPARENT)
if (!fromInfoWindow) {
setBackgroundResource(R.drawable.infowindow_bg)
}
content.invoke()
}else {
setBackgroundResource(R.drawable.infowindow_bg)
}
addView(
ComposeView(mapContext).apply {
setParentCompositionContext(parentContext)
setContent {
content.invoke()
}
}
)
}
(this.parent as? MapView)?.removeView(this)
return result
}

/**
* 针对普通的Marker
*/
private fun getDefaultInfoContent(title: String?,snippet: String?): LinearLayout {
return LinearLayout(mapView.context).apply {
return LinearLayout(mapContext).apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
Expand All @@ -129,7 +127,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
orientation = LinearLayout.VERTICAL
if (title?.isNotBlank() == true) {
addView(
TextView(mapView.context).apply {
TextView(mapContext).apply {
gravity = Gravity.CENTER
setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
setTextColor(Color.BLACK)
Expand All @@ -138,7 +136,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
)
}
addView(
Space(mapView.context).apply {
Space(mapContext).apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
Expand All @@ -148,7 +146,7 @@ internal class ComposeInfoWindowAdapter(private val mapView: MapView) {
)
if (snippet?.isNotBlank() == true) {
addView(
TextView(mapView.context).apply {
TextView(mapContext).apply {
gravity = Gravity.CENTER
setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
setTextColor(Color.BLACK)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext {
compose_version = '1.3.3'
compose_version = '1.5.0-beta03'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
Expand Down
14 changes: 8 additions & 6 deletions gd-map-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
composeOptions {
kotlinCompilerExtensionVersion "1.3.2"
composeOptions {
kotlinCompilerExtensionVersion "1.3.2"
kotlinCompilerVersion "1.7.10"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
}

lint {
checkReleaseBuilds false
abortOnError false
}
}

dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'

// 高德地图3d
api 'com.amap.api:3dmap:9.7.0'
Expand Down
Loading

0 comments on commit 1f5cde3

Please sign in to comment.