我如何在此函数中使用常量
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])
}
我在点击按钮后在哪里打开地图,所以我想让坐标=坐标,我该怎么办?
最佳答案
基本上,您似乎想要保留对传入位置的引用,并在以后使用。这表明:
创建一个实例变量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
}
关于ios - 如何在按钮func中传递特定func常数的值以将其用作导航的目的地,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47141138/