加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift – 将MKAnnotationView添加到MKMapView

发布时间:2020-12-14 05:52:04 所属栏目:百科 来源:网络整理
导读:我试图将MKAnnotationView添加到MKMapView,但是我无法做到这一点…任何人都可以帮我把MKAnnotationView添加到MKMapView? 这是我的代码: override func viewDidLoad() { super.viewDidLoad() locationManager.desiredAccuracy = kCLLocationAccuracyBest l
我试图将MKAnnotationView添加到MKMapView,但是我无法做到这一点…任何人都可以帮我把MKAnnotationView添加到MKMapView?

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    var latitude:CLLocationDegrees = locationManager.location.coordinate.latitude
    var longitude:CLLocationDegrees = locationManager.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta)
    var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati,homeLong)
    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude)
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation,theSpan)
    self.theMapView.setRegion(theRegion,animated: true)
    self.theMapView.mapType = MKMapType.Hybrid
    self.theMapView.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.theMapView.addAnnotation(myHomePin)



    var anView:MKAnnotationView = MKAnnotationView()
    anView.annotation = myHomePin
    anView.image = UIImage(named:"xaxas")
    anView.canShowCallout = true
    anView.enabled = true

}
您需要实现viewForAnnotation委托方法并从那里返回一个MKAnnotationView。

以下是一个例子:

func mapView(mapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),//return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation,reuseIdentifier: reuseId)
        anView.image = UIImage(named:"xaxas")
        anView.canShowCallout = true
    }
    else {
        //we are re-using a view,update its annotation reference...
        anView.annotation = annotation
    }

    return anView
}

记住,当您要使用自己的自定义图像时,您需要创建一个简单的MKAnnotationView。 MKPinAnnotationView只能用于标准引脚注释。

另请注意,您不应该在调用startUpdatingLocation之后立即尝试访问locationManager.location。它可能还没有准备好

使用didUpdateLocations委托方法。

您还需要调用requestAlwaysAuthorization / requestWhenInUseAuthorization(请参阅Location Services not working in iOS 8)。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读