Created
March 13, 2021 00:06
-
-
Save FStranieri/23b6a733e120594f087b8f45c5137fda to your computer and use it in GitHub Desktop.
SupportMapFragment with Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fs.composesample | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.activity.compose.setContent | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.viewinterop.AndroidView | |
import com.huawei.hms.maps.* | |
import com.huawei.hms.maps.model.LatLng | |
import com.huawei.hms.maps.model.Marker | |
import com.huawei.hms.maps.model.MarkerOptions | |
class MainActivity : AppCompatActivity(), OnMapReadyCallback { | |
private var testMarker: Marker? = null | |
private val testMarkerOptions: MarkerOptions = MarkerOptions() | |
.position(LatLng(45.4781878, 9.1684945)) | |
.title("Hello Compose!") | |
.snippet("MapView with Compose!") | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
BuildMap(this) | |
} | |
} | |
@Composable | |
private fun BuildMap(onMapReadyCallback: OnMapReadyCallback){ | |
// Adds view to Compose | |
AndroidView( | |
modifier = Modifier.fillMaxSize(), // Occupy the max size in the Compose UI tree | |
factory = { context -> | |
layoutInflater.inflate(R.layout.map_fragment, null, false) | |
}, | |
update = { view -> | |
(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).apply { | |
getMapAsync(onMapReadyCallback) | |
} | |
} | |
) | |
} | |
override fun onMapReady(map: HuaweiMap?) { | |
testMarker?.remove() | |
testMarker = map?.addMarker(testMarkerOptions) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment