【发布时间】:2016-04-09 07:22:46
【问题描述】:
我正试图弄清楚如何在 XCODE 中使用 SWIFT 为 iOS 应用程序获取某人的当前位置。
我正在关注本教程 here,但由于某种原因,我的代码无法正常工作。我认为这是因为我使用了更新的 Xcode 版本控制。出于某种原因,我的代表有不同的参数,我不知道为什么,但是当我构建我的程序时,它甚至没有问我是否要允许定位服务。
这是我的代码
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization();
self.locationManager.startUpdatingLocation();
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// GPS STUFF
// UPDATE LOCATION
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in
if(ErrorType != nil)
{
print("Error: " + ErrorType!.localizedDescription);
return;
}
if(placemarks?.count > 0)
{
let pm = placemarks![0] ;
self.displayLocationInfo(pm);
}
}
}
// STOP UPDATING LOCATION
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation();
print(placemark.locality);
print(placemark.postalCode);
print(placemark.administrativeArea);
print(placemark.country);
}
另外,在 info.plist 中我还添加了 NSLocationWhenInUseUsageDescription。
任何建议都会很棒
【问题讨论】:
标签: ios xcode swift gps location