【问题标题】:Location Data from CoreLocation swift来自CoreLocation swift的位置数据
【发布时间】:2014-08-03 13:14:34
【问题描述】:

我正在尝试获取用户坐标,但我不知道如何检索它们?这是我在地图上查找他们位置的代码,我应该添加什么来获取经度/纬度的数值?它工作正常,我已将 requestAuthorization 密钥添加到 info.plist。我尝试使用 CLLocation 对象,但我似乎无法让它工作。

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var Map: MKMapView!


var locationMgr = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()


    locationMgr.delegate = self
    locationMgr = CLLocationManager()
    locationMgr.requestAlwaysAuthorization()
     locationMgr.startUpdatingLocation()
            Map.showsUserLocation = true


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

【问题讨论】:

  • 您必须实现此方法才能实际检索位置:- func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[])
  • 感谢您的快速回复!你有没有机会给我一些示例代码,它将位置打印到控制台? func 中的 println(locations) 似乎不起作用,我认为它不断返回 nil 值?我真的很陌生,这是我编码的第一周!
  • 谢谢!有用的链接 :) 我现在已经可以使用它了,并且也可以从视图控制器中使用

标签: ios swift coordinates core-location xcode6


【解决方案1】:

实现 CLLocationManager 委托协议方法,具体来说:

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

【讨论】:

  • 嘿!就像我刚刚对 Lyndsey 说的一样,你有没有机会给我一些示例代码,它将位置打印到控制台? func 中的 println(locations) 似乎不起作用,我认为它不断返回 nil 值?
  • 如果这是您编码的第一周,请首先使用 Apple 提供的 Objective-C 介绍材料(developer.apple.com/library/ios/referencelibrary/GettingStarted/…developer.apple.com/library/ios/referencelibrary/GettingStarted/…)。 Swift 仍处于测试阶段,因此我不建议使用 Swift 来学习编程。无论如何,要打印出 Swift 中的位置,请使用: println(\(locations))
  • 我正在学习它,因为我正在享受 x 代码的实际结果,而且 swift 比 obj C 直观得多。现在可以工作了 :)
  • Swift 不再处于测试阶段,并且已经足够稳定,可以在此时学习编码。
【解决方案2】:

您已初始化位置管理器两次。所以你在第一次初始化经理后设置的委托是零。所以在你的 viewDidLoad 方法中删除这个locationMgr = CLLocationManager()。它应该工作

【讨论】:

    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多