亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MKMapView:自定義視圖代替了注釋釘

MKMapView:自定義視圖代替了注釋釘

iOS
大話西游666 2019-10-26 13:39:36
我要顯示圖像MKMapView而不是小別針。有人可以在這里放置一些有用的代碼,或告訴它怎么做嗎?謝謝!編輯-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {MKPinAnnotationView *pinView = nil; if(annotation != mapView.userLocation) {    static NSString *defaultPinID = @"com.invasivecode.pin";    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];    if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID];    pinView.pinColor = MKPinAnnotationColorGreen;     pinView.canShowCallout = YES;    pinView.animatesDrop = YES;    pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch} else {    [mapView.userLocation setTitle:@"I am here"];}return pinView;}我希望我的圖片pinks.jpg會顯示在地圖上,固定位置而不是默認的大頭針視圖(搖滾大頭針形)。但是我仍然得到該引腳的默認圖像。
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

這是Swift 3的答案。如果可能,它將使注釋視圖出隊,否則將創建新視圖:


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    // Don't want to show a custom image if the annotation is the user's location.

    guard !(annotation is MKUserLocation) else {

        return nil

    }


    // Better to make this class property

    let annotationIdentifier = "AnnotationIdentifier"


    var annotationView: MKAnnotationView?

    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {

        annotationView = dequeuedAnnotationView

        annotationView?.annotation = annotation

    }

    else {

        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

    }


    if let annotationView = annotationView {

        // Configure your annotation view here

        annotationView.canShowCallout = true

        annotationView.image = UIImage(named: "yourImage")

    }


    return annotationView

}

Swift 2.2:


func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    // Don't want to show a custom image if the annotation is the user's location.

    guard !annotation.isKindOfClass(MKUserLocation) else {

        return nil

    }


    // Better to make this class property

    let annotationIdentifier = "AnnotationIdentifier"


    var annotationView: MKAnnotationView?

    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {

        annotationView = dequeuedAnnotationView

        annotationView?.annotation = annotation

    }

    else {

        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)

        annotationView = av

    }


    if let annotationView = annotationView {

        // Configure your annotation view here

        annotationView.canShowCallout = true

        annotationView.image = UIImage(named: "yourImage")

    }


    return annotationView

}


查看完整回答
反對 回復 2019-10-26
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

我同意安娜的答案,我想展示一下swift3的外觀。這個答案與其他許多選項一樣,比如調整圖像大小,從array和ect中獲取圖像列表。


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if let annotation = annotation as? PetrolStation {

            let identifier = "pinAnnotation"

            var view: MKAnnotationView

            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

                as? MKPinAnnotationView { // 2

                dequeuedView.annotation = annotation

                view = dequeuedView

            } else {

                // 3

                view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)

                view.canShowCallout = true


                //here We put a coordinates where we like to show bubble with text information up on the pin image

                view.calloutOffset = CGPoint(x: -7, y: 7)



                //Here this is a array of images

                let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace]


                //Here we set the resize of the image

                let size = CGSize(width: 30, height: 30)

                UIGraphicsBeginImageContext(size)

                pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

                let resizeImage = UIGraphicsGetImageFromCurrentImageContext()

                UIGraphicsEndImageContext()

                view.image = resizeImage


                //Here we like to put into bubble window a singe for detail Informations

                view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView

               //Here we make change of standard pin image with our image

                view.image = resizeImage

            }


            return view

        }

        return nil

    }


查看完整回答
反對 回復 2019-10-26
  • 3 回答
  • 0 關注
  • 609 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號