ios - 如何显示 iPhone map 应用程序蓝点和浅蓝色字段作为当前位置而不是红色图钉? (在 swift 中)

标签 ios swift mkmapview mapkit mkannotation

所以实际上有一个与我类似的问题,我在这里找到了How to get iPhone Maps app blue dot with light blue field for current location? ,我尝试了这个解决方案,但它没有用,这就是为什么是时候问我的下一个问题了......

我的 ios swift 应用程序中有一个 map View ,我将它以用户的位置为中心。到目前为止效果很好,但该位置用红色图钉标记,而不是我想要的,我想要这个典型的苹果蓝点,周围有一个区域。我的代码如下:

override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)
    mapView.delegate = self

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(appDelegate.getLocation().coordinate, 500, 500)

    mapView.setRegion(coordinateRegion, animated: true)
    mapView.showsUserLocation = true

    print("MapEvents: \(appDelegate.getLatitude()), \(appDelegate.getLongitude())") 
    //this prints correct GPS coords

}

那我做错了什么?

==== 编辑

按照 Ahmed 在评论部分的线索 - 是的,我已经实现了这个方法:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        var reuseId = ""
        if annotation.isKindOfClass(FBAnnotationCluster) {
            reuseId = "Cluster"
            var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
            clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
            return clusterView
        } else {
            reuseId = "Pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.calloutOffset = CGPoint(x: -5, y: 5)
            pinView!.canShowCallout = true
            pinView!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView

            return pinView

        }


    }

这是因为我正在使用本教程中的标记聚类 https://github.com/ribl/FBAnnotationClusteringSwift

当我注释掉此方法时,我看到了蓝点,这很好,但现在我需要恢复旧代码 - 有没有一种方法可以同时使用这两种功能(集群和蓝点)?那么我应该如何修改我的注解方法的 View ?

非常感谢!

最佳答案

在 MKMapView viewForAnnotation 委托(delegate)方法中,如果注释类型为 MKUserLocation,则返回 nil。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }
    var reuseId = ""
    if annotation.isKindOfClass(FBAnnotationCluster) {
        reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
        return clusterView
    } else {
        reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.calloutOffset = CGPoint(x: -5, y: 5)
        pinView!.canShowCallout = true
        pinView!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView

        return pinView

    }


}

关于ios - 如何显示 iPhone map 应用程序蓝点和浅蓝色字段作为当前位置而不是红色图钉? (在 swift 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826623/

相关文章:

ios - Firebase 打印 Analytics SSL 错误,但未使用 Analytics

ios - 如何在自身类中设置 UIBarButtonSystemItem (Swift)

ios - 当我的应用程序未在手机上打开时,我可以向服务器请求吗?

ios - Annotation的calloutView上UIButton点击失败

iphone - 可以在MKMapView上绘制的最大注释数(MKAnnotation)?

ios - 将数据从应用转移到应用?

ios - 使用 Core Graphics 绘图时 PDF 文件显示不正确

ios - UIScrollView 切断底部内容

ios - 如何动态填充标签栏 Controller 的标签栏。? ( swift ——iOS 9)

ios - MKAnnotationView 和点击检测