繁体   English   中英

使用Swift CoreLocation获取经度和纬度

[英]Swift CoreLocation to get latitude and longitude

我正在尝试使用CoreLocation在iOS 8中获取经度和纬度坐标。查看了很多帖子之后,我仍然不确定如何执行此操作。 另外,我希望对locationManager()方法进行简要说明,因为我不了解它们在获取位置中所起的作用。 我是否需要调用这些方法? 因为我从未在任何示例中看到它们的调用。

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationMan = CLLocationManager()
var ourLocation: CLLocation!

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.locationMan.delegate = self
    self.locationMan.desiredAccuracy = kCLLocationAccuracyBest
    self.locationMan.requestWhenInUseAuthorization()
    self.locationMan.startUpdatingLocation()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations : [AnyObject]!){
    locationMan.stopUpdatingLocation()
    ourLocation = locations[0] as! CLLocation
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Error: " + error.localizedDescription)
}

@IBAction func button(sender: AnyObject) {
    var testClass:item = item()

在下一行之后,应用程序崩溃,并且出现错误:致命错误:在展开Optional值时意外发现nil。 我认为这意味着ourLocation从未真正设置过。 (请注意,由于我需要将该位置存储为浮动内容,因此我将其强制转换为浮动内容)

    testClass.lat = Float(ourLocation.coordinate.latitude)
    println(testClass.lat)
    testClass.long = Float(ourLocation.coordinate.longitude)
    println(testClass.long)
    testClass.name = "Nice"
    testClass.description = "Wow this is pretty cool"
    testClass.postItemToServer()
    }
}

请注意,我已经适当地更新了我的plist文件! 在模拟器中打开了位置信息。

检查您传递给变量的值是否为nil

if  ourLocation.coordinate.latitude != nil
{
  testClass.lat = Float(ourLocation.coordinate.latitude)
  println(testClass.lat)
}

同时检查经度。

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

    var manager:CLLocationManager!
    var location : CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        self.manager = CLLocationManager()
        self.manager.delegate = self;
        self.manager.desiredAccuracy = kCLLocationAccuracyBest
        self.manager.requestWhenInUseAuthorization()
        self.manager.startUpdatingLocation()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations : [AnyObject]!){
        manager?.stopUpdatingLocation()
        self.location = locations[0] as CLLocation
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("Error: " + error.localizedDescription)
    }

    @IBAction func button(sender: AnyObject) {

        //check if location is not nil
        if self.location != nil {

            //assign your loaction values here
            let lat = Float(location.coordinate.latitude)
            let long = Float(location.coordinate.longitude)

        } else {
            println("locationManager.location is nil")
        }

    }


}

location属性是一个隐式解包的CLLocation可选,它可能为nil。 除非您知道它不是nil,否则永远不要访问隐式展开的可选成员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM