ios - 将 userLocation 打印到 textLabel

标签 ios swift memory-management cllocationmanager

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

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

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)

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

最佳答案

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

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

使您的类符合 CLLocationManagerDelegateMKMapViewDelegate。 在Info.plist文件中添加需要的权限(Location when in use usage description)。

就在你的类声明下面插入:

    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!
                    }
                }
            }
        }
    }
}

关于ios - 将 userLocation 打印到 textLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625271/

相关文章:

ios - 使 NSDate 等于 NSString

ios - 将值传递给 WKWebView Swift

ios:Socket.io 中的 Json

memory-management - 在不调整数组大小的情况下填充任意大小的向量的惯用方法?

iphone - 消除由于返回创建的变量而导致的内存泄漏

c++ - 修改指针后C++中数组的释放器

iphone - UITableView - 扩展单元格

iphone - 如何将相同的 UIButton 添加到 iphone 中所有 View Controller 的导航栏?

Swift新手基本功能

ios - 在 iOS Swift 中设置提醒