如何存储用户的当前位置,并在地图上显示该位置?
我可以在地图上显示预定义的坐标,只是不知道如何从设备接收信息。
我也知道我必须添加一些项目到Plist。我该怎么做呢?
如何存储用户的当前位置,并在地图上显示该位置?
我可以在地图上显示预定义的坐标,只是不知道如何从设备接收信息。
我也知道我必须添加一些项目到Plist。我该怎么做呢?
要获取用户的当前位置,你需要声明:
let locationManager = CLLocationManager()
在viewDidLoad()中,你必须实例化CLLocationManager类,就像这样:
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
然后在CLLocationManagerDelegate方法中,你可以获得用户当前的位置坐标:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
在信息中。plist你必须添加NSLocationAlwaysUsageDescription 和你的自定义警报消息像;AppName(演示应用程序)想使用您的当前位置。
NSLocationWhenInUseUsageDescription =请求权限使用位置服务时,应用程序是在后台。在你的plist文件中。
如果这是工作,然后请投票的答案。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization();
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
else{
print("Location service disabled");
}
}
这是你的视图didload方法,在ViewController类中还包括mapStart更新方法,如下所示
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue : CLLocationCoordinate2D = manager.location.coordinate;
let span2 = MKCoordinateSpanMake(1, 1)
let long = locValue.longitude;
let lat = locValue.latitude;
print(long);
print(lat);
let loadlocation = CLLocationCoordinate2D(
latitude: lat, longitude: long
)
mapView.centerCoordinate = loadlocation;
locationManager.stopUpdatingLocation();
}
另外,不要忘记添加CoreLocation。框架和MapKit。项目中的框架(从XCode 7.2.1开始不再需要)
首先在项目中添加两个框架
1:地
2:共重定位(从XCode 7.2.1开始不再需要)
在类中定义
var manager:CLLocationManager!
var myLocations: [CLLocation] = []
然后在viewDidLoad方法中编写此代码
manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
//Setup our Map View
mapobj.showsUserLocation = true
不要忘记在plist文件中添加这两个值
1: NSLocationWhenInUseUsageDescription
2: NSLocationAlwaysUsageDescription
你应该做这些步骤:
add CoreLocation.framework to BuildPhases -> Link Binary With Libraries (no longer necessary as of XCode 7.2.1) import CoreLocation to your class - most likely ViewController.swift add CLLocationManagerDelegate to your class declaration add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to plist init location manager: locationManager = CLLocationManager() locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() get User Location By: func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let locValue:CLLocationCoordinate2D = manager.location!.coordinate print("locations = \(locValue.latitude) \(locValue.longitude)") }
更新到iOS 12.2与Swift 5
您必须在plist文件中添加以下隐私权限
<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>
我是这样的
在Swift 2.0中获取当前位置并在地图上显示
确保你已经在你的项目中添加了CoreLocation和MapKit框架(这在XCode 7.2.1中不需要)
import Foundation
import CoreLocation
import MapKit
class DiscoverViewController : UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad()
{
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}
}
这里是结果屏幕
首先导入Corelocation和MapKit库:
import MapKit
import CoreLocation
从CLLocationManagerDelegate继承到我们的类
class ViewController: UIViewController, CLLocationManagerDelegate
创建一个locationManager变量,这将是你的位置数据
var locationManager = CLLocationManager()
创建一个函数来获取位置信息,具体语法如下:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
在函数中为用户的当前位置创建一个常量
let userLocation:CLLocation = locations[0] as CLLocation // note that locations is same as the one in the function declaration
停止更新位置,这可以防止你的设备在移动时不断地改变窗口以使你的位置居中(如果你想让它发挥其他作用,你可以省略这个)
manager.stopUpdatingLocation()
从刚刚定义的userLocatin获取用户坐标:
let coordinations = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
定义你想要缩放的地图:
let span = MKCoordinateSpanMake(0.2,0.2) 将这两个组合起来得到region:
let region = MKCoordinateRegion(center: coordinations, span: span)//this basically tells your map where to look and where from what distance
现在设置区域,并选择是否希望它与动画一起去那里
mapView.setRegion(region, animated: true)
关闭函数 }
从你的按钮或者其他你想要设置locationManagerDeleget为self的方式
现在允许显示位置
指定精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
授权:
locationManager.requestWhenInUseAuthorization()
为了能够授权位置服务,您需要将这两行添加到您的plist
位置:
locationManager.startUpdatingLocation()
显示给用户:
mapView.showsUserLocation = true
这是我的完整代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func locateMe(sender: UIBarButtonItem) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let coordinations = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.2,0.2)
let region = MKCoordinateRegion(center: coordinations, span: span)
mapView.setRegion(region, animated: true)
}
}
// its with strongboard
@IBOutlet weak var mapView: MKMapView!
//12.9767415,77.6903967 - exact location latitude n longitude location
let cooridinate = CLLocationCoordinate2D(latitude: 12.9767415 , longitude: 77.6903967)
let spanDegree = MKCoordinateSpan(latitudeDelta: 0.2,longitudeDelta: 0.2)
let region = MKCoordinateRegion(center: cooridinate , span: spanDegree)
mapView.setRegion(region, animated: true)
斯威夫特3.0
如果你不想在地图中显示用户位置,而只是想将其存储在firebase或其他地方,那么请遵循以下步骤,
import MapKit
import CoreLocation
现在在你的VC上使用CLLocationManagerDelegate,你必须覆盖下面显示的最后三个方法。您可以看到requestLocation()方法如何使用这些方法获取当前用户位置。
class MyVc: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
isAuthorizedtoGetUserLocation()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
}
}
//if we have no permission to access user location, then ask user for permission.
func isAuthorizedtoGetUserLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse {
locationManager.requestWhenInUseAuthorization()
}
}
//this method will be called each time when a user change his location access preference.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("User allowed us to access location")
//do whatever init activities here.
}
}
//this method is called by the framework on locationManager.requestLocation();
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did location updates is called")
//store the user location here to firebase or somewhere
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Did location updates is called but failed getting location \(error)")
}
}
现在,一旦用户登录到您的应用程序,您就可以编写下面的调用。当requestLocation()被调用时,它将进一步调用上面的didUpdateLocations,您可以将位置存储到Firebase或其他任何地方。
if CLLocationManager.locationServicesEnabled() {
locationManager.requestLocation();
}
如果你正在使用GeoFire,那么在上面的didUpdateLocations方法中,你可以像下面那样存储位置
geoFire?.setLocation(locations.first, forKey: uid) where uid is the user id who logged in to the app. I think you will know how to get UID based on your app sign in implementation.
最后但并非最不重要的,去你的信息。plist并启用“Privacy -Location when in Use Usage Description.”
当您使用模拟器测试时,它总是给您一个自定义位置,您在模拟器中配置->调试->位置。
import CoreLocation
import UIKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status != .authorizedWhenInUse {return}
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
let locValue: CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
因为requestWhenInUseAuthorization的调用是异步的,所以应用程序在用户授予权限或拒绝它之后才调用locationManager函数。因此,在给定用户权限的情况下,在函数中放置位置获取代码是合适的。这是我找到的最好的教程。
像这样导入库:
import CoreLocation
设置委托:
CLLocationManagerDelegate
取如下变量:
var locationManager:CLLocationManager!
在viewDidLoad()上写这样漂亮的代码:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()
}
编写CLLocation委托方法:
//MARK: - location delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation :CLLocation = locations[0] as CLLocation
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
self.labelLat.text = "\(userLocation.coordinate.latitude)"
self.labelLongi.text = "\(userLocation.coordinate.longitude)"
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(userLocation) { (placemarks, error) in
if (error != nil){
print("error in reverseGeocode")
}
let placemark = placemarks! as [CLPlacemark]
if placemark.count>0{
let placemark = placemarks![0]
print(placemark.locality!)
print(placemark.administrativeArea!)
print(placemark.country!)
self.labelAdd.text = "\(placemark.locality!), \(placemark.administrativeArea!), \(placemark.country!)"
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error \(error)")
}
现在设置访问位置的权限,将这些键值添加到您的信息中。plist文件
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
100%工作,没有任何问题。测试
100%工作在iOS Swift 4: Parmar Sajjad
步骤1:进入GoogleDeveloper Api控制台并创建你的ApiKey
步骤2:Goto项目安装Cocoapods GoogleMaps pod
步骤3:Goto AppDelegate.swift导入GoogleMaps和
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("ApiKey")
return true
}
步骤4: 进口UIKit 进口GoogleMaps 类ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapview: UIView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManagerSetting()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManagerSetting() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationonMap()
self.locationManager.stopUpdatingLocation()
}
func showCurrentLocationonMap() {
let
cameraposition = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)! , longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 18)
let mapviewposition = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.mapview.frame.size.width, height: self.mapview.frame.size.height), camera: cameraposition)
mapviewposition.settings.myLocationButton = true
mapviewposition.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = cameraposition.target
marker.snippet = "Macczeb Technologies"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapviewposition
self.mapview.addSubview(mapviewposition)
}
}
第五步:打开信息。plist文件和添加下面的隐私-使用时的位置使用说明......在主故事板文件基名下面
第六步:运行
下面是一个适用于我的复制粘贴示例。
http://swiftdeveloperblog.com/code-examples/determine-users-current-location-example-in-swift/
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// manager.stopUpdatingLocation()
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
用法:
在类中定义字段
let getLocation = GetLocation()
通过简单的代码在类的函数中使用:
getLocation.run {
if let location = $0 {
print("location = \(location.coordinate.latitude) \(location.coordinate.longitude)")
} else {
print("Get Location failed \(getLocation.didFailWithError)")
}
}
类:
import CoreLocation
public class GetLocation: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
var locationCallback: ((CLLocation?) -> Void)!
var locationServicesEnabled = false
var didFailWithError: Error?
public func run(callback: @escaping (CLLocation?) -> Void) {
locationCallback = callback
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestWhenInUseAuthorization()
locationServicesEnabled = CLLocationManager.locationServicesEnabled()
if locationServicesEnabled { manager.startUpdatingLocation() }
else { locationCallback(nil) }
}
public func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
locationCallback(locations.last!)
manager.stopUpdatingLocation()
}
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
didFailWithError = error
locationCallback(nil)
manager.stopUpdatingLocation()
}
deinit {
manager.stopUpdatingLocation()
}
}
不要忘记在info.plist中添加“NSLocationWhenInUseUsageDescription”。
import Foundation
import CoreLocation
enum Result<T> {
case success(T)
case failure(Error)
}
final class LocationService: NSObject {
private let manager: CLLocationManager
init(manager: CLLocationManager = .init()) {
self.manager = manager
super.init()
manager.delegate = self
}
var newLocation: ((Result<CLLocation>) -> Void)?
var didChangeStatus: ((Bool) -> Void)?
var status: CLAuthorizationStatus {
return CLLocationManager.authorizationStatus()
}
func requestLocationAuthorization() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
manager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func getLocation() {
manager.requestLocation()
}
deinit {
manager.stopUpdatingLocation()
}
}
extension LocationService: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
newLocation?(.failure(error))
manager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.sorted(by: {$0.timestamp > $1.timestamp}).first {
newLocation?(.success(location))
}
manager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined, .restricted, .denied:
didChangeStatus?(false)
default:
didChangeStatus?(true)
}
}
}
需要在所需的ViewController中编写此代码。
//NOTE:: Add permission in info.plist::: NSLocationWhenInUseUsageDescription
let locationService = LocationService()
@IBAction func action_AllowButtonTapped(_ sender: Any) {
didTapAllow()
}
func didTapAllow() {
locationService.requestLocationAuthorization()
}
func getCurrentLocationCoordinates(){
locationService.newLocation = {result in
switch result {
case .success(let location):
print(location.coordinate.latitude, location.coordinate.longitude)
case .failure(let error):
assertionFailure("Error getting the users location \(error)")
}
}
}
func getCurrentLocationCoordinates() {
locationService.newLocation = { result in
switch result {
case .success(let location):
print(location.coordinate.latitude, location.coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
if error != nil {
print("Reverse geocoder failed with error" + (error?.localizedDescription)!)
return
}
if (placemarks?.count)! > 0 {
print("placemarks", placemarks!)
let pmark = placemarks?[0]
self.displayLocationInfo(pmark)
} else {
print("Problem with the data received from geocoder")
}
})
case .failure(let error):
assertionFailure("Error getting the users location \(error)")
}
}
}
之前所有的答案都没有正确地遵循正确的步骤。当设置CLLocationManager时,您不需要最初调用CLLocationManager. locationservicesenabled()或locationManager.requestWhenInUseAuthorization()。你不应该调用startUpdatingLocation(),直到你已经验证你有授权。
要开始,如果还没有完成,请添加所需的隐私设置。选择你的应用目标,然后转到信息选项卡。在“自定义iOS目标属性”部分右键单击并选择“添加行”。向下滚动到“隐私”条目,并选择其中之一:
"隐私-使用时的位置使用说明" "隐私-位置始终使用说明" 隐私-使用时的位置使用说明
这取决于你的应用程序的需求。对于这个值,确保你输入了一个句子,向用户解释为什么你的应用程序需要访问他们的位置。如果所提供的描述对用户没有用处,你的应用可能会被苹果拒绝。
然后使用以下(大量注释)代码作为示例,在视图控制器中获取用户的位置:
import UIKit
import CoreLocation
class ViewController: UIViewController {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Setup the location manager
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation // Or other desired accuracy
// That's it for the initial setup. Everything else is handled in the
// locationManagerDidChangeAuthorization delegate method.
}
}
extension ViewController: CLLocationManagerDelegate {
// This is called as soon as the location manager is setup (in viewDidLoad)
// This is called when the user responds to the privacy dialog
// This is called if the user changes the privacy setting in the Settings app
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .notDetermined:
// Request the appropriate authorization based on the needs of the app
manager.requestWhenInUseAuthorization()
// manager.requestAlwaysAuthorization()
case .restricted:
print("Sorry, restricted")
// Optional: Offer to take user to app's settings screen
case .denied:
print("Sorry, denied")
// Optional: Offer to take user to app's settings screen
case .authorizedAlways, .authorizedWhenInUse:
// The app has permission so start getting location updates
manager.startUpdatingLocation()
@unknown default:
print("Unknown status")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Received \(locations.count) locations")
// Some location updates can be invalid or have insufficient accuracy.
// Find the first location that has sufficient horizontal accuracy.
// If the manager's desiredAccuracy is one of kCLLocationAccuracyNearestTenMeters,
// kCLLocationAccuracyHundredMeters, kCLLocationAccuracyKilometer, or kCLLocationAccuracyThreeKilometers
// then you can use $0.horizontalAccuracy <= manager.desiredAccuracy. Otherwise enter the number of meters desired.
if let location = locations.first(where: { $0.horizontalAccuracy <= 40 }) {
print("Good location found: \(location)")
// Call the following if you don't need any more updates
manager.stopUpdatingLocation()
// Do something useful with the found location
// - show on a map
// - Reverse geocode to get the address
// - send to server
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location manager error: \(error)")
}
}