Swift开发教程--如何通过定位来获取经纬度

Swift开发教程--如何通过定位来获取经纬度,第1张

概述首先,定义和继承协议CLLocationManagerDelegate var locManager:CLLocationManager?; 然后调用:         self.locManager = CLLocationManager();//[[CLLocationManager alloc] init];         self.locManager!.delegate = self;

首先,定义和继承协议CLLocationManagerDelegate

var locManager:CLLocationManager?;


然后调用:

self.locManager = CLLocationManager();//[[CLLocationManager alloc] init];

self.locManager!.delegate = self;

self.locManager!.desiredAccuracy = kCLLocationAccuracyBest;

self.locManager!.distanceFilter = 5.0;

//设置定位权限 ios8有意义

self.locManager?.requestWhenInUseAuthorization();//前台定位

self.locManager?.startUpdatingLocation();


最后,根据回调来获取经纬度

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

println("经度:%g",newLocation.coordinate.latitude);

println("纬度:%g",newLocation.coordinate.longitude);

}

func locationManager(manager: CLLocationManager!,dIDFailWithError error: NSError!) {

println("locationManager error");

}


还有一点,别忘了在info.pList里,加一项:

NSLocationWhenInUseUsageDescription,设为YES

总结

以上是内存溢出为你收集整理的Swift开发教程--如何通过定位来获取经纬度全部内容,希望文章能够帮你解决Swift开发教程--如何通过定位来获取经纬度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085813.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存