Uibutton to pass location information and start to navigation on map in swift

孤者浪人 提交于 2019-12-11 05:08:33

问题


I have a array content of CLLocationcooridinate2d and location detail. And I put it on tableview cell with Uibutton, what Im doing is trying to pass the specific cell information by Uibuttom and start a new view of map view and began to navigation. Here is my code:

   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){



}

thanks for any advice!


回答1:


You can use MKMapItem.openMapsWithItems:launchOptions: to start turn-by-turn navigation in the Maps app.

From the documentation:

If you specify the MKLaunchOptionsDirectionsModeKey option in the launchOptions dictionary, the mapItems array must have no more than two items in it. If the array contains one item, the Maps app generates directions from the user’s current location to the location specified by the map item. If the array contains two items, the Maps app generates directions from the location of the first item to the location of the second item in the array.

@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)
}



回答2:


I do not really understand about your problem. Perhaps your question is not specific. However, if the button action is not functioning. You can do this way;

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)
   }
}

I hope it helps




回答3:


There may be some suggestions in this SO question: Passing parameters on button action:@selector

As you have set the tag property on the UIButton you can access this parameter in tapDidNavi and use this to get the CLLocationCoordinate2D from the array.

What you do next depends on how you've set up your app. If you e.g. have the mapview in a different viewcontroller and have a segue set up you can call

performSegueWithIdentifier("identifer", sender: <CLLocationCoordinate2D object>)



回答4:


Try this code, AppleMap will open up with the directions marked from device's current location to the location specified the coordinates.

    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])


来源:https://stackoverflow.com/questions/38345371/uibutton-to-pass-location-information-and-start-to-navigation-on-map-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!