Swift 为什么可以';使用操作按钮将城市输出到文本标签?
我已经在viewdidload中设置了位置和所有内容。用户几乎会按下一个按钮,一个标签会显示他们当前的位置(城市) 错误:使用未解析的标识符“placemark”Swift 为什么可以';使用操作按钮将城市输出到文本标签?,swift,cllocationmanager,Swift,Cllocationmanager,我已经在viewdidload中设置了位置和所有内容。用户几乎会按下一个按钮,一个标签会显示他们当前的位置(城市) 错误:使用未解析的标识符“placemark” 这种方法可以解决您的问题: func displayLocationInfo(placemark:CLLocationCoordinate2D) ->String{ self.locationManager.stopUpdatingLocation() originTextField.text
这种方法可以解决您的问题:
func displayLocationInfo(placemark:CLLocationCoordinate2D) ->String{
self.locationManager.stopUpdatingLocation()
originTextField.text = ("locations = \(placemark.latitude) \(placemark.longitude)")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
displayLocationInfo(locValue)
}
@IBAction func currentLocation(sender: AnyObject) {
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
var
location
在函数displayLocationInfo()
中定义。您如何在currentLocation()
中访问它?请尝试originTextField.text=displayLocationInfo(您的位置标记)
originTextField.text=displayLocationInfo(位置)“使用未解析的标识符‘location’。您必须基于当前位置创建一个placemark,然后必须将该placemark传递给funcdisplayLocationInfo()
No,否则将无法使用。”。您刚刚创建了一个函数,可以接受placemark并返回location。但是,您永远不会使用特定的placemark调用函数。获取位置没有问题。问题在于仅在按下iAction按钮后将位置放置在originTextField.text标签中。displayLocationInfo(placemark:CLPlacemark)函数要求placemark类型为CLPlacemark,但您正在iAction.yes中发送未定义的值名称位置。如果我发送placemark.locality,它仍然有错误
func displayLocationInfo(placemark:CLLocationCoordinate2D) ->String{
self.locationManager.stopUpdatingLocation()
originTextField.text = ("locations = \(placemark.latitude) \(placemark.longitude)")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
displayLocationInfo(locValue)
}
@IBAction func currentLocation(sender: AnyObject) {
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}