UI按钮传递位置信息并开始在swift中在地图上导航

UI按钮传递位置信息并开始在swift中在地图上导航,swift,uitableview,mapkit,Swift,Uitableview,Mapkit,我有一个CLLocationcooridinate2d的数组内容和位置详细信息。我用Uibutton将它放在tableview单元格上,我所做的是尝试通过Uibuttom传递特定的单元格信息,启动地图视图的新视图并开始导航。这是我的密码: var tripspot:[tripSpot] = [ tripSpot( title: "一中商圈", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionR

我有一个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){



}

谢谢你的建议

我真的不明白你的问题。也许你的问题不具体。但是,如果按钮操作不起作用。你可以这样做

protocol ButtonDelegate {
func buttonPressedOnIndexPath(indexPath : NSIndexPath)
}

extension ViewController : UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell

    cell.indexPath = indexPath
    cell.delegate = self 
    return cell
}

extension ViewController : ButtonDelegate {
func buttonPressedOnIndexPath(indexPath: NSIndexPath) {
    //use the indexpath.row for the array
    print("indexPath : \(indexPath.row)")
}}

 class TableViewCell: UITableViewCell {
   var delegate : ButtonDelegate!
   var indexPath : NSIndexPath!

   @IBOutlet weak var naviButton: UIButton!

   @IBAction func buttonPressed(sender: UIButton) {
      delegate.buttonPressedOnIndexPath(indexPath)
   }
}

我希望这能有所帮助

我不太了解你的问题。也许你的问题不具体。但是,如果按钮操作不起作用。你可以这样做

protocol ButtonDelegate {
func buttonPressedOnIndexPath(indexPath : NSIndexPath)
}

extension ViewController : UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell

    cell.indexPath = indexPath
    cell.delegate = self 
    return cell
}

extension ViewController : ButtonDelegate {
func buttonPressedOnIndexPath(indexPath: NSIndexPath) {
    //use the indexpath.row for the array
    print("indexPath : \(indexPath.row)")
}}

 class TableViewCell: UITableViewCell {
   var delegate : ButtonDelegate!
   var indexPath : NSIndexPath!

   @IBOutlet weak var naviButton: UIButton!

   @IBAction func buttonPressed(sender: UIButton) {
      delegate.buttonPressedOnIndexPath(indexPath)
   }
}

我希望这会有所帮助

在这个问题上可能会有一些建议:

在UIButton上设置了
标记
属性后,您可以在tapDidNavi中访问此参数,并使用此参数从数组中获取CLLocationCoordinate2D

下一步要做什么取决于您如何设置应用程序。例如,如果地图视图位于不同的viewcontroller中,并且设置了segue,则可以调用

performSegueWithIdentifier("identifer", sender: <CLLocationCoordinate2D object>)
performsguewithidentifier(“identifer”,发送方:)

在这个问题上可能有一些建议:

在UIButton上设置了
标记
属性后,您可以在tapDidNavi中访问此参数,并使用此参数从数组中获取CLLocationCoordinate2D

下一步要做什么取决于您如何设置应用程序。例如,如果地图视图位于不同的viewcontroller中,并且设置了segue,则可以调用

performSegueWithIdentifier("identifer", sender: <CLLocationCoordinate2D object>)
performsguewithidentifier(“identifer”,发送方:)
您可以在“地图”应用程序中使用开始逐圈导航

从文件中:

如果在中指定mklaunchoptions方向modekey选项 启动选项字典时,mapItems数组不能超过 里面有两个项目。如果阵列包含一个项目,则地图应用程序 生成从用户当前位置到该位置的方向 由映射项指定。如果数组包含两项,则映射 应用程序生成从第一个项目的位置到第二个项目的方向 数组中第二项的位置

您可以使用启动地图应用程序中的逐圈导航

从文件中:

如果在中指定mklaunchoptions方向modekey选项 启动选项字典时,mapItems数组不能超过 里面有两个项目。如果阵列包含一个项目,则地图应用程序 生成从用户当前位置到该位置的方向 由映射项指定。如果数组包含两项,则映射 应用程序生成从第一个项目的位置到第二个项目的方向 数组中第二项的位置


请尝试此代码,AppleMap将打开,显示从设备当前位置到坐标指定位置的标记方向

    let coordinate = CLLocationCoordinate2DMake(currentLat, currentLong)
    let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
    mapItem.name = “Destination/Target Address or Name”
    mapItem.openInMapsWithLaunchOptions([MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])

请尝试此代码,AppleMap将打开,显示从设备当前位置到坐标指定位置的标记方向

    let coordinate = CLLocationCoordinate2DMake(currentLat, currentLong)
    let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
    mapItem.name = “Destination/Target Address or Name”
    mapItem.openInMapsWithLaunchOptions([MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])

我认为你需要提供更多的细节。您需要向mapview传递哪些信息?这是同一个视图控制器中的mapview,还是您要实例化一个新的视图控制器?很抱歉,我没有清楚地描述我的问题,我有一个数组内容CLLocationcooridinate2d和位置详细信息。我用Uibutton将其放在tableview单元格上,我所做的是尝试通过Uibuttom传递特定单元格信息,并启动地图视图的新视图并开始导航。您能否显示您提到的“CLLocationCoordinate2D的数组内容和位置详细信息”的声明。我想您需要提供更多的详细信息。您需要向mapview传递哪些信息?这是同一个视图控制器中的mapview,还是您要实例化一个新的视图控制器?很抱歉,我没有清楚地描述我的问题,我有一个数组内容CLLocationcooridinate2d和位置详细信息。我用Uibutton将它放在tableview单元格上,我所做的是尝试通过Uibuttom传递特定的单元格信息,启动地图视图的新视图并开始导航。你能展示你提到的“CLLocationCoordinate2D的数组内容和位置细节”的声明吗?谢谢你@Luke这就是我需要的!如何使用数组的标题更改未知位置?您可以通过将字典传递给placemark
addressDictionary
属性,将标题、地址和其他设置为。有关可用的密钥,请参阅文档。如果更新问题以显示如何存储此信息,例如,包含位置坐标和标题的数据结构,然后,我将用一个示例更新我的答案。感谢@Luke我已经添加了我存储的信息。更新显示了如何将mapItem的名称设置为tripspot标题。谢谢@Luke这是我需要的!如何使用数组的标题更改未知位置?您可以通过将字典传递给placemark
addressDictionary
属性,将标题、地址和其他设置为。有关可用的密钥,请参阅文档。如果您更新您的问题以显示如何存储此信息,例如,包含位置坐标和标题的数据结构,则我将用一个示例更新我的答案,说明如何执行此操作。感谢@Luke我已添加我存储的信息。更新以显示如何将地图项目的名称设置为tripspot标题。