【问题标题】:Print userLocation to textLabel将 userLocation 打印到 textLabel
【发布时间】:2019-02-11 06:51:36
【问题描述】:

我尝试将 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)

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

【问题讨论】:

  • 在标签上打印位置的代码在哪里?
  • 在最后的代码sn -p @OOPer
  • 我的意思是你班上的代码 sn-p 存在哪里?
  • 我在我的 LocationManager 中使用了它,在 self.mapView.setRegion 之前的一行
  • 那么您最好更新您的问题并包含您使用的确切代码以获得更好的响应。所有这些事情都可能会影响。

标签: ios swift memory-management cllocationmanager


【解决方案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!
                    }
                }
            }
        }
    }
}

【讨论】:

  • 感谢您的回答,您的代码似乎运行良好,它会在标签上打印街道名称,但它没有给我地图上显示的任何位置,我是否遗漏了什么?
  • @J.Doe 很抱歉,您的问题并不清楚(至少对我而言)您还想在 MKMapView 上显示位置,但我再次查看您的代码看到你实现了 mapView。这样做很容易,我上面的答案只需要几行额外的代码。这个 SO 答案肯定会帮助你:stackoverflow.com/questions/25449469/… 试试这个,如果你不知道怎么做,我会编辑我的答案以包含这个。
  • 不,不用担心哈哈哈我也没有明确要求,我只是想问一下,因为看起来你真的知道你在说什么。但是为了解决这个问题,我试过了,但是得到 Value of type '[CLLocation]' has no member 'coordinate' 作为错误...我也将它添加到 locationManagerFunction - 我没有'不知道那里的错误是什么,所以如果你能在你的答案中包含你的解决方案,那就太好了:)
  • 这太棒了,即使有一个标记哈哈 - 非常感谢你,你已经是本周的人物了
  • @J.Doe 我很高兴它有帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2012-03-17
  • 1970-01-01
相关资源
最近更新 更多