【问题标题】:How can I make an annotation like maps app has如何制作像地图应用程序一样的注释
【发布时间】:2017-07-20 12:47:13
【问题描述】:

我有一个带有地图的应用程序和许多使用自定义图像制作的注释,我的问题是当我拖动地图或缩放时,注释会显示另一个点(纬度,经度)。

所以我想知道,因为我在其他应用程序中也看到了这个错误,有没有办法修复它(作为始终显示正确纬度和经度的地图注释)。

这是我用于注释的代码:

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

    if annotation is MKUserLocation {
        return nil
    }


    var reuseId = ""

    if annotation is FBAnnotationCluster {

        reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, configuration: FBAnnotationClusterViewConfiguration.default())
        return clusterView
    }else{

        reuseId = "pin"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if  annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView!.image = UIImage(named: "locat.png")


        }
        else {
            annotationView!.annotation = annotation

        }

        let cpa = annotation as! FBAnnotation

        annotationView!.image = nil

        let image = UIImage(named:cpa.nameImage)


        annotationView!.image = image


        return annotationView
    }




}

如果您对我的问题或其他任何问题有任何疑问,请随时发表评论。

提前致谢!

编辑

我上传了一个video 显示我的问题

EDIT2

这里有一个来自地图的video,表明他们的注释没有丢失它的纬度和经度。

【问题讨论】:

    标签: ios swift swift3 annotations mapkit


    【解决方案1】:

    你的问题是MKAnnotationView.centerOffset你需要在func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?中调整这个属性

    在 MKAnnotationView 文档中定义

    centerOffset 属性显示的偏移量(以磅为单位) 查看。

    var centerOffset: CGPoint

    讨论 默认情况下, 注释视图的中心点放置在坐标点处 的相关注释。您可以使用此属性重新定位 根据需要注释视图。测量此 x 和 y 偏移值 在点。正偏移值将注释视图向下移动到 向右,而负值将其向上和向左移动。

    类似的东西

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
            if  annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
                annotationView!.image = UIImage(named: "locat.png")
                //this is an example you must to get in account the form of your current view and what you want, I think your values are CGPoint(x: 0.5, y: 1)
                annotationView?.centerOffset = CGPoint(x: 0, y: -((UIImage(named: "locat.png")?.size.height)!/2))
    
            }
            else {
                annotationView!.annotation = annotation
    
            }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      您不会丢失经度,但您的经度位于注释图像的中心,您必须更改锚点。

      annotationView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
      

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

      【讨论】:

      • 谢谢你成功了,但是 Reinier 先回答了所以我选择他的回答作为接受
      猜你喜欢
      • 2018-02-04
      • 1970-01-01
      • 2012-04-15
      • 2018-12-04
      • 2014-07-03
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多