我的代码不起作用.
当用户输入纬度和经度时,它应该转到地图,但它只会引发错误.它只适用于我对纬度和经度进行硬编码的情况.
import UIKitimport MapKitclass 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) }}解决方法 在你的行上,让你的纬度: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 – 尝试从文本字段获取纬度和经度时无法转换类型的值全部内容,希望文章能够帮你解决ios – 尝试从文本字段获取纬度和经度时无法转换类型的值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)