我想在ios8中找到位置的纬度和经度,并将其显示在标签中。我添加了" NSLocationWhenInUseUsageDescription" info.plist中的属性。我已打开此特定应用的位置设置。但即使这样,它也没有更新标签。没有日志也正常工作。我的代码如下: -
@IBAction func clicked(sender: UIButton) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let locat = locations[0] as! CLLocation
latitude.text = String(stringInterpolationSegment: locat.coordinate.latitude)
longitude.text = String(stringInterpolationSegment: locat.coordinate.longitude)
}
答案 0 :(得分:1)
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
您的位置:[AnyObject]!实际上是[CLLocation]只是获取它的最后一个对象并使用CLLocation的坐标属性。
将 CoreLocation.framework 添加到BuildPhases - >链接二进制文件库
将 CoreLocation 导入您的类 - 可能是ViewController.swift 将 CLLocationManagerDelegate 添加到类的解除 将 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription 添加到plist 初始位置管理员:
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
get User Location By:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var userLocation:CLLocation = locations[0] as! CLLocation
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
//Do What ever you want with it
}
即在你的plist文件中添加。,
NSLocationWhenInUseUsageDescription = Request permission to use location service when the apps is in background.
创建名为“Location.GPX”的新文件
- >添加静态Lat Long用于测试目的。
如下所示
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="23.0271480" lon="72.5085160">
<name>Ahmedabad</name>
</wpt>
</gpx>
然后
- &GT;转到Xcode - &gt;编辑方案 - &gt;再次选择Location.GPX文件和Rubn项目..
答案 1 :(得分:0)
除了Mehul提供的解决方案之外,还要记住的另一件事是不要引用您的CLLocationManger()对象。
func locationManager(manager:CLLocationManager!,didUpdateLocations locations:[AnyObject]!)
这是异步的,如果你松开对你的lCLLocationManger()对象的引用,那么它就不会被调用,即使在完成所有事情之后你也不会获得该位置。