打印用户位置来为textLabel

问题描述 投票:0回答:1

我尝试打印的用户位置到为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)

但可悲的是标签就继续空...

ios swift memory-management cllocationmanager
1个回答
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!
                    }
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.