【问题标题】:SwiftUI - Get City/locality information from user's locationSwiftUI - 从用户的位置获取城市/地区信息
【发布时间】:2020-10-23 12:07:08
【问题描述】:

我目前能够确定用户的纬度/经度并访问坐标。我将如何对坐标进行反向地理编码以确定用户当前所在的城市以便我可以显示它?

这是我当前的 LocationManager 文件

import Foundation
import MapKit

class LocationManager: NSObject, ObservableObject {

private let locationManager = CLLocationManager()
@Published var location: CLLocation? = nil

override init() {
    super.init()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = kCLDistanceFilterNone
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    }
}


class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
}

// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        print("notDetermined")
        manager.requestWhenInUseAuthorization()
    default:
        break
        }
    }
}

extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
    guard let location = locations.last else {
        return
    }
    
    self.location = location
}


}

我想将城市信息传递给我的一个视图,并以文本元素的形式输出城市。即“探索奥兰多”

【问题讨论】:

标签: swiftui core-location


【解决方案1】:

您可以为此使用 CLGeocoder

// Add below code to get address for touch coordinates.
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE)
        geoCoder.reverseGeocodeLocation(location, completionHandler:
            {
                placemarks, error -> Void in

                // Place details
                guard let placeMark = placemarks?.first else { return }

                // Location name
                if let locationName = placeMark.location {
                    print(locationName)
                }
                // Street address
                if let street = placeMark.thoroughfare {
                    print(street)
                }
                // City
                if let city = placeMark.subAdministrativeArea {
                    print(city)
                }
                // Zip code
                if let zip = placeMark.isoCountryCode {
                    print(zip)
                }
                // Country
                if let country = placeMark.country {
                    print(country)
                }
        })

感谢Convert coordinates to City name?

【讨论】:

  • 不确定这是否会有所不同,但我不会尝试根据用户与地图的交互来获取坐标。我想在应用启动时获取位置信息只是为了显示个性化。就像它是一个天气应用程序,当应用程序启动时文本会显示“检查当地天气(获取坐标并显示用户当前的城市)。
  • @JayHamilton 您不需要用户与地图进行交互,只需将此方法添加到您的 didUpdateLocations 中并使用您在那里获得的用户位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
相关资源
最近更新 更多