CLLocation'坐标'不可用

时间:2014-10-11 13:39:04

标签: ios swift cllocation

我在使用从CLLocation对象获取lat和long时遇到问题。

奇怪的是,直到昨天我才能通过执行以下操作检索lat和

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    var location: AnyObject? = locations.last

    if let location: AnyObject = location {
        //The println illustrates how I would retrieve the lat and long, i.e. by calling
        //location.coordinate.latitude.

        println(location.coordinate.latitude)

        locationDelegate?.updateLabels(location)
        self.locationsArray.append(location)
    }
}

现在已停止工作,它会抛出以下编译器错误:

enter image description here

如果我删除.coordinate来电并直接在.latitude对象上致电location,则会返回nil。关于这里发生的事情,我有点迷失......

如果我println(location)我得到以下输出:

<+56.92239472,+1.47020887> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/11/14, 2:37:59 PM British Summer Time

任何人都可以帮助我了解最新情况以及如何重新检索lat和很长时间吗?

3 个答案:

答案 0 :(得分:1)

使用func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!)代替上述委托方法,然后调用newLocation.coordinate.latitude似乎有效。

答案 1 :(得分:0)

我有这个问题。正确的解决方案是在取消引用坐标属性之前将位置对象强制转换为CLLocation。以下代码应该可以工作,虽然我没有实际测试过它。此外,可能有一种更优雅的方式来进行演员表演;我自己还在学习Swift。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

   var location: AnyObject? = locations.last

   if let location: AnyObject = location {
       //The println illustrates how I would retrieve the lat and long, i.e. by calling
       //location.coordinate.latitude.
       var loc = location as CLLocation
       println(loc.coordinate.latitude)

       locationDelegate?.updateLabels(loc)
       self.locationsArray.append(loc)
   }
}

答案 2 :(得分:0)

实现此方法的最佳方法是定义坐标。 (我假设您尝试获取当前位置)

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            if !isCurrentLocation {
                return
            }

            isCurrentLocation = false

            let location = locations.last

// here define the center variable to hold the coordinates for reuse!
            let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: DesiredLocation.latitude, longitudeDelta: DesiredLocation.longitude))


            self.mapView.setRegion(region, animated: true)


    }

所有设置!