如何使用CLLocationManager-Swift获取当前经度和纬度 [英] How to get current longitude and latitude using CLLocationManager-Swift

查看:141
本文介绍了如何使用CLLocationManager-Swift获取当前经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Swift获取位置的当前经度和纬度,并通过标签显示它们。我尝试执行此操作,但标签上没有任何显示。

I want to get the current longitude and latitude of a location using Swift and display them via labels. I tried to do this but nothing displays on the labels.

import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet weak var longitude: UILabel!
    @IBOutlet weak var latitude: UILabel!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        if (CLLocationManager.locationServicesEnabled()) {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        } else {
            println("Location services are not enabled");
        }
    }

    // MARK: - CoreLocation Delegate Methods

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
         locationManager.stopUpdatingLocation()
         removeLoadingView()
         if (error) != nil {
             print(error)
          }
     }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as CLLocation
        var coord = locationObj.coordinate
        longitude.text = coord.longitude
        latitude.text = coord.latitude
        longitude.text = "\(coord.longitude)"
        latitude.text = "\(coord.latitude)"
    }
}


推荐答案

恕我直言,当您寻找的解决方案漂亮时,您就使代码复杂化了

IMHO, you are over complicating your code when the solution you are looking is pretty simple.

我已经使用以下代码完成了此操作:

I have done it by using the following code:

首先创建 CLLocationManager 和请求授权

var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()

然后检查用户是否允许授权。

then check if the user allowed authorization.

var currentLocation: CLLocation!

if 
   CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
   CLLocationManager.authorizationStatus() ==  .authorizedAlways
{         
    currentLocation = locManager.location        
}

使用它就可以了

label1.text = "\(currentLocation.coordinate.longitude)"
label2.text = "\(currentLocation.coordinate.latitude)"

您将它们设置为 label.text 的想法是正确的,但是我能想到的唯一原因是用户未给予您许可,这就是为什么您当前的位置数据将为零。

Your idea of setting them to the label.text is correct, however the only reason I can think of is that the user is not giving you permission and that is why your current Location data will be nil.

但是您需要调试并告诉我们。
也不需要 CLLocationManagerDelegate

However you would need to debug and tell us that. Also the CLLocationManagerDelegate is not necessary.

希望这会有所帮助。问问是否有疑问。

Hopefully this helps. Ask away if you have doubts.

这篇关于如何使用CLLocationManager-Swift获取当前经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆