-
-
Save am-MongoDB/3073226a84bf6deb6b2df7afcb21ca92 to your computer and use it in GitHub Desktop.
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
// | |
// MyMapView.swift | |
// | |
// Created by Andrew Morgan on 22/06/2021. | |
// | |
import MapKit | |
import SwiftUI | |
struct MyAnnotationItem: Identifiable { | |
var coordinate: CLLocationCoordinate2D | |
var color: Color? | |
var tint: Color { color ?? .red } | |
let id = UUID() | |
} | |
struct MyMapView: View { | |
@State private var region: MKCoordinateRegion = MKCoordinateRegion( | |
center: CLLocationCoordinate2D(latitude: MapDefaults.latitude, longitude: MapDefaults.longitude), | |
span: MKCoordinateSpan(latitudeDelta: MapDefaults.zoom, longitudeDelta: MapDefaults.zoom)) | |
let annotationItems = [MyAnnotationItem( | |
coordinate: CLLocationCoordinate2D( | |
latitude: MapDefaults.latitude, | |
longitude: MapDefaults.longitude)), | |
MyAnnotationItem( | |
coordinate: CLLocationCoordinate2D( | |
latitude: 45.8827419, | |
longitude: -1.1932383), | |
color: .yellow), | |
MyAnnotationItem( | |
coordinate: CLLocationCoordinate2D( | |
latitude: 45.915737, | |
longitude: -1.3300991), | |
color: .blue)] | |
private enum MapDefaults { | |
static let latitude = 45.872 | |
static let longitude = -1.248 | |
static let zoom = 0.5 | |
} | |
var body: some View { | |
VStack { | |
Text("lat: \(region.center.latitude), long: \(region.center.longitude). Zoom: \(region.span.latitudeDelta)") | |
.font(.caption) | |
.padding() | |
Map(coordinateRegion: $region, | |
interactionModes: .all, | |
showsUserLocation: true, | |
annotationItems: annotationItems) { item in | |
MapAnnotation(coordinate: item.coordinate) { | |
Image(systemName: "gamecontroller.fill") | |
.foregroundColor(item.tint) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment