ios - 无法在 MKMapview Swift 上显示注释引脚
问题描述
我正在尝试显示当前位置的注释图钉。为此,我编写了以下代码
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mapView.layer.cornerRadius = 8.0
imageView.layer.cornerRadius = 8.0
//mapview settings
mapView.mapType = MKMapType.standard
mapView.isZoomEnabled = true
mapView.isZoomEnabled = true
// Or, if needed, we can position map in the center of the view
mapView.center = view.center
mapView.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
determineCurrentLocation()
}
func determineCurrentLocation()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
//locationManager.startUpdatingHeading()
locationManager.startUpdatingLocation()
}
}
//Mapview Delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
//manager.stopUpdatingLocation()
let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
// Drop a pin at user's Current Location
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
myAnnotation.title = "Current location"
mapView.addAnnotation(myAnnotation)
}
func locationManager(manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
但是,它没有在地图视图中显示。有什么建议么?
解决方案
推荐阅读
- css - How can I nest a selector with ::after psudo class using SCSS?
- html - 在源代码中换行时 {% url ... %} 中的 URL 错误
- java - 如何生成列表地图的地图(地图
>>) 在java中使用流 - aframe - 双击监听器需要在 iOS 13 上点击三下
- sql - SQL - 仅显示日期最新的行
- python - 是否可以在 python 中“流式传输”一个变量?
- python - 如何分析芹菜任务的内存使用情况?
- javascript - 将相关元素嵌套在一起父/子
- highcharts - 是否可以根据实时交易在图表上更新或绘制最后一根蜡烛?
- java - 有没有办法在 Maven 中为 javacv 的本机部分设置依赖项,而无需手动安装和设置 java.library.path?