Я пытаюсь нарисовать маршрут от текущего местоположения пользователя до булавки, сброшенной longPressGesture. Я пытался нарисовать маршрут, но найти способы, которые работают только с выводами, установленными в коде, а не с помощью GestureRecognizer.
Я пытался установить его с помощью MKMapItems, но он не может принимать координаты местоположения, которые выбирает пользователь, потому что пользователь выбирает его, просто нажав на mapView
import UIKit import MapKit import CoreLocation import AVFoundation
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager:CLLocationManager!
let mapView = MKMapView()
var currentPlacemark: CLPlacemark?
let lbl: UILabel = {
let label = UILabel(frame: CGRect(x: 20, y: 80, width: 250, height: 50))
label.backgroundColor = UIColor.clear
label.textColor = UIColor.black
label.textAlignment = .left
label.text = "Running Map"
label.font = UIFont.boldSystemFont(ofSize: 35.0)
return label
}()
let startRunningButton: UIButton = {
let button = UIButton(frame: CGRect(x: 16, y: 398, width: 163, height: 53))
button.setTitleColor(UIColor.black, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
button.setTitle("Run", for: .normal)
button.backgroundColor = UIColor.white
button.layer.borderColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 10.0, height: 10.0)
button.clipsToBounds = false
button.layer.shadowOpacity = 0.3
button.layer.shadowColor = UIColor.black.cgColor
button.layer.cornerRadius = 10
button.layer.shadowRadius = 10
button.layer.shadowColor = UIColor.black.withAlphaComponent(0.75).cgColor
button.setTitleShadowColor(UIColor.black, for: .normal)
button.addTarget(self, action: #selector(showDirectionAndStartRunning), for: .touchUpInside)
return button
}()
@ objc func showDirectionAndStartRunning () {
}
let secondButton: UIButton = {
let button = UIButton(frame: CGRect(x: 196, y: 398, width: 163, height: 53))
button.setTitleColor(UIColor.black, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
button.setTitle("Save", for: .normal)
button.backgroundColor = UIColor.white
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 0
button.layer.shadowOffset = CGSize(width: 10.0, height: 10.0)
button.clipsToBounds = false
button.layer.shadowOpacity = 0.3
button.layer.shadowColor = UIColor.black.cgColor
button.layer.cornerRadius = 10
button.layer.shadowRadius = 10
button.layer.shadowColor = UIColor.black.withAlphaComponent(0.75).cgColor
button.setTitleShadowColor(UIColor.black, for: .normal)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(lbl)
view.addSubview(startRunningButton)
view.addSubview(secondButton)
self.navigationController?.navigationBar.isHidden = true
mapView.tintColor = UIColor.blue.withAlphaComponent(0.95)
mapView.backgroundColor = UIColor.clear
mapView.isZoomEnabled = true
}
//MARK: -SettingMap
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
super.viewWillAppear(animated)
let mapHeight:CGFloat = 270
mapView.frame = CGRect(x: 0, y: 459, width: 375, height: mapHeight)
mapView.mapType = MKMapType.standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
mapView.showsScale = true
mapView.showsPointsOfInterest = true
mapView.showsUserLocation = true
view.addSubview(mapView)
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(longpress(gestureRecognizer:)))
uilpgr.minimumPressDuration = 1.5
mapView.addGestureRecognizer(uilpgr)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
determineMyCurrentLocation()
}
//MARK: -Setting Pin On MKMapView By GestureRecognizer(Tap)
@objc func longpress(gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = gestureRecognizer.location(in: self.mapView)
let newCoordinate = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
let location = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)
var title = ""
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
if error != nil {
print(error)
} else {
if let placemark = placemarks?[0] {
if placemark.subThoroughfare != nil {
title += placemark.subThoroughfare! + " "
}
if placemark.thoroughfare != nil {
title += placemark.thoroughfare! + " "
}
}
}
if title == "" {
title = "Added \(NSDate())"
}
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
self.mapView.removeAnnotations(self.mapView.annotations)
self.mapView.addAnnotation(annotation)
})
}
}
//MARK: -MyCurrentLocation
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
*1012* здесь - это 1013 *
который позволяет пользователю увидеть его текущее местоположение и сбросить пин-код с помощью longPress