Created
May 20, 2021 15:43
-
-
Save VAndrJ/8a0c99ba9487cf22e2b73fd1f2afbd46 to your computer and use it in GitHub Desktop.
Example for https://ru.stackoverflow.com/questions/1283910
This file contains hidden or 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
// | |
// ContentView.swift | |
// TestMapkit | |
// | |
// Created by Volodymyr Andriienko on 20.05.2021. | |
// Copyright © 2021 VAndrJ. All rights reserved. | |
// | |
import SwiftUI | |
import MapKit | |
import CoreLocation | |
struct ContentView: View { | |
@State private var region = MKCoordinateRegion( | |
// Apple Park | |
center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965), | |
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) | |
) | |
@State private var lineCoordinates = [ | |
// Steve Jobs theatre | |
CLLocationCoordinate2D(latitude: 37.330828, longitude: -122.007495), | |
// Caffè Macs | |
CLLocationCoordinate2D(latitude: 37.336083, longitude: -122.007356), | |
// Apple wellness center | |
CLLocationCoordinate2D(latitude: 37.336901, longitude: -122.012345), | |
] | |
var body: some View { | |
MapView( | |
region: region, | |
lineCoordinates: lineCoordinates, | |
onMapTap: { coordinate in | |
// Прописать как добавлять / удалять | |
lineCoordinates.append(coordinate) | |
} | |
) | |
.edgesIgnoringSafeArea(.all) | |
} | |
} | |
class TappableMKMapView: MKMapView { | |
var onMapTap: ((CLLocationCoordinate2D) -> Void)? | |
convenience init() { | |
self.init(frame: .zero) | |
addRecognizer() | |
} | |
private func addRecognizer() { | |
let recognizer = UITapGestureRecognizer(target: self, action: #selector(onTap(_:))) | |
addGestureRecognizer(recognizer) | |
} | |
@objc private func onTap(_ sender: UITapGestureRecognizer) { | |
let location = sender.location(in: self) | |
let coordinate = convert(location, toCoordinateFrom: self) | |
onMapTap?(coordinate) | |
} | |
} | |
struct MapView: UIViewRepresentable { | |
let region: MKCoordinateRegion | |
let lineCoordinates: [CLLocationCoordinate2D] | |
let onMapTap: (CLLocationCoordinate2D) -> Void | |
func makeUIView(context: Context) -> MKMapView { | |
let mapView = TappableMKMapView() | |
mapView.onMapTap = onMapTap | |
mapView.delegate = context.coordinator | |
mapView.region = region | |
let polyline = MKPolyline(coordinates: lineCoordinates, count: lineCoordinates.count) | |
mapView.addOverlay(polyline) | |
return mapView | |
} | |
func updateUIView(_ view: MKMapView, context: Context) { | |
let polyline = MKPolyline(coordinates: lineCoordinates, count: lineCoordinates.count) | |
view.addOverlay(polyline) | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
} | |
class Coordinator: NSObject, MKMapViewDelegate { | |
var parent: MapView | |
init(_ parent: MapView) { | |
self.parent = parent | |
} | |
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { | |
if let routePolyline = overlay as? MKPolyline { | |
let renderer = MKPolylineRenderer(polyline: routePolyline) | |
renderer.strokeColor = UIColor.systemBlue | |
renderer.lineWidth = 10 | |
return renderer | |
} | |
return MKOverlayRenderer() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment