显示两点之间的方向 - 斯威夫特

时间:2016-05-24 20:17:36

标签: ios swift mapkit apple-maps

我试图将用户当前位置的路线显示到他们搜索的所需位置。这是代码,我选择一个位置时会遇到同样的错误。 "以NSException类型的未捕获异常终止"从thr UITableView中选择一个位置后。有人可以帮忙吗? 我的代码如下:
 谢谢,杰克。

import UIKit
import MapKit

class LocationSearchTable : UITableViewController {
var handleMapSearchDelegate:HandleMapSearch! //=nil
var matchingItems:[MKMapItem] = []
var mapView: MKMapView!


func parseAddress(selectedItem:MKPlacemark) -> String {
    // put a space between "4" and "Melrose Place"
    let firstSpace = (selectedItem.subThoroughfare != nil &&       selectedItem.thoroughfare != nil) ? " " : ""
    // put a comma between street and city/state
    let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
    // put a space between "Washington" and "DC"
    let secondSpace = (selectedItem.subAdministrativeArea != nil && selectedItem.administrativeArea != nil) ? " " : ""
    let addressLine = String(
        format:"%@%@%@%@%@%@%@",
        // street number
        selectedItem.subThoroughfare ?? "",
        firstSpace,
        // street name
        selectedItem.thoroughfare ?? " ",
        comma,
        secondSpace,
        // city
        selectedItem.locality ?? " ",
        secondSpace,
        // state
        selectedItem.administrativeArea ?? " "
    )
    return addressLine
}
}

extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController:  UISearchController) {
        guard let mapView = mapView,
        let searchBarText = searchController.searchBar.text else {   return }
        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = searchBarText
        request.region = mapView.region
        let search = MKLocalSearch(request: request)
        search.startWithCompletionHandler { response, _ in
        guard let response = response else {
            return
        }
        self.matchingItems = response.mapItems

        self.tableView.reloadData()
    }
    }
    }
    extension LocationSearchTable {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return matchingItems.count
}

    override func tableView(tableView: UITableView,  cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = parseAddress(selectedItem)
    return cell
    }
}

extension LocationSearchTable {
    override func tableView(tableView: UITableView,     didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedItem = matchingItems[indexPath.row].placemark
    print("£££££££££££££££££££")
    print(selectedItem.location!.coordinate.latitude);
    print("£££££££££££££££££££")
    print(mapView);
    print(mapView!.userLocation.coordinate.latitude);
    print(mapView!.userLocation.coordinate.longitude);

    handleMapSearchDelegate!.dropPinZoomIn(selectedItem)
    dismissViewControllerAnimated(true, completion: nil)

    let request: MKDirectionsRequest = MKDirectionsRequest()

    let userPlacemark = MKPlacemark(coordinate:  (mapView.userLocation.coordinate), addressDictionary: [:])


    let source = MKMapItem(placemark: userPlacemark)
    let destination = MKMapItem(placemark: selectedItem)

    // source and destination are the relevant MKMapItems
    request.source = source // matchingitems
    request.destination = destination // selectedItem

    // Specify the transportation type
    request.transportType = MKDirectionsTransportType.Walking;

    // If you're open to getting more than one route,
    // requestsAlternateRoutes = true; else requestsAlternateRoutes = false;
    request.requestsAlternateRoutes = true

    let directions = MKDirections(request: request)
    let route = MKRoute()
    print("----------------------------------5");
    print(directions);
    print("£££££0");

    directions.calculateDirectionsWithCompletionHandler { (response, error) in

        let launchOptions = [

            MKLaunchOptionsDirectionsModeKey,
            MKLaunchOptionsDirectionsModeWalking]


    }
   // let directions = MKDirections(request: request)



mapView.addOverlay(route.polyline)
    //mapView.addOverlay(route.polyline, level:     MKOverlayLevel.AboveRoads)


}

func showRoute(response:MKDirectionsResponse){

    print("----------------------------------");

    for route in (response.routes as [MKRoute]){
        print(route);
        print("----------------------------------2");

        //ISSUE ON THIS LINE
        mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
        print("----------------------------------3");


        let routeSeconds = route.expectedTravelTime
        let routeDistance = route.distance
        print("distance between two points is \(routeSeconds) and \(routeDistance)")


        }

    }

}

0 个答案:

没有答案