gpt4 book ai didi

ios - 如何在按钮函数中传递特定函数的常量值以将其用作导航目的地

转载 作者:行者123 更新时间:2023-11-30 12:01:40 25 4
gpt4 key购买 nike

如何在这个函数中使用常量

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
}

(因此常量坐标)在此处获取目的地

  @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])

}

我在点击按钮后打开 map ,所以我想让坐标=坐标,我该怎么办?

最佳答案

基本上,您似乎想保存对传入位置的引用并在以后使用它。这表明:

  1. 创建实例变量var目标:CLLocationCooperative2D?
  2. 从方法中指定目的地:

    func didSelect(place:QPlace) {
    destination = place.location
    }
  3. 设置目的地

    @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 - 如何在按钮函数中传递特定函数的常量值以将其用作导航目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47141138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com