【问题标题】:In Swift, can I detect if a CLLocation is valid or not?在 Swift 中,我可以检测 CLLocation 是否有效?
【发布时间】:2015-05-11 04:19:54
【问题描述】:

我正在将一个位置传递给另一个UIViewController,它使用prepareForSegue 具有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)

    }

到目前为止,这对我不起作用。 CLLocationCoordinate2DIsValid 始终返回 true 并以岛屿为中心,并为那个奇怪的地方添加注释。

【问题讨论】:

  • @CodyLucas 如果您只想检查有效位置,那么您也可以使用问题中提到的 'CLLocationCoordinateIsValid' 函数。这是 swift var location = CLLocation() var isValid = CLLocationCoordinate2DIsValid(location.coordinate) 中的示例代码,以更深入地理解问题需要您的源代码。
  • 我不明白。您在该代码中引用的receivedLocation 是什么?显示它的声明。
  • @matt 抱歉,不知道我为什么停在 viewDidLoad()... 给你。
  • 好的,现在正在寻找答案。

标签: ios swift mapkit core-location


【解决方案1】:

首先,不要把一个无用的CLLocation() 放入receivedLocation。将receivedLocation 声明为隐式展开的可选

 var receivedLocation : CLLocation! = nil

这样,要么有人设置了它,要么他们没有。如果没有,则为 nil,您可以对其进行测试:

if receivedLocation != nil {
    // okay, it's a real location!
}

其次,您的else 永远无法工作,因为传感器需要时间来预热并获得位置 - 事实上,它们可能永远也无法获得位置。所以我可以很好地保证,在receivedLocation nil 的情况下,currentManager.location 也没有任何用处。 (有关如何使用位置管理器获取单个位置值的说明和指向示例代码的指针,请参阅my answer here。)

【讨论】:

  • 太好了,你搞定了。至于其他,receivedLocation 仅在按下按钮后才具有值。如果他们不按地图就去地图,这是为了解决这个问题。现在我唯一的问题是,如果我添加注释然后离开 MapView 并返回,注释就消失了。您不必回答,因为这是一个单独的问题,但如果您有时间,有什么办法可以解决这个问题?
  • 不,你是对的,这是一个单独的问题。请随时将您的其他问题作为一个单独的问题提出!更多的问题是好的。 (很好。)您会发现,Stack Overflow 非常适合单问题问题 - 我们刚刚使用的问题/评论/编辑/回答循环完美运行(如您所见!)。如果您评论中提出进一步的问题,事情就会失控。
  • 感谢您的帮助,马特!度过一个美妙的夜晚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
相关资源
最近更新 更多