Skip to content

Instantly share code, notes, and snippets.

@jordansinger
Created July 7, 2020 00:03
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jordansinger/4113143465810848ba6fc8079cc33831 to your computer and use it in GitHub Desktop.
Save jordansinger/4113143465810848ba6fc8079cc33831 to your computer and use it in GitHub Desktop.
import SwiftUI
import MapKit
import PlaygroundSupport
struct Location {
var title: String
var latitude: Double
var longitude: Double
}
struct ContentView: View {
// add locations here
@State var locations = [
Location(title: "San Francisco", latitude: 37.7749, longitude: -122.4194),
Location(title: "New York", latitude: 40.7128, longitude: -74.0060)
]
var body: some View {
MapView(locations: locations)
}
}
struct MapView: UIViewRepresentable {
@State var locations: [Location]
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: .zero)
// change the map type here
mapView.mapType = .hybridFlyover
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
for location in locations {
// make a pins
let pin = MKPointAnnotation()
// set the coordinates
pin.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
// set the title
pin.title = location.title
// add to map
view.addAnnotation(pin)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment