Skip to content

Instantly share code, notes, and snippets.

@am-MongoDB
Created June 23, 2021 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-MongoDB/3073226a84bf6deb6b2df7afcb21ca92 to your computer and use it in GitHub Desktop.
Save am-MongoDB/3073226a84bf6deb6b2df7afcb21ca92 to your computer and use it in GitHub Desktop.
//
// 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