【问题标题】:How to get only current location coordinates if "allow once" in swift如果在swift中“允许一次”,如何仅获取当前位置坐标
【发布时间】:2020-08-14 05:41:58
【问题描述】:

我在info.plist中添加了三个权限

弹出窗口带有三个选项..这里

1)如果我点击"allow while using app",那么我需要用当前位置地址自动填充文本字段,但是

  1. 如果我点击"allow once",那么我不需要用当前位置自动填充文本字段

在我的代码中既允许一次,也允许在使用条件时文本字段自动填充当前位置地址:

 class MapViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var latitude: String?
var logitude: String?
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var text3: UITextField!
let annotation = MKPointAnnotation()

@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self

        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let _: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    
    let userLocation :CLLocation = locations[0] as CLLocation
    latitude = "\(userLocation.coordinate.latitude)"
    logitude = "\(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]
          
            let placemarkDictonary: NSDictionary=placemark.addressDictionary as! NSDictionary
            self.text1.text=placemarkDictonary["ZIP"] as? String
            self.text2.text=placemarkDictonary["City"] as? String
            self.text3.text=placemarkDictonary["Street"] as? String
        }
    }
    
    let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
    mapView.setRegion(region, animated: true)
    let myAnnotation: MKPointAnnotation = MKPointAnnotation()
    myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    myAnnotation.title = "Current location"
    mapView.addAnnotation(myAnnotation)
    locationManager.stopUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
          switch status {
         
          case .denied:
              print("User hates you!!")

          case .authorizedWhenInUse:
              
               let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
               self.navigationController?.pushViewController(viewController, animated: true);

          case .authorizedAlways:
              
              let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FinalViewController") as! FinalViewController
              self.navigationController?.pushViewController(viewController, animated: true);

          }
      }  
}

实际上,当我单击"allow once" 时,我只需要当前位置坐标,无需在文本字段中自动填充地址。该怎么做?

请帮忙写代码。

【问题讨论】:

  • 有什么办法可以这样做..或不..请给我一些建议

标签: swift mkmapview


【解决方案1】:

When In UseAllow Once 是相同的权限,但有效期不同。在位置权限提示上点击Allow Once 将为应用程序的当前会话启用When In Use 权限,即。直到用户强制终止应用程序或应用程序长时间停留在后台。如果用户点击Allow Once 权限,您将获得所有位置更新,就像他授予When In Use 权限一样。但是如果用户杀死应用并重新启动它,系统会再次显示位置权限提示。

因此,您将无法通过检查locationManager(_ manager: CLLocationManager, didChangeAuthorization 返回的状态来尝试执行此操作,因为这两种权限的状态均为authorizedWhenInUse。你无法区分它们。

Documentation

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多