首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationManager问题

CLLocationManager问题
EN

Stack Overflow用户
提问于 2016-09-04 18:33:58
回答 1查看 182关注 0票数 0

我在斯威夫特学习关于用户本地化的知识。我试图在控制台上打印本地化(稍后我将使用它作为标签上的信息,所以我想检查它是否有效),但我不知道为什么它什么也不打印。即使删除到字符串的转换,并只留下打印任何东西,它仍然不工作。请帮帮忙。

是的,我添加了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

代码语言:javascript
复制
import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()
    var myPosition = CLLocationCoordinate2D()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {

        print("Got location: \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")

        myPosition = newLocation.coordinate

        locationManager.stopUpdatingLocation()
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-04 19:16:23

didUpdateToLocationCLLocationManagerDelegate过时的方法。它在10.6版及更早版本中提供。相反,使用didUpdateLocations,它将按最近的时间顺序返回所有location对象的数组。然后访问最新的位置,获取返回数组中的最后一个对象。

所以试试这个

代码语言:javascript
复制
 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

          var latestLocation: CLLocation = locations.last;
         print("Got location: \(latestLocation.coordinate.latitude), \(latestLocation.coordinate.longitude)")

    }

告诉我事情进展如何。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39320233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档