首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将userLocation打印到textLabel

将userLocation打印到textLabel
EN

Stack Overflow用户
提问于 2019-02-11 14:51:37
回答 1查看 93关注 0票数 0

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

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

代码语言:javascript
复制
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)
}

要打印标签上的位置,请执行以下操作:

代码语言:javascript
复制
self.posizione.text = "latitude: " +  
String(location.coordinate.latitude) + ", longitude: " + 
String(location.coordinate.longitude)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-11 19:50:46

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

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

让你的类符合CLLocationManagerDelegateMKMapViewDelegate。在Info.plist文件中添加所需的权限(使用时的位置使用说明)。

在你的类声明下面插入:

代码语言:javascript
复制
    let locationManager = CLLocationManager()
    var scanLocation: String?

在你的viewDidLoad插件中:

代码语言:javascript
复制
        //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方法,如下所示:

代码语言:javascript
复制
    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!
                    }
                }
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54625271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档