MapKit 中的中心位置在 UiLabel 中不可见
Posted
技术标签:
【中文标题】MapKit 中的中心位置在 UiLabel 中不可见【英文标题】:The centre locations in MapKit is not visible in the UiLabel 【发布时间】:2018-11-02 07:53:46 【问题描述】:无法在标签中显示地图的中心位置!
导入 UIKit 导入 MapKit 导入核心位置
类视图控制器:UIViewController、CLLocationManagerDelegate、MKMapViewDelegate
@IBOutlet weak var myMapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!
let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
var previousLocation: CLLocation?
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
checkLocationServices()
func setupLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
func centerViewOnUserLocation()
if let location = locationManager.location?.coordinate
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
myMapView.setRegion(region, animated: true)
// the Location service
func checkLocationServices()
if CLLocationManager.locationServicesEnabled()
setupLocationManager()
checkLocationAuthorisation()
else
//show user to turn ON the services
func checkLocationAuthorisation()
switch CLLocationManager.authorizationStatus()
case .authorizedWhenInUse:
// do the stuff
startTrackingLocation()
case .denied:
//Alert to turn ON the permissions
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// Alert to show what's up
break
case .authorizedAlways:
break
func startTrackingLocation()
myMapView.showsUserLocation = true
centerViewOnUserLocation()
locationManager.startUpdatingLocation()
previousLocation = getCenterLocation(for: myMapView)
func getCenterLocation(for mapView: MKMapView) -> CLLocation
let latitude = mapView.centerCoordinate.latitude
let longitude = mapView.centerCoordinate.longitude
return CLLocation(latitude: latitude, longitude: longitude)
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
// We'll be back
checkLocationAuthorisation()
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool)
let center = getCenterLocation(for: myMapView)
let geoCoder = CLGeocoder()
guard let previousLocation = self.previousLocation else return
guard center.distance(from: previousLocation) > 50 else return
self.previousLocation = center
geoCoder.reverseGeocodeLocation(center) [weak self] (placemarks, error) in
guard let self = self else return
if let _ = error
//TODO: Show alert informing the user
return
guard let placemark = placemarks?.first else
//TODO: Show alert informing the user
return
let streetNumber = placemark.subThoroughfare ?? ""
let streetName = placemark.subThoroughfare ?? ""
DispatchQueue.main.async
self.addressLabel.text = "\(streetNumber) \(streetName)"
@IBAction func satelliteView(_ sender: Any)
myMapView.mapType = MKMapType.satellite
@IBAction func hybridView(_ sender: Any)
myMapView.mapType = MKMapType.hybrid
@IBAction func standardView(_ sender: Any)
myMapView.mapType = MKMapType.standard
@IBAction func findDirections(_ sender: Any)
应用程序中的标签应该显示地图的中心位置(图钉)。但是不知道哪里出错了!
【问题讨论】:
你确定调用了 regionDidChangeAnimated 方法吗?因为这个方法告诉代理,地图视图显示的区域刚刚发生了变化。 您是否将您的 UIViewController 设置为 MKMapViewDelegate? myMapView.delegate = self 是的...但无法获得所需的结果。 :( 【参考方案1】:更改了 getCenterlocation 功能,它的工作正常! `func getCenterLocation(for mapView: MKMapView) -> CLLocation
let latitude=mapView.centerCoordinate.latitude
let longitude=mapView.centerCoordinate.longitude
return CLLocation(latitude:latitude, longitude:longitude)
func getCenterLocation(for myMapView: MKMapView) -> CLLocation
let latitude=myMapView.centerCoordinate.latitude
let longitude=myMapView.centerCoordinate.longitude
return CLLocation(latitude:latitude, longitude:longitude)
`
【讨论】:
以上是关于MapKit 中的中心位置在 UiLabel 中不可见的主要内容,如果未能解决你的问题,请参考以下文章