【问题标题】:iOS MapKit - showAnnotations to Include User Location on ZoomiOS MapKit - 显示注释以在缩放时包含用户位置
【发布时间】:2018-01-03 19:37:28
【问题描述】:

我在 viewDidAppear 中有一个网络调用,它检索包含要在地图上显示的位置信息的站点列表。调用 showAnnotations 时,地图将缩放以显示所有注释不包括用户的位置。

其他 Stack Overflow 问题说调用 showAnnotations 并将动画标志设为 true,但这不起作用。

// Assign new sites
self.sites = newSites

// Show annotations on map
self.mapView.showAnnotations(self.sites, animated: true)

为什么只放大显示网站而不显示用户的位置?

【问题讨论】:

    标签: ios swift swift3 mapkit


    【解决方案1】:

    问题在于 showAnnotations 会将注释添加到地图中,并且仅缩放以显示这些注释 - 这是非常有意义的。那么我们如何才能包含用户的位置呢?我用以下代码做到了:

    // Assign new sites
    self.sites = newSites
    
    // Add new annotations
    self.mapView.addAnnotations(self.sites)
    
    // Show all annotations to include current user location
    self.mapView.showAnnotations(self.mapView.annotations, animated: true)
    

    这里的关键部分是首先使用 mapView.addAnnotations 添加注解,然后在包含用户位置的所有 mapView 的注解上调用 mapView.showAnnotations(..., animated: true)。

    【讨论】:

    • 有一点值得一提,mapView.showUserLocation=true必须在showAnnotations之前调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多