gpt4 book ai didi

ios - MKPolyLine 未显示在 Swift 2.0 中的 iOS 应用程序上

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

我正在创建一种方向/GPS 应用程序,到目前为止一切都很简单。

  • 我已经弄清楚如何查找用户的位置(当然要经过他们的许可)
  • 我设法让他们能够轻松快捷地设置目的地

但是,我遇到了一个小问题。我想要的是用户在屏幕上选择目的地,应用程序将为他们提供到达目的地的最快方式。

这是我的 View Controller :

class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

@IBOutlet var mapOfMaps: MKMapView!
let locationManager = CLLocationManager()
var center:CLLocationCoordinate2D!
var SourcePM:MKPlacemark!
let sourcePlacemark: MKPlacemark! = nil
override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapOfMaps.showsUserLocation = true

let sourceAnnotation = MKPointAnnotation()

if let location = locationManager.location {
sourceAnnotation.coordinate = location.coordinate
}
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CardioViewController.action(_:)))
longPress.minimumPressDuration = 1.0
mapOfMaps.addGestureRecognizer(longPress)

//directionRequest
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003))
self.mapOfMaps.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
SourcePM = MKPlacemark(coordinate: center, addressDictionary: nil)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("ERROr " + error.localizedDescription)
}

func action(gestureRecognizer:UIGestureRecognizer) {
let touchPoint = gestureRecognizer.locationInView(self.mapOfMaps)
let newCoord:CLLocationCoordinate2D = mapOfMaps.convertPoint(touchPoint, toCoordinateFromView: self.mapOfMaps)

let newAnotation = MKPointAnnotation()
newAnotation.coordinate = newCoord
newAnotation.title = "Your Destination"
mapOfMaps.addAnnotation(newAnotation)
let anotPM = MKPlacemark(coordinate: newAnotation.coordinate, addressDictionary: nil)


let source = MKMapItem(placemark: SourcePM)
let dstn = MKMapItem(placemark: anotPM)

let directionRequest = MKDirectionsRequest()
directionRequest.source = source
directionRequest.destination = dstn
directionRequest.transportType = .Automobile

// Calculate the direction
let directions = MKDirections(request: directionRequest)

// 8.
directions.calculateDirectionsWithCompletionHandler() {
(response, error) in
if(error == nil && response != nil) {
for route in response!.routes {
var r: MKRoute = route as! MKRoute
self.mapOfMaps.addOverlay(r.polyline, level: MKOverlayLevel.AboveRoads)
}
}

let route = response!.routes[0]
self.mapOfMaps.addOverlay((route.polyline), level: MKOverlayLevel.AboveLabels)


let rect = route.polyline.boundingMapRect
self.mapOfMaps.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}

}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.orangeColor()
renderer.alpha = 1
renderer.lineWidth = 4.0

return renderer
}
}

帮助您完成我的代码。我的用户将按住 map 上的目的地,应用程序将添加注释,并且它应该给出路线。但是,所发生的只是注释将添加到 map 中,并且 map 将调整以显示两个位置(用户的位置和注释位置),但不显示路线。

最佳答案

这可能不是“理想”的解决方案,但值得注意的是,两个 locationManager 功能不能同时工作。我尝试对 locationManager 函数之一进行评论,结果发现另一个函数运行良好

关于ios - MKPolyLine 未显示在 Swift 2.0 中的 iOS 应用程序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781633/

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