gpt4 book ai didi

ios - 将 userLocation 打印到 textLabel

转载 作者:行者123 更新时间:2023-11-28 13:50:54 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com