- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试将 ObjC 中的旧应用程序转换为 Swift 作为练习,但遇到了一些问题。我在旧应用程序中的方式是建立 CLLocation 管理器,然后我将使用:
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation]
它会自动调用:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}
从那里我可以提取我需要的所有信息。但是在 swift 中,这个方法没有自动完成,我不知道如何重现它。文档说
startUpdatingLocation()
仍将由委托(delegate)调用,但它不会发生。
这是我目前所拥有的:
import UIKit
import corelocation
class ViewController: UIViewController,CLLocationManagerDelegate{
@IBOutlet var gpsResult : UILabel
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locations)")
gpsResult.text = "success"
}
}
任何有关查找位置的帮助或指示将不胜感激。谢谢。
编辑:根据建议更新,但仍然无效
EDIT2:似乎是一些错误不允许该方法在 ViewController 中正常工作
最佳答案
你错过了两件事。首先,您必须使用 requestAlwaysAuthorization
或 requestWhenInUseAuthorization()
请求许可。所以你的 viewDidLoad()
应该是这样的:
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
其次,编辑您的Info.plist
as indicated here .
关于cllocationmanager - Swift 中的 CLLocation Manager 获取用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24063798/
我在主线程上调用 -[CLLocationManager requestWhenInUseAuthorization]。我已经设置了我的委托(delegate),并在 info.plist 中为 NS
这是我的代码,显示 map 上当前位置的警报和蓝点: MapName.h #import #import #import @interface MapName : UIViewControlle
我怎样才能不出错地执行以下操作? locationManager.delegate = self 行表示它不能 Cannot assign value of type '(APPLocationMan
我需要一直跟踪用户位置(但不要耗尽电池电量)。 我了解在应用程序终止后获取更新的唯一方法是使用 startMonitoringSignificantLocationChanges。 来自苹果关于 st
在我的标题中放置 CLLocationManager *locationManager; @property (nonatomic, retain) CLLocationManager *locati
我正在按照几个教程中的描述实现 CLLocationManager。 一切工作正常,直到 LocationManager 收到第二次更新。然后就会发生内存泄漏。 Instruments 告诉我,泄漏的
我正在尝试获取 iOS 设备(特别是我的 iPhone)上的当前位置。 我正在使用this苹果的例子。 调用 stopUpdatingLocations 之前的超时时间是 60 秒。 当我将desir
是否可以在后台“挂起” cllocationmanager? 我试图停止管理器,然后在特定时间在后台重新启动它。这可能吗?我尝试停止管理器并启动 nstimer,但它不起作用,因为 nstimer 不
如果我有一个选项卡栏应用程序,并计划在不同的选项卡中使用核心位置,是否有一个好的通用位置来放置用于分配/初始化 CLLocationManager 的代码,并在调用 startUpdatingLoca
我有一个 Mac 应用程序,想要使用核心位置,但是,当我没有使用 wifi 但使用以太网电缆连接时,核心位置 (CLLocationManager) 报告操作无法完成。 确切的错误消息是 The op
我正在开发一个跟踪应用程序,它需要在 map 上显示跟踪路径。当我开始更新位置并且安装后没有移动设备时,locationManagar报告错误的位置更新, 我正在根据报告的位置点绘制路线, 另外当用户
从iOS9开始,我们在CCLocationManager中遇到了一些奇怪的问题。 iOS 7-8没有问题。怪异的位置会导致应用程序出错。该应用程序在开车时使用,我们有大约50位TestFlight测试
我正在使用 CLLocationManager要访问用户位置,以下委托(delegate)方法返回不准确的坐标 -(void)locationManager:(CLLocationManager *)
我的应用程序中有一个映射功能。它属于选项卡栏 Controller 选项卡之一。 我遇到的问题是该应用程序在启动后立即要求位置权限。 在用户真正进入应用程序的 map 部分之前,我不想用与位置相关的问
我有一个 iOS 应用程序,它是一个选项卡式应用程序,带有 3 个 View Controller ,所有这些都需要知道手机何时进入特定地理区域。 我们监控的区域是在运行时通过 Web 界面提供的,因
当我尝试尝试 LBS 应用程序时,我遇到了问题。在模拟器中测试时,它似乎工作。然后,我在 iPhone 上试了一下。它没有按预期工作。 当我启动应用程序时,它显示了我的位置纬度、经度、距离等所有信息。
我有两个不同的类,其中之一是 LocationService,仅用于确定位置。问题是我不知道为什么它不更新坐标变量。 View Controller : import UIKit class View
我是 swift 新手 - 目前,当我第一次通过 override func awakeFromNib() 在 init 上调用 setupLocationManager() 时,我的当前位置仅在控制
我正在阅读 application:didFinishLaunchingWithOptions:方法并从 NSHipster 中发现以下代码: @import CoreLocation; @inter
位置管理器使用辅助 GPS 来确定用户的位置,使用来自 wifi 和蜂窝适配器的信息来提高返回位置的准确性和电池效率。但是,该文档没有明确说明为此使用 wifi/手机信号信息是否涉及任何可能消耗用户手
我是一名优秀的程序员,十分优秀!