【问题标题】:set unique images in leftcalloutaccessoryview using for loop array in annotation map swift使用注释映射中的for循环数组在leftcalloutaccessoryview中设置唯一图像
【发布时间】:2015-03-10 05:28:22
【问题描述】:

我有一组图像需要在 MKAnnotation 的 leftcalloutaccessoryview 中设置为左图标。我已经使用 if else 语句完成了它。我的问题是,如果我可以使用数组循环在“for”语句中对其进行编码,而不是使用“if else”语句,那就太好了。任何人都可以帮助我吗?如何使用 for 循环数组使用我的数组集中的一组图像和坐标来编码“leftcalloutaccessoryview”?我已经面对这个代码一个月了 :(...谢谢你们

    //Define nearby Place of Interest
    let latArray = [24.469546, 24.469450]
    let longArray = [39.609105, 39.613062]
    let poiTitle = ["An-Nisa 1","An-Nisa 2"]
    let poiSubTitle = ["Gate 1","Gate 2"]
  //  let imageName = ["Jons.png","Locus.png"]

    for var i=0;i<poiSubTitle.count;i++
    {
        var latPoi: CLLocationDegrees = latArray[i]
        var longPoi: CLLocationDegrees = longArray[i]

        var locationPoiAnnot = MKPointAnnotation()

        locationPoiAnnot.title = poiTitle[i]
        locationPoiAnnot.subtitle = poiSubTitle[i]
        var locationPoi : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latPoi,longPoi)
        locationPoiAnnot.coordinate = locationPoi


        self.mapView.addAnnotation(locationPoiAnnot)

    }

}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
   if annotation is MKUserLocation {
              //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true

        let arrowButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        arrowButton.frame.size.width = 44
        arrowButton.frame.size.height = 44
        arrowButton.setImage(UIImage(named: "arrow.jpeg"), forState: .Normal)

        pinView!.rightCalloutAccessoryView = arrowButton

    var imageview = UIImageView(frame: CGRectMake(0, 0, 45, 45))

   if(annotation.subtitle == "I am here!")
        {

            imageview.image=UIImage(named: "Locus.png")

        }
        else if(annotation.subtitle == "Gate 1")
        {
            imageview.image=UIImage(named: "Jons.png")

        }

        else if(annotation.subtitle == "Gate 2")
        {

        imageview.image=UIImage(named: "Katara.png")

        }


        pinView!.leftCalloutAccessoryView = imageview


}
    else {
        pinView!.annotation = annotation
    }
    return pinView    }

【问题讨论】:

  • 您的问题与这个问题非常相似:stackoverflow.com/questions/25631410/…。唯一的区别是您想为标注使用不同的图像而不是注释视图。
  • yup Anna...非常相似...只是我想在“for”语句循环中实现它(图像,在数组集中声明的坐标)...想象一下,如果我有 30 个地方有兴趣,我觉得我在代码里一一声明不合适……Anna你还有其他解决办法吗?
  • 重点是在创建注释时不能访问 leftCalloutAccessoryView 属性。链接的答案显示了如何通过注释对象本身将数据传递到 viewForAnnotation 委托方法。因此,不要使用内置的 MKPointAnnotation 类,而是使用您需要的属性创建一个自定义类,并在 viewForAnnotation 中使用该属性。

标签: arrays loops swift mkannotation


【解决方案1】:

想知道这是否有帮助:

let poiSubTitle = ["Gate 1","Gate 2"]
let imageName = ["Jons.png","Locus.png"]

for(var i = 0 ; i < imageName.count ; i++)
{
    if(annotation.subtitle == poiSubTitle[i])
    {
        imageview.image = UIImage(named: imageName[i])
    }
}

pinView!.leftCalloutAccessoryView = imageview

所以只需将所有 30 个 POI 标题添加到 poiSubTitle 并将图像路径添加到 imageName 而不更改 for 循环...

【讨论】:

  • 我假设您建议将此放在 viewForAnnotation 中。尽管这可能有效,但效率低下(可以为同一个注解多次调用 viewForAnnotation),不必要地强制您声明和使用类级变量,并继续将逻辑与注解的标题联系起来。一种更面向对象的方法是为包含所有必需属性的注释创建一个定义良好的自定义类,并传递这些对象,而不是不断地遍历数组来寻找东西。
  • 是的,我只是根据上面的要求简化了代码。更好的方法是按照 Anna 的建议做一个函数。
猜你喜欢
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多