如何在按钮 func 中传递特定 func 的常量值以将其用作导航的目的地
Posted
技术标签:
【中文标题】如何在按钮 func 中传递特定 func 的常量值以将其用作导航的目的地【英文标题】:How to pass the value of a constant of a specific func in a button func to use it as destination of the navigation 【发布时间】:2017-11-06 16:08:41 【问题描述】:如何在这个函数中使用常量
func didSelect(place:QPlace)
guard let coordinates = place.location else
return
// clear current marker
marker?.map = nil
// add marker
marker = GMSMarker()
marker?.position = coordinates
marker?.title = place.name
marker?.map = mapView
mapView.selectedMarker = marker
moveToMarker(marker!)
// update bottom info panel view
let desc = place.getDescription()
descriptionLabel.text = desc.characters.count > 0 ? desc : "-"
distanceLabel.text = "-"
// update distance
if userLocation != nil
let dist = distance(from: userLocation!, to: coordinates)
distanceLabel.text = String.init(format: "Distance %.2f meters", dist)
self.drawPath(startLocation: userLocation!, endLocation: coordinates)
title = place.name
(所以常量coordinates
)在这里得到目的地
@IBAction func navigationStart(_ sender: Any)
let coordinate = CLLocationCoordinate2DMake(51.5007, -0.1246)
let placeMark = MKPlacemark(coordinate: coordinate)
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "Big Ben"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
点击按钮后我在哪里打开地图,所以我想要让坐标 = 坐标,我该怎么做?
【问题讨论】:
将destPosition
声明为属性(在类级别)?
【参考方案1】:
基本上看起来您想要保存对传入位置的引用并在以后使用它。所以这表明:
-
创建实例变量
var destination: CLLocationCoordinate2D?
从方法中分配目的地:
func didSelect(place:QPlace)
destination = place.location
设置目的地
@IBAction func navigationStart(_ sender: Any)
if let coordinate = self.destination
let placeMark = MKPlacemark(coordinate: coordinate)
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "Big Ben"
mapItem.openInMaps(launchOptions:
[MKLaunchOptionsDirectionsModeKey:
MKLaunchOptionsDirectionsModeDriving])
假设它有效,我将它基于您的代码。没有检查类型。如果它不起作用,那应该很容易解决。但主要的逻辑是这样的。
更新 2.:
func didSelect(place:QPlace)
guard let coordinates = place.location else
return
// ADD THIS
self.destination = coordinates
// clear current marker
marker?.map = nil
// add marker
marker = GMSMarker()
marker?.position = coordinates
marker?.title = place.name
marker?.map = mapView
mapView.selectedMarker = marker
moveToMarker(marker!)
// update bottom info panel view
let desc = place.getDescription()
descriptionLabel.text = desc.characters.count > 0 ? desc : "-"
distanceLabel.text = "-"
// update distance
if userLocation != nil
let dist = distance(from: userLocation!, to: coordinates)
distanceLabel.text = String.init(format: "Distance %.2f meters", dist)
self.drawPath(startLocation: userLocation!, endLocation: coordinates)
title = place.name
【讨论】:
对不起,我用完整的功能编辑了问题,如果我拿掉保护让,我会在代码中得到很多错误 @Tvenden 试试这个。再次,逻辑是相同的。保持对位置的引用并在按钮单击方法中更新到该位置。以上是关于如何在按钮 func 中传递特定 func 的常量值以将其用作导航的目的地的主要内容,如果未能解决你的问题,请参考以下文章
如何调用Func并将其传递给构造函数中具有较少参数的另一个Func?
如何从包含Python3中特定索引和列的列表的dict创建Pandas DataFrame?
如何将网格传递给“void my_func(void * args)”
如何从`callq func @ PLT`获取`func`的实际地址