1

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!

Johnny Hsieh
  • 856
  • 2
  • 10
  • 23
  • I think you need to provide a bit more details. What information do you need to pass to the mapview? Is this mapview in the same view controller or will you instansiate a new one? – mlidal Jul 13 '16 at 07:49
  • Sorry for not describe my question clear, 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. – Johnny Hsieh Jul 13 '16 at 07:54
  • Can you show the declaration of the "array content of CLLocationCoordinate2D and location detail" you mentioned. – Luke Van In Jul 13 '16 at 08:21

4 Answers4

5

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)
}
Luke Van In
  • 5,215
  • 2
  • 24
  • 45
  • Thanks you @Luke this is what I need! How can I change the unknown location with my array's title? – Johnny Hsieh Jul 13 '16 at 08:36
  • You can set the title, address, and other into by passing a dictionary to the placemark `addressDictionary` property. See the documentation for [ABPerson](https://developer.apple.com/library/ios/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/index.html#//apple_ref/doc/uid/TP40007210) for the available keys. If you update your question to show how you store this information, e.g. the data structure which has the location's coordinate and title, then I will update my answer with an example of how to do this. – Luke Van In Jul 13 '16 at 08:41
  • Appreciate @Luke I have add my information I stored. – Johnny Hsieh Jul 13 '16 at 08:50
  • 1
    Updated to show how to set the name of the mapItem to the tripspot title. – Luke Van In Jul 13 '16 at 09:29
1

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

Syafiq Mastor
  • 178
  • 2
  • 10
1

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>)
Community
  • 1
  • 1
mlidal
  • 1,111
  • 14
  • 27
1

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])
Prabhu.Somasundaram
  • 1,380
  • 10
  • 13