我很困惑为什么这是显示默认图像,而不是在纽约上空显示一个蓝色圆圈。任何关于这一点的见解,以及何时使用默认图像,都将非常感谢。
import UIKit
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setupMapview()
}
func setupMapview(){
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.74699, longitude: -73.98742), zoomLevel: 9, animated: false)
view.addSubview(mapView)
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.77014, longitude: -73.97480)
mapView.addAnnotation(annotation)
mapView.delegate = self
}
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
print("CORDINATE")
print(annotation.coordinate)
if annotation is MGLPointAnnotation {
print("SET\n\n\n")
let av = RoundedAnnotationView(annotation: annotation, reuseIdentifier: "ResuseIdentifier")
av.configure()
return av
}
return nil
}
}
class RoundedAnnotationView: MGLAnnotationView{
func configure(){
backgroundColor = .blue
layer.cornerRadius = 24
clipsToBounds = true
}
}
发布于 2020-06-09 12:15:09
标准的默认注释显示在NY中,因为这正是您在setupMapview
中添加到地图中的内容。如果你想让地图显示用户的位置,你必须告诉它这样做:
mapView.addAnnotation(annotation)
mapView.showsUserLocation = true // This needs to be set explicitly.
mapView.delegate = self
通常,当您想要访问用户的位置时,必须通过在info.plist
中插入正确的标志来请求许可
Privacy - Location When In Use Usage Description
以及某种解释性字符串:
"We'd like to track you with our satellite."
如果你在模拟器上运行你的应用程序,你可以创建一个自定义的位置:
-> ->功能模拟器位置->自定义位置...
发布于 2020-06-09 15:47:13
应在地图完全加载后添加注释。我有一个更详细的逐步解决方案:https://github.com/mapbox/mapbox-gl-native/issues/16492
https://stackoverflow.com/questions/62272090
复制