如何在 Swift iOS 编程中获取某人的当前位置?
How to get someones current location in Swift iOS Programming?
我正在尝试弄清楚如何使用 SWIFT 为 XCODE 中的 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。
任何建议都很好
在您的 Info.plist
中添加:
或者您可以在 Info.plist
中添加这样的内容:
<key>NSLocationAlwaysUsageDescription</key>
<string>To get current location</string>
这会要求您允许定位服务。
希望对您有所帮助。
我正在尝试弄清楚如何使用 SWIFT 为 XCODE 中的 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。
任何建议都很好
在您的 Info.plist
中添加:
或者您可以在 Info.plist
中添加这样的内容:
<key>NSLocationAlwaysUsageDescription</key>
<string>To get current location</string>
这会要求您允许定位服务。
希望对您有所帮助。