当前位置: 首页 > 面试题库 >

在iOS中的CommentationView中自定义图钉图像

孙帅
2023-03-14
问题内容

我正在尝试从Swift 1.2更改为Swift
2.0,而我在更改的最后。目前,我正在MapViewController中进行更改,没有任何错误或警告,但是我的图钉(annotationView)的自定义图像未分配给图钉,并且显示了默认图像(红点)。

这是我的代码,希望您能提供一些帮助,因为我认为一切都很好,但是仍然无法正常工作:

func parseJsonData(data: NSData) -> [Farmacia] {

    let farmacias = [Farmacia]()

    do
    {
        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

        // Parse JSON data
        let jsonProductos = jsonResult?["farmacias"] as! [AnyObject]

        for jsonProducto in jsonProductos {

            let farmacia = Farmacia()
            farmacia.id = jsonProducto["id"] as! String
            farmacia.nombre = jsonProducto["nombre"] as! String
            farmacia.location = jsonProducto["location"] as! String

            let geoCoder = CLGeocoder()
            geoCoder.geocodeAddressString(farmacia.location, completionHandler: { placemarks, error in

                if error != nil {
                    print(error)
                    return
                }

                if placemarks != nil && placemarks!.count > 0 {

                    let placemark = placemarks?[0]

                    // Add Annotation
                    let annotation = MKPointAnnotation()
                    annotation.title = farmacia.nombre
                    annotation.subtitle = farmacia.id
                    annotation.coordinate = placemark!.location!.coordinate

                    self.mapView.addAnnotation(annotation)
                }

            })
        }
    }
    catch let parseError {
        print(parseError)
    }

    return farmacias
}

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

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    // Reuse the annotation if possible
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

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

    annotationView!.image = UIImage(named: "custom_pin.png")

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
    annotationView!.rightCalloutAccessoryView = detailButton

    print(annotationView!.image)

    return annotationView
}

提前致谢,

问候。


问题答案:

接受的答案无效,因为它尚未annotationViewelse块中初始化。

这是一个更好的解决方案。如果可能,它将使注释视图出队,否则将创建新视图:

斯威夫特3,4

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
    }

    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
}


 类似资料:
  • 关于这个问题有任何一个想法,请分享...当我在那个时候做通用应用程序时,如果我有两个图像,一个用于ipad,另一个用于iphone,如“Demo~iphone.png”和“Demo~ipad.png”在这种情况下,如果我只是使用“Demo”作为图像名称,那么xcode管理它并默认为ipad和iphone进行调整。那么这个功能可以用于横向和竖屏吗?我在默认的午餐图像中看到这种类型的功能,就像它的名字

  • 问题内容: 我要显示图像而不是小 别针 。 有人可以在这里放置一些有用的代码,或告诉它怎么做吗? 我希望我的图片 pinks.jpg 会显示在地图上,而不是默认的图钉视图( 石头图钉形状 ),而是固定位置。但是我仍然得到该引脚的默认图像。 问题答案: 如果要将自己的图像用于注释视图,则应创建一个,而不是。 是的子类,因此具有属性,但通常会覆盖该属性并绘制图钉图像(这就是它的作用)。 因此,将代码更

  • 问题内容: 我有一个.bmp图像,我想将其用作GUI的光标。该QCursor文件表明,这是可能的(“要创建自己的位图光标,要么使用QCursor构造函数需要一个位图和一个口罩或需要一个像素图作为参数构造函数”),但我似乎无法得到它在我收到’TypeError:QCursor():当我尝试将建议的模块与位图一起使用时,参数1具有意外的类型’str’时起作用。应该怎么做? 下面是产生上述错误的代码。该

  • 问题内容: 我正在尝试在Swift中更改MKMapView上的大头针图像,但不幸的是它不起作用。知道我在做什么错吗?我在这里看到了一些示例,但是没有用。 GreenDot图片可在其他地方使用。 问题答案: 不要忘记设置: 并确保您实施该协议。 如果您忘记执行此操作,则不会为您的地图调用实现。 此外,它看起来像破坏自定义图像。您必须将其设置为,或使用(没有属性)。

  • 在V7必应地图中,您提供了在下面链接的集群地图中添加图钉和infobox的功能 https://www.bingmapsportal.com/isdk/ajaxv7#loadingdynamicmodule3

  • 提前致谢 下面是我的plugin.xml