gpt4 book ai didi

swift - 如何更新 Apple map 中的路线 - iOS 13.0 中的 Xcode Swift 5?

转载 作者:行者123 更新时间:2023-11-30 10:32:02 25 4
gpt4 key购买 nike

我正在像 Uber 这样的 MapApp 中工作,但我需要更新折线叠加层。问题是我找不到任何响应更新路线的方法或事件。

这是我的代码:

import UIKit                                                 
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var StopRoute: UIBarButtonItem!
@IBOutlet var mapa: MKMapView!
@IBOutlet weak var PlayRoute: UIBarButtonItem!
var locationManager = CLLocationManager()
var localization: CLLocation?
var anotationCounter: Set<MKPointAnnotation> = Set<MKPointAnnotation>()
var destinationMapItem: MKMapItem?
var destinationMarker: MKPointAnnotation?
open var destinationImage: UIImage?

//Control
var routeNum: Int = 0
var onPathfinding = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.mapa.delegate = self

//Autorización para jalar el mapa
locationManager.requestWhenInUseAuthorization()

//Mostar current location
mapa.showsUserLocation = true
mapa.showsScale = true

//Update curent location
locationManager.startUpdatingLocation()

//Tratando Datos
UserLocation()

let tapPressRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapPress(_:)))
tapPressRecognizer.numberOfTapsRequired = 1

mapa.addGestureRecognizer(tapPressRecognizer)

}

func UserLocation(){
//trata de mantener actualizado lat y long siempre que sus valores sean != de nil
localization = locationManager.location
let latitude: Double? = localization?.coordinate.latitude
let longitude: Double? = localization?.coordinate.longitude
mapa.userLocation.title = "Tu ubicación"
if let lat = latitude, let long = longitude{
//inforrmación extrra
mapa.userLocation.subtitle = "(\(lat); \(long))"
AdjustMap(location: localization!)

}
}

//Funcion que ajusta la pantalla y la centra en la ubiacacion
func AdjustMap(location: CLLocation){
let coordinatesRegion = MKCoordinateRegion(center: localization!.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)

mapa.setRegion(coordinatesRegion, animated: true)
}

//Encontrando el camino al punto designado y toma como parametro la cantidad de rutas que quiero
func Pathfinding(routeNum: Int){
//Limpia mapa y toma mis coordenadas
self.mapa.removeOverlays(self.mapa.overlays)
let coor = locationManager.location?.coordinate
let start = MKMapItem(placemark: MKPlacemark(coordinate: coor!))

//var routeArray = Array<MKDirections.Response>()

let request = MKDirections.Request()
request.source = start
request.destination = destinationMapItem
request.requestsAlternateRoutes = true
request.transportType = .automobile


let tracking = MKDirections(request: request)
tracking.calculate(completionHandler: {(routes, error) in
if let out = error{
print("Error \(out)")
return
}
else if let rutas = routes?.routes
{
self.mapa.addOverlay(rutas[self.routeNum].polyline, level: MKOverlayLevel.aboveRoads)
}
})
}

//Definiendo el gesto de toque corto sobre la pantalla
@objc func handleTapPress(_ gestureRecognizer: UITapGestureRecognizer){
if !onPathfinding
{
//LimpiaMarca anterior
if destinationMarker != nil{
mapa.removeAnnotation(destinationMarker!)
}

let tapLocation = gestureRecognizer.location(in: mapa)
let coordinates = mapa.convert(tapLocation, toCoordinateFrom: mapa)

destinationMapItem = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coordinates.latitude, longitude: coordinates.longitude)))

destinationMarker = MKPointAnnotation()
destinationMarker?.coordinate = coordinates
mapa.addAnnotation(destinationMarker!)
}
}

@IBAction func PlayRoute(_ sender: Any) {
if !onPathfinding
{
onPathfinding = true
Pathfinding(routeNum: routeNum)
print(onPathfinding)
}
}

@IBAction func StopRoute(_ sender: Any) {
if onPathfinding
{
onPathfinding = false
print(onPathfinding)
self.mapa.removeOverlays(self.mapa.overlays)
}
}

//Defininiendo la línea de traceo
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.orange
renderer.lineWidth = 5
return renderer
}
}

我定义了几个按钮来启动/结束寻路功能,但我需要随着时间的推移更新路线。

我在 Apple 的文档和网络中寻找它,但找不到该信息。

最佳答案

我认为你需要注册一个 CLLocationManagerDelegate 然后拥有该功能

optional func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

然后您可以根据需要更新您的路线。

关于swift - 如何更新 Apple map 中的路线 - iOS 13.0 中的 Xcode Swift 5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58979480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com