【问题标题】:Attempting to change MKAnnotation color within my code?试图在我的代码中更改 MKAnnotation 颜色?
【发布时间】:2015-04-25 00:38:21
【问题描述】:

所以我在 MapView 中设置了我的MKAnnotations,它可以按我的意愿工作。现在我想进一步试验并尝试更改引脚的颜色,在以下代码中实现这一点的最有效方法是什么:

override func viewDidAppear(animated: Bool) {
var annotationQuery = PFQuery(className: "Post")
currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
//annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
annotationQuery.whereKeyExists("Location")
annotationQuery.findObjectsInBackgroundWithBlock {
    (points, error) -> Void in
    if error == nil {
        // The find succeeded.
        println("Successful query for annotations")
        // Do something with the found objects

        let myPosts = points as! [PFObject]

        for post in myPosts {
            let point = post["Location"] as! PFGeoPoint
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
            annotation.title = post["title"] as! String!
            annotation.subtitle = post["username"] as! String!

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

                        let reuseId = "pin"
                        var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

                            println("Pinview was nil")
                            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                            pinView!.canShowCallout = true
                            pinView!.animatesDrop = true
                            pinView!.pinColor = .Green
                        return pinView
                }


            self.mapView.addAnnotation(annotation)
        }

    } else {
        // Log details of the failure
        println("Error: \(error)")
    }
}

我似乎无法自己解决这个问题,尽管它看起来很简单。我也对viewForAnnotation 方法感到困惑,据我所知,我似乎需要使用它,但我正在尝试的任何方法都不起作用。

【问题讨论】:

  • 显示您尝试过的 viewForAnnotation 代码。您是否确认委托方法实际上被调用(使用断点或 NSLog)?有关简单示例,请参阅stackoverflow.com/questions/24523702/…。记得设置/连接地图视图的代理出口。
  • 用 viewForAnnotation 方法更新了上面的代码。 NSLog 没有被调用。
  • viewForAnnotation 方法必须在其他方法之外——在类的顶层(与 viewDidAppear 相同)。此外,第一个参数可能必须是“mapView”而不是“aMapView”。
  • 所以这允许引脚颜色改变,但我遇到了 2 个问题。首先,我的蓝色当前位置信标不再出现,而是我的当前位置被一个大头针替换并跟随我当前的位置。其次,我将标注设置为 true,但是当点击注释标题/副标题时,没有选项可以切换到新视图。有什么建议吗?

标签: ios swift annotations mapkit xcode6.3


【解决方案1】:

我知道这是一个较晚的答案,但对于其他搜索的人,请查看此处的教程,该教程准确显示了如何为大头针着色

Ray Wenderlich MapView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2013-01-19
    • 2018-03-15
    • 1970-01-01
    • 2015-09-20
    • 2012-02-14
    • 2017-12-08
    相关资源
    最近更新 更多