将userLocation打印到textLabel

时间:2019-02-11 06:51:37

标签: ios swift memory-management cllocationmanager

我尝试将userLocation打印到textLabel上,但是我不想要确切的经度和纬度,例如,我只想要用户当前位置所在的街道名称,甚至只是提示代码,你知道我的意思吗?

我使用以下代码来获取用户位置:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    mapView.showsUserLocation = true
if CLLocationManager.locationServicesEnabled() == true {

        if CLLocationManager.authorizationStatus() == .restricted || CLLocationManager.authorizationStatus() == .denied ||  CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    } else {
        print("PLease turn on location services or GPS")
    }
 }

 //MARK:- CLLocationManager Delegates
func locationManager(_ manager: CLLocationManager,       
 didUpdateLocations locations: [CLLocation]) {
    self.locationManager.stopUpdatingLocation()
    let region = MKCoordinateRegion(center:  
 CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, 
 longitude: locations[0].coordinate.longitude), span: 
   MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
      //self.posizione.text = "latitude: " +  
      //String(location.coordinate.latitude) + ", longitude: " + 
     //String(location.coordinate.longitude)
    self.mapView.setRegion(region, animated: true)
}

要在我尝试过的标签上打印位置:

self.posizione.text = "latitude: " +  
String(location.coordinate.latitude) + ", longitude: " + 
String(location.coordinate.longitude)

但遗憾的是,标签保持空白...

1 个答案:

答案 0 :(得分:1)

因此,如果我对您的理解正确,那么您就不需要经度和纬度,而是需要用户当前所在的城市名称和街道地址。

要获取这些,请执行以下操作:

使您的班级符合CLLocationManagerDelegateMKMapViewDelegate。 在Info.plist文件(使用说明中的位置,使用说明)中添加所需的权限。

只需在类声明下面插入:

    let locationManager = CLLocationManager()
    var scanLocation: String?

在您的viewDidLoad中插入:

        //setting up location manager
    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    //setting up the map view
    mapView.delegate = self
    mapView.mapType = .standard
    mapView.isZoomEnabled = true
    mapView.isScrollEnabled = true

然后按如下所示调用didUpdateLocations方法:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let lastLocation = locations.last {
        let geoCoder = CLGeocoder()

        //this is where you deal with the mapView to display current location
        let center = CLLocationCoordinate2D(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = manager.location!.coordinate
        annotation.title = "I know where you are"
        annotation.subtitle = "your current location"
        mapView.addAnnotation(annotation)

        //This is where you get the current street and city name
        geoCoder.reverseGeocodeLocation(lastLocation) { (placeMarks, error) in
            if error == nil {
                if let firstLocation = placeMarks?[0] {
                    self.locationManager.stopUpdatingLocation()

                    if let cityName = firstLocation.locality,
                        let street = firstLocation.thoroughfare {

                    self.scanLocation = "\(street), \(cityName)"
                    print("This is the current city name", cityName)
                    print("this is the current street address", street)

                    self.posizione.text = self.scanLocation!
                    }
                }
            }
        }
    }
}