gpt4 book ai didi

ios - 尝试从文本字段获取纬度和经度时无法转换类型的值

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:11 26 4
gpt4 key购买 nike

Image of the project

我的代码不工作。当用户输入纬度和经度时,它应该转到 map ,但它只是抛出一个错误。它仅在我对纬度和经度进行硬编码时有效。

import UIKit
import MapKit

class ViewController: UIViewController {
@IBOutlet weak var Longitude: UITextField!
@IBOutlet weak var Latitude_button: UITextField!

@IBAction func showMeWhere(_ sender: Any)
{
//Defining destination
let latitude:CLLocationDegrees = Latitude_button
let longitude:CLLocationDegrees = Longitude

// let latitude:CLLocationDegrees = 39.048825
// let longitude:CLLocationDegrees = -120.981227

let regionDistance:CLLocationDistance = 1000;
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)

let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]

let placemark = MKPlacemark(coordinate: coordinates)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Test Location"
mapItem.openInMaps(launchOptions: options)
}
}

最佳答案

在您的 let latitude:CLLocationDegrees = Latitude_button 行中,您试图将类型为 UITextField 的变量分配给类型为 CLLocationDegrees 的变量。

您需要做的是从文本字段中获取文本并尝试将其转换为数字,然后将该数字分配给您的变量。

guard let latitude = CLLocationDegrees(Latitude_button.text!),
let longitude = CLLocationDegrees(Longitude.text!) else {
// show some sort message to the user that the values are invalid
return
}

关于ios - 尝试从文本字段获取纬度和经度时无法转换类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53272798/

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