Uibutton传递位置信息并开始在swift地图上导航

Joh*_*ieh 1 uitableview mapkit swift

我有一个CLLocationcooridinate2d的数组内容和位置详细信息.我把它放在带有Uibutton的tableview单元格上,我正在做的是尝试通过Uibuttom传递特定的单元格信息并开始一个新的地图视图视图并开始导航.这是我的代码:

   var tripspot:[tripSpot] = [
    tripSpot( title: "????", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionRadius: 300.0, location: "????????", type: "Food",cllocation:CLLocation(latitude: 24.181143,  longitude: 120.593158))


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MapCell") as! mapTableViewCell
    if  searchController.active{
    cell.title.text = searchResults[indexPath.row].title
    cell.location.text = searchResults[indexPath.row].location
    cell.naviButton.tag = indexPath.row
    cell.naviButton.addTarget(self, action: Selector("tapDidNavi"), forControlEvents: .TouchUpInside)

    return cell
    }else{
    cell.title.text = tripspot[indexPath.row].title
    cell.location.text = tripspot[indexPath.row].location
    cell.naviButton.tag = indexPath.row
    cell.naviButton.addTarget(self, action: Selector("tapDidNavi"), forControlEvents: .TouchUpInside)
    print(cell.naviButton.tag.description)
    return cell
    }


 }

@IBAction func tapDidNavi(sender: UIButton){



}
Run Code Online (Sandbox Code Playgroud)

谢谢你的建议!

Luk*_* In 5

您可以使用MKMapItem.openMapsWithItems:launchOptions:在地图应用中启动精细导航.

从文档:

如果在launchOptions字典中指定MKLaunchOptionsDirectionsModeKey选项,则mapItems数组中的项目必须不超过两个.如果数组包含一个项目,则地图应用程序会生成从用户当前位置到地图项目指定位置的路线.如果数组包含两个项目,则地图应用程序会生成从第一个项目的位置到阵列中第二个项目的位置的路线.

@IBAction func tapDidNavi(sender: UIButton){

    let location = self.tripspot[sender.tag]

    let placemark = MKPlacemark(
        coordinate: coordinate, 
        addressDictionary: nil
    )

    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = location.title

    let options = [
        MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
    ]

    MKMapItem.openMapsWithItems([mapItem], launchOptions: options)
}
Run Code Online (Sandbox Code Playgroud)