简体   繁体   English

使用Swift CoreLocation获取经度和纬度

[英]Swift CoreLocation to get latitude and longitude

I am trying to use CoreLocation to get the latitude and longitude coordinates in iOS 8. After looking through a lot of posts, I am still unsure how to go about doing this. 我正在尝试使用CoreLocation在iOS 8中获取经度和纬度坐标。查看了很多帖子之后,我仍然不确定如何执行此操作。 Also, I would appreciate a brief explanation of the locationManager() methods as I don't understand what role they play in obtaining the location. 另外,我希望对locationManager()方法进行简要说明,因为我不了解它们在获取位置中所起的作用。 Do I ever need to call these methods? 我是否需要调用这些方法? Because I never saw them called in any examples. 因为我从未在任何示例中看到它们的调用。

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()

After the next line, the app crashes and I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value. 在下一行之后,应用程序崩溃,并且出现错误:致命错误:在展开Optional值时意外发现nil。 I think this means that ourLocation never actually gets set. 我认为这意味着ourLocation从未真正设置过。 (Note that I am casting to float because I need to store the location as a float on my server) (请注意,由于我需要将该位置存储为浮动内容,因此我将其强制转换为浮动内容)

    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()
    }
}

Note that I have updated my plist file appropriately! 请注意,我已经适当地更新了我的plist文件! Location is turned on in the simulator. 在模拟器中打开了位置信息。

Check if the values you are passing to variable are nil or not 检查您传递给变量的值是否为nil

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

Also check for longitude. 同时检查经度。

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")
        }

    }


}

The location property is an implicitly unwrapped CLLocation optional, and it is likely nil. location属性是一个隐式解包的CLLocation可选,它可能为nil。 You should never access members of implicitly unwrapped optional unless you know it is not nil. 除非您知道它不是nil,否则永远不要访问隐式展开的可选成员。

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

相关问题 Swift 2:iOS 8+如何使用CoreLocation存储纬度/经度并停止定位 - Swift 2 : iOS 8+ How to store latitude/longitude with CoreLocation and stop locating CoreLocation获取距坐标一定距离和方向的纬度和经度 - CoreLocation get latitude and longitude at a certain distance and direction from coordinate CoreLocation ::获取纬度经度坐标 - CoreLocation :: Getting Latitude Longitude Coordinates 如何获取经度和纬度然后将其存储在变量中? 斯威夫特 - How to get the Longitude and Latitude then store it on a Variable? on Swift 是否可以使用CoreLocation / MapKit查找纬度/经度附近的地址? - Is it possible to use CoreLocation/MapKit to find addresses near a Latitude/Longitude? Swift:获取用户的位置坐标作为纬度和经度? - Swift: Get user's location coordinates as latitude and longitude? 如何使用 CLLocationManager-Swift 获取当前经度和纬度 - How to get current longitude and latitude using CLLocationManager-Swift 我可以从 Swift 中的地址获取用户的经度和纬度吗? - Can I get the user longitude and latitude from their address in Swift? 无法通过当前的经纬度获得城市名称 - Unable to get city name by current latitude and longitude in swift 如何在 swift 中获取 Google 位置标记纬度和经度 - How to get Google places marker latitude and longitude in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM