Swift 如何从我在搜索栏中定义的地图项目构建路线
Swift How to build a route FROM map Item, which I define in Search Bar
抱歉,如果主题不是那么准确。我正在尝试构建一个点到点的地图路线,关键是这两个点都是由用户定义的。所以我想从界面生成器传递源 mapItem 和目标 mapItem 并在它们之间建立路由。
我做了什么:
我有一个建议 table 视图控制器,它会在搜索栏中输入内容时建议搜索 table 视图控制器的位置;然后搜索TVC将带有名称和地址的地图注释传递给结果地图View。
在我的结果地图视图中,我有两个按钮:源和目标。因此,当从搜索 TVC 传递地址时,mapView 建议按下两个按钮。如果按下 Destination 按钮,则意味着源地图项为 nil,它会构建从我当前位置到目标地图项的路线。这里没问题,但是当按下 Source 按钮时,不会存储源地图项,当搜索目标地图项并按下 Destination 按钮时,应用程序会从我当前的位置再次构建路线,忽略之前我按下 Source 按钮并且我需要不是从我当前位置出发,而是从源 mapItem 出发的路线。
所以在代码中,我定义了变量:
var mapItems: [MKMapItem]?
private var sourceMapItem: MKMapItem!
@IBOutlet weak var selectPointBmessage: UILabel!
@IBOutlet weak var resultMapView: MKMapView!
@IBOutlet weak var resultName: UILabel!
@IBOutlet weak var resultAddress: UILabel!
两个函数:findMe(),用于搜索我的当前位置,以及 buildRoute(destinationMapItem: MKMapItem),如果源地图项为 nil,则构建一条从我当前位置到 destinationMapItem 的路线。
和两个按钮:
@IBAction func sourceButton(_ sender: UIButton) {
// add searched mapItems to Array of MapItems
sourceMapItem = mapItems!.first
// message saying the user to find destination is source button is pressed
selectPointBmessage.isHidden = false
}
@IBAction func buildRouteButton(_ sender: UIButton) {
// add searched mapItems to Array of MapItems
let destinationMapItems = mapItems!.compactMap { (mapItem) -> MKMapItem in
let destinationMapItem = mapItem
return destinationMapItem
}
// for each if several or for one if one - build a route(s) to it
for destinationMapItem in destinationMapItems {
buildRoute(sourceMapItem: sourceMapItem, destinationMapItem: destinationMapItem)
}
}
(mapItems 定义并记录在 Search Table View Controller 中。这里我们使用 compact map 解压它们并开始与它们交互。)
和其他内容,如添加注释、到 mapView 的路线、两个 Swift 文件以及建议 TVC 和搜索 TVC。
还添加了 StoryBoard 的 printScreen 以显示其组织方式。
storyBoard printScreen
如果需要更多详细信息,请键入。
所以:
如何让我的应用程序记住 sourceMapItem 并在按下“目的地”按钮时构建用户定义的点到点的路线?
所以我创建了一个 mapItems 数组,当 table 行被选中时,将 mapItems 存储在其中。然后在调用我的按钮时,使用该数组,我已经为我的源点和目标点分配了值。
抱歉,如果主题不是那么准确。我正在尝试构建一个点到点的地图路线,关键是这两个点都是由用户定义的。所以我想从界面生成器传递源 mapItem 和目标 mapItem 并在它们之间建立路由。
我做了什么: 我有一个建议 table 视图控制器,它会在搜索栏中输入内容时建议搜索 table 视图控制器的位置;然后搜索TVC将带有名称和地址的地图注释传递给结果地图View。 在我的结果地图视图中,我有两个按钮:源和目标。因此,当从搜索 TVC 传递地址时,mapView 建议按下两个按钮。如果按下 Destination 按钮,则意味着源地图项为 nil,它会构建从我当前位置到目标地图项的路线。这里没问题,但是当按下 Source 按钮时,不会存储源地图项,当搜索目标地图项并按下 Destination 按钮时,应用程序会从我当前的位置再次构建路线,忽略之前我按下 Source 按钮并且我需要不是从我当前位置出发,而是从源 mapItem 出发的路线。
所以在代码中,我定义了变量:
var mapItems: [MKMapItem]?
private var sourceMapItem: MKMapItem!
@IBOutlet weak var selectPointBmessage: UILabel!
@IBOutlet weak var resultMapView: MKMapView!
@IBOutlet weak var resultName: UILabel!
@IBOutlet weak var resultAddress: UILabel!
两个函数:findMe(),用于搜索我的当前位置,以及 buildRoute(destinationMapItem: MKMapItem),如果源地图项为 nil,则构建一条从我当前位置到 destinationMapItem 的路线。
和两个按钮:
@IBAction func sourceButton(_ sender: UIButton) {
// add searched mapItems to Array of MapItems
sourceMapItem = mapItems!.first
// message saying the user to find destination is source button is pressed
selectPointBmessage.isHidden = false
}
@IBAction func buildRouteButton(_ sender: UIButton) {
// add searched mapItems to Array of MapItems
let destinationMapItems = mapItems!.compactMap { (mapItem) -> MKMapItem in
let destinationMapItem = mapItem
return destinationMapItem
}
// for each if several or for one if one - build a route(s) to it
for destinationMapItem in destinationMapItems {
buildRoute(sourceMapItem: sourceMapItem, destinationMapItem: destinationMapItem)
}
}
(mapItems 定义并记录在 Search Table View Controller 中。这里我们使用 compact map 解压它们并开始与它们交互。)
和其他内容,如添加注释、到 mapView 的路线、两个 Swift 文件以及建议 TVC 和搜索 TVC。 还添加了 StoryBoard 的 printScreen 以显示其组织方式。 storyBoard printScreen 如果需要更多详细信息,请键入。
所以: 如何让我的应用程序记住 sourceMapItem 并在按下“目的地”按钮时构建用户定义的点到点的路线?
所以我创建了一个 mapItems 数组,当 table 行被选中时,将 mapItems 存储在其中。然后在调用我的按钮时,使用该数组,我已经为我的源点和目标点分配了值。