任何人都可以提供帮助 - 以下代码适用于所有方面,除了路线折线没有在地图上绘制 - 你能告诉我我缺少什么吗?
我一直在这个特定的树上走了很长一段时间 - 大声笑
import UIKit
import MapKit
protocol HandleMapSearch: class {
func dropPinZoomIn(_ placemark:MKPlacemark)
}
class ViewController: UIViewController {
var selectedPin: MKPlacemark?
var resultSearchController: UISearchController!
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Location Manager Variables
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
//Setup Search Bar and Table
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Do You Know Where You're Going To LA LA LA?"
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
}
}
extension ViewController : CLLocationManagerDelegate {
// Find PolicyHolders current Location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
let distancespan: CLLocationDegrees = 2000
let PolicyHolderLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(PolicyHolderLocation, distancespan, distancespan), animated: true)
// Drop pin at Policyholders current Location
let PolicyHolderPin = Annotation(title: "Title", subtitle: "subtitle", coordinate: PolicyHolderLocation)
mapView.addAnnotation( PolicyHolderPin )
print (PolicyHolderLocation)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error:: \(error)")
}
}
// Show queried location on map
extension ViewController: HandleMapSearch {
func dropPinZoomIn(_ placemark: MKPlacemark){
// cache the pin
selectedPin = placemark
// clear existing pins
//mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
annotation.title = placemark.name
if let city = placemark.locality,
let state = placemark.administrativeArea {
annotation.subtitle = "\(city) \(state)"
}
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(placemark.coordinate, span)
mapView.setRegion(region, animated: true)
}
}
// Drop Pin amd show route into queried location
extension ViewController : MKMapViewDelegate {
private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) {
//Drop Pin
mapView.delegate = self
// Convert source and destination routes to latitude and longitude
let location = locationManager.location
let sourceLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.longitude)
// Make them placemarks (probably a duplicate of above but I dont know how to call the existing policy holder placemark from the two functions)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
// Make them mapitems
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: selectedPin!)
// set up and calculate the route
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
// Draw the directions (not working!!!)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
} }
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 4.0
return renderer
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}