48 lines
1.5 KiB
Swift
48 lines
1.5 KiB
Swift
//
|
|
// LocationMapView.swift
|
|
// app
|
|
//
|
|
// Created by Tobias Stottrop on 30.01.20.
|
|
// Copyright © 2020 Marcus Handte. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import MapKit
|
|
import MowestaApi
|
|
|
|
struct LocationMapView: UIViewRepresentable {
|
|
@Binding var centerCoordinate: City
|
|
|
|
func makeUIView(context: Context) -> MKMapView {
|
|
MKMapView(frame: .zero)
|
|
}
|
|
|
|
func updateUIView(_ view: MKMapView, context: Context) {
|
|
// create coordinate for pin
|
|
let coordinate = CLLocationCoordinate2D(
|
|
latitude: centerCoordinate.coordinate.latitude, longitude: centerCoordinate.coordinate.longitude)
|
|
|
|
// create pin with city-name
|
|
let annotation = MKPointAnnotation()
|
|
annotation.title = "\(centerCoordinate.getName())"
|
|
annotation.subtitle = "\(centerCoordinate.getSubName())"
|
|
annotation.coordinate = coordinate
|
|
|
|
// create a span to be shown by a region
|
|
let span = MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)
|
|
|
|
// alter latitude that pin appears in upper third
|
|
// coordinate.latitude -= view.region.span.latitudeDelta * 0.2;
|
|
|
|
let region = MKCoordinateRegion(center: coordinate, span: span)
|
|
view.setRegion(region, animated: true)
|
|
view.addAnnotation(annotation)
|
|
}
|
|
}
|
|
|
|
struct LocationMapView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
LocationMapView(centerCoordinate: .constant(essen))
|
|
}
|
|
}
|