Last active
July 12, 2024 12:12
-
-
Save samigehi/f2c6199a1e6eaa1cab0514c99a0e65c4 to your computer and use it in GitHub Desktop.
Android / iOS Native MapView using Compose Multiplatform
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.samigehi.map | |
@Composable | |
actual GetNativeMapView(modifier: Modifier) : NativeMapView { | |
// TODO not implemenmted | |
} |
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.samigehi.map | |
import platform.MapKit.MKMapView | |
import androidx.compose.ui.Modifier | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.interop.UIKitView | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.runtime.saveable.rememberSaveable | |
@Composable | |
actual GetNativeMapView(modifier: Modifier) : NativeMapView { | |
val mkMapView = rememberSaveable { | |
MKMapView().apply { | |
delegate = MapViewDelegate() | |
zoomEnabled = true | |
showsUserLocation = true | |
setRegion( | |
MKCoordinateRegionMakeWithDistance( | |
centerCoordinate = centerCoordinate, | |
latitudinalMeters = 5000.0, | |
longitudinalMeters = 5000.0 | |
), true | |
) | |
} | |
} | |
val mapView = rememberSaveable { NativeMapView(mkMapView) } | |
mapView.initializeMap() | |
UIKitView(factory = { | |
mkMapView | |
}, modifier = modifier.fillMaxSize(), update = { view -> | |
mapView.onUpdate() | |
}, onRelease = { mapView.onRelease() }) | |
return mapView | |
} | |
class NativeMapVIew(val map : MKMapView?) { | |
fun initializeMap(){ | |
} | |
override fun zoomIn() { | |
// map.zoom in | |
} | |
override fun zoomOut() { | |
// map.zoom out | |
} | |
override fun addMarkers(list: List<Marker>) { | |
// map add markers / annotations | |
map.addAnnotations(list.map { | |
MKPointAnnotation( | |
coordinate = CLLocationCoordinate2DMake( | |
it.latitude, it.longitude | |
), | |
title = it.title, | |
//subtitle = it.subtitle, | |
// image = UIImage(""), | |
) | |
}) | |
} | |
} | |
// MKMapView delegate | |
class MapViewDelegate() : NSObject(), MKMapViewDelegateProtocol { | |
override fun mapView( | |
mapView: MKMapView, | |
annotationView: MKAnnotationView, | |
didChangeDragState: MKAnnotationViewDragState, | |
fromOldState: MKAnnotationViewDragState, | |
) { | |
// on marker drag | |
} | |
override fun mapView( | |
mapView: MKMapView, | |
viewForAnnotation: MKAnnotationProtocol, | |
): MKAnnotationView? { | |
//return MKAnnotationView for coords (add custom icons to marker) | |
return null | |
} | |
override fun mapView(mapView: MKMapView, didSelectAnnotationView: MKAnnotationView) { | |
} | |
} | |
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.samigehi.map | |
interface NativeMapView { | |
fun zoomIn() | |
fun zoomOut() | |
fun addMarkers(list: List<Marker>) | |
} | |
data class Marker(val latitude: Double, val longitude : Double, val icon: Any? = null, val title: String = "") | |
@Composable | |
expect GetNativeMapView(modifier: Modifier) : NativeMapView |
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
//Example | |
@Composable | |
fun NativeMapView(modifier: Modifier) { | |
Box(modifier.background(Color.Transparent)){ | |
val mapView = GetNativeMapView(modifier) | |
. | |
. | |
. | |
. | |
mapView.addMarkers(getListOfMarkers()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment