【问题标题】:Changing MKMapView User Location Annotation更改 MKMapView 用户位置注释
【发布时间】:2016-08-22 11:47:39
【问题描述】:

我正在尝试将 MKMapView 上的用户默认位置注释从蓝色更改为名为 geo 的自定义图像。当我设置断点时,将其设置为 geo 就行了,但是两个点(用户默认点和乘客点都是默认的红色精确注释)是我设置错了,还是有某些图像规定?

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

        if annotation.isKindOfClass(PassengerLocation) == false {

            //User location
            let userIdentifier = "UserLocation"
            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(userIdentifier)

            if annotationView == nil {
                annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:userIdentifier)
            }
            annotationView!.annotation = annotation
            annotationView!.canShowCallout = true

            // THIS IS NOT WORKING, DEFAULT RED PIN POINT STILL SHOWING
            annotationView!.image = UIImage(named: "geo")
            return annotationView


        }

        let identifier = "PassengerLocation"

        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
            annotationView.annotation = annotation

            return annotationView
        } else {
            let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
            annotationView.enabled = true
            annotationView.canShowCallout = true

            let btn = UIButton(type: .DetailDisclosure)
            annotationView.rightCalloutAccessoryView = btn

            return annotationView
        }

    }

【问题讨论】:

    标签: ios swift mkmapview


    【解决方案1】:
    guard !annotation.isKind(of: MKUserLocation.self) else {
         return nil
    }
    

    效果很好

    【讨论】:

    • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
    【解决方案2】:

    它对我有用

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    
        // want to show a custom image if the annotation is the user's location.
        guard !annotation.isKindOfClass(MKUserLocation) else {
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
            annotationView.image = UIImage(named: "icon_coordinates_self")
            return annotationView
            //return nil
        }
    
        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: "Annotation_map")
        }
    
        return annotationView
    }
    

    【讨论】:

    • 迄今为止最好的解决方案。
    【解决方案3】:

    这行得通:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
            if annotation.isEqual(mapView.userLocation) {
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
            annotationView.image = UIImage(named: "geo")
            return annotationView
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果您想自定义图像,请使用常规 MKAnnotationView。有了别针,你所能做的就是改变颜色。

      【讨论】:

        猜你喜欢
        • 2013-04-17
        • 2014-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-11
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多