Ios 在Swift中,我可以检测CLLocation是否有效吗?
我正在使用Ios 在Swift中,我可以检测CLLocation是否有效吗?,ios,swift,mapkit,core-location,Ios,Swift,Mapkit,Core Location,我正在使用prepareForSegue将一个位置传递给另一个UIViewController,该控制器具有MapView。我称之为receivedLocation。地图以经过的位置为中心。如果用户单击按钮将他们带到地图,而不首先发送位置,这不会失败吗 如何测试receivedLocation是否实际包含数据,以便在地图为空时将其设置为其他内容的中心 创建一个布尔变量,并最初将其设置为false,但在传递位置和测试时将其设置为true,这是否符合我的要求 我看到一个名为CLLocationCoo
prepareForSegue
将一个位置传递给另一个UIViewController
,该控制器具有MapView
。我称之为receivedLocation
。地图以经过的位置为中心。如果用户单击按钮将他们带到地图,而不首先发送位置,这不会失败吗
如何测试receivedLocation是否实际包含数据,以便在地图为空时将其设置为其他内容的中心
创建一个布尔变量,并最初将其设置为false,但在传递位置和测试时将其设置为true,这是否符合我的要求
我看到一个名为CLLocationCoordinateIsValid
的东西可用,但它的参数是2DCoordinate,在这里我遇到了相同的问题,即测试receivedLocation
是否有效
我对这方面还不太熟悉,已经尝试过寻找答案,但找不到合适的答案。谢谢大家!
import UIKit
import MapKit
import CoreLocation
class mapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var map: MKMapView!
@IBOutlet var notes: UITextView!
var receivedLocation = CLLocation()
var annotation = MKPointAnnotation()
var currentManager:CLLocationManager!
var latitute:CLLocationDegrees = CLLocationDegrees()
var longitude:CLLocationDegrees = CLLocationDegrees()
override func viewDidLoad() {
super.viewDidLoad()
map.layer.cornerRadius = 12.0
notes.layer.cornerRadius = 12.0
currentManager = CLLocationManager()
currentManager.delegate = self
currentManager.desiredAccuracy = kCLLocationAccuracyBest
currentManager.startUpdatingLocation()
if CLLocationCoordinate2DIsValid(receivedLocation.coordinate) {
latitute = receivedLocation.coordinate.latitude
longitude = receivedLocation.coordinate.longitude
annotation.coordinate.latitude = receivedLocation.coordinate.latitude
annotation.coordinate.longitude = receivedLocation.coordinate.longitude
map.addAnnotation(annotation)
} else {
latitute = currentManager.location.coordinate.latitude
longitude = currentManager.location.coordinate.longitude
}
var latDelta: CLLocationDegrees = 0.01
var lonDelta: CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute, longitude)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: false)
}
到目前为止,这对我不起作用。CLLocationCoordination2DisValid始终返回true并以一个岛为中心,并向该奇怪的点添加注释。首先,不要不必要地将无用的
CLLocation()
放入receivedLocation
中。将receivedLocation
声明为隐式展开的可选项:
这样一来,要么有人设定了,要么没有设定。如果没有,则为零,您可以测试:
if receivedLocation != nil {
// okay, it's a real location!
}
其次,你的
else
永远不会工作,因为传感器需要时间预热并获得位置-事实上,它们可能永远也得不到位置。因此,我可以很好地保证,在receivedLocation
为零的情况下,currentManager.location
也不会有任何用处。(有关如何使用位置管理器获取单个位置值的说明和指向示例代码的指针,请参阅。)@CodyLucas如果您只想检查有效位置,则也可以使用问题中提到的“CLLocationCoordinateIsValid”函数。下面是swiftvar location=CLLocation()中的示例代码var isValid=CLLocationCoordinate2DIsValid(location.coordinate)
要更深入地理解这个问题,需要您的源代码。我不明白。您在该代码中引用的receivedLocation
是什么?显示其声明。@matt抱歉,我不知道为什么在viewDidLoad()处停止。。。给你。很好,正在准备答案。很好,你已经准备好了。至于else,receivedLocation只有在按下按钮后才有一个值。如果他们不按地图就去看地图,这意味着要处理这个问题。现在我唯一的问题是,如果我添加了一个注释,然后离开MapView并返回,注释就消失了。你不必回答,因为这是一个单独的问题,但如果你有时间,什么能解决这个问题?不,你是对的,这是一个单独的问题。请随便问你的其他问题作为一个单独的问题!更多的问题是好的。(很好。)正如您将发现的,Stack Overflow是为单问题问题设计的,我们刚才使用的问题/评论/编辑/回答循环非常有效(如您所见!)。如果你在评论中再问一个问题,事情就会失控。谢谢你的帮助,马特!祝你度过一个美妙的夜晚。
if receivedLocation != nil {
// okay, it's a real location!
}