Uibutton传递位置信息并开始在地图上快速导航 [英] Uibutton to pass location information and start to navigation on map in swift
问题描述
我有CLLocationcooridinate2d和位置详细信息的数组内容.我使用Uibutton将其放在表格单元格上,Im所做的就是试图通过Uibuttom传递特定的单元格信息,并启动地图视图的新视图并开始导航.这是我的代码:
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){
}
感谢您的任何建议!
推荐答案
You can use MKMapItem.openMapsWithItems:launchOptions: to start turn-by-turn navigation in the Maps app.
从文档中:
如果您在 launchOptions字典中,mapItems数组不得超过 其中有两个项目.如果数组包含一项,则地图"应用 生成从用户当前位置到该位置的路线 由地图项指定.如果数组包含两个项目,则Maps 应用会生成从第一个项目的位置到目的地的路线 数组中第二项的位置.
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)
}
这篇关于Uibutton传递位置信息并开始在地图上快速导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!