将 userLocation 打印到 textLabel
Print userLocation to textLabel
我尝试将 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)
但遗憾的是标签一直保持空白...
因此,如果我的理解正确的话,您不需要经度和纬度,而是用户当前所在的城市名称和街道地址。
要获得这些,请执行以下操作:
让你的 class 符合 CLLocationManagerDelegate
和 MKMapViewDelegate
。
在Info.plist文件中添加需要的权限(Location when in use usage description)。
就在您的 class 声明下方插入:
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!
}
}
}
}
}
}
我尝试将 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)
但遗憾的是标签一直保持空白...
因此,如果我的理解正确的话,您不需要经度和纬度,而是用户当前所在的城市名称和街道地址。
要获得这些,请执行以下操作:
让你的 class 符合 CLLocationManagerDelegate
和 MKMapViewDelegate
。
在Info.plist文件中添加需要的权限(Location when in use usage description)。
就在您的 class 声明下方插入:
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!
}
}
}
}
}
}