菜鸟教程小白 发表于 2022-12-11 19:56:24

ios - map 注释有很多问题


                                            <p><p>对于一段非常简单的代码,我有几个问题。目标是从数据库中读取项目并将它们固定在 map 中。如果项目已被标记为收藏,则图钉应该是不同的颜色。</p>

<p>第一个问题是并不是所有的项目都被渲染了。在示例中,我将使用从查询返回的 12 个结果,并且我已验证每个项目都会创建一个 MKAnnotation,并且每个注释都会调用 ViewFor。</p>

<p>除了不显示所有图钉之外,还有另外两个问题。</p>

<p>滚动 map 时,第一个 Pin 图会随机丢失其标题。</p>

<p>其次,最喜欢的(绿色色调)很少呈现绿色。 80% 的时间它以标准蓝色出现。我再次验证了 MKMarkerAnnotationView 颜色设置正确。</p>

<p>面对所有这些问题,我不得不得出结论,我做的事情从根本上来说是非常错误的。这很奇怪,因为这看起来很简单。</p>

<pre><code>class FacilityMarker: NSObject, MKAnnotation {

// title and subtitle are from the MKAnnotation protocol
var coordinate:   CLLocationCoordinate2D
var title:          String?
var address:      String
var phone:          String
var providerNumber: String
var favorite:       Bool
var subtitle:       String? {
    get {
      return phone
    }
}
</code></pre>

<p> ViewController </p>

<pre><code>    class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    mapView.showAnnotations(mapView.annotations, animated: true)


// Create Annotations for all the facilities that are now visible on map
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let edgePoints = mapView.edgePoints()

    let minLat = edgePoints.ne.latitude &lt; edgePoints.sw.latitude ? edgePoints.ne.latitude : edgePoints.sw.latitude
    let maxLat = edgePoints.ne.latitude &gt; edgePoints.sw.latitude ? edgePoints.ne.latitude : edgePoints.sw.latitude
    let minLong = edgePoints.ne.longitude &lt; edgePoints.sw.longitude ? edgePoints.ne.longitude : edgePoints.sw.longitude
    let maxLong = edgePoints.ne.longitude &gt; edgePoints.sw.longitude ? edgePoints.ne.longitude : edgePoints.sw.longitude
    let visibleCitiesReqeuest =
      managedObjectModel.fetchRequestFromTemplate(withName: &#34;FetchByCoordinates&#34;, substitutionVariables: [&#34;minLat&#34; : minLat, &#34;minLong&#34; : minLong, &#34;maxLat&#34; : maxLat, &#34;maxLong&#34; : maxLong])
    do {
      let facilities = try CoreDataHelper.shared.persistentContainer.viewContext.fetch(visibleCitiesReqeuest!) as!
       for facility in facilities {
            let facilityMarker = FacilityMarker(name: facility.facilityName!, address: facility.addressLine1!, location: facility.location!, phone: facility.phoneNumber!, providerNumber: facility.providerNumber!, favorite: facility.favorite)   
            mapView.addAnnotation(facilityMarker)
      }
    } catch {
      let alertController = UIAlertController(title: &#34;No Facilities&#34;, message: &#34;There are no Facilities within the visible map area&#34;, preferredStyle: .alert)
      alertController.addAction(UIAlertAction(title: &#34;OK&#34;, style: UIAlertActionStyle.cancel, handler: nil))
      self.present(alertController, animated: true, completion: nil)
    }
}

// Put the pins in the map
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -&gt; MKAnnotationView? {
    guard let annotation = annotation as? FacilityMarker else { return nil}

    let identifier = &#34;facility&#34;
    var view: MKMarkerAnnotationView
    if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as?
      MKMarkerAnnotationView {
      dequeuedView.annotation = annotation
      view = dequeuedView
    } else {
      print(&#34;CREATING NEW View for: \(annotation.title!)&#34;)
      view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
    }

    // Set the Colors
    if (annotation.favorite) {
      view.markerTintColor = .green
      view.tintColor = .green
    } else {
      view.markerTintColor = .blue
      view.tintColor = .blue
    }
    return view
}
</code></pre>

<p>这是查询返回的实际数据。只有突出显示的项目才会被渲染。
这在所有测试中都是 100% 一致的。</p>

<ul>
<li> <blockquote>
<p>CREATING NEW View for: SEMINOLE DIALYSIS CENTER</p>
</blockquote> </li>
<li>为 LAKE SEMINOLE DIALYSIS 创建新 View </li>
<li> <blockquote>
<p>CREATING NEW View for: RAI CARE CENTERS - LARGO</p>
</blockquote> </li>
<li> <blockquote>
<p>CREATING NEW View for: BAY BREEZE DIALYSIS CLINIC INC</p>
</blockquote> </li>
<li>创建新 View :RENVIVA DIALYSIS CENTER OF CLEARWATER, LLC</li>
<li> <blockquote>
<p>CREATING NEW View for: FKC - BELLEAIR DIALYSIS CENTER</p>
</blockquote> </li>
<li> <blockquote>
<p>CREATING NEW View for: RAI CARE CENTERS - CLEARWATER</p>
</blockquote> </li>
<li>为 FMC - BELLEAIR 家庭疗法创建新 View </li>
<li> <blockquote>
<p>CREATING NEW View for: CORVA GULF COAST DIALYSIS CENTER</p>
</blockquote> </li>
<li>为:海湾微风透析中心创建新 View </li>
<li> <blockquote>
<p>CREATING NEW View for: BMA - CLEARWATER</p>
</blockquote> </li>
<li>为:RAI-US 19 NORTH-CLEARWATER 创建新 View </li>
</ul>

<p>以下是显示上述结果的捕获</p>

<p>这是一个初始渲染。这次 BMA 引脚是蓝色的,它被设置为绿色。
<a href="/image/wDe0b.png" rel="noreferrer noopener nofollow"><img src="/image/wDe0b.png" alt="Initial Render View Tint Wrong"/></a> </p>

<p>这次滚动后,BMA 色调是正确的。什么时候是正确的,没有规律可循。
<a href="/image/Y23eE.png" rel="noreferrer noopener nofollow"><img src="/image/Y23eE.png" alt="Sometimes Tint Works. Might be on initial render or maybe after scrolling.No pattern detected"/></a> </p>

<p>更多的滚动,只是为了增加更多的陌生感,有时一个或多个注释不会呈现它们的标题属性
<a href="/image/r6q7m.png" rel="noreferrer noopener nofollow"><img src="/image/r6q7m.png" alt="Sometimes Annotation Title disappears after scrolling"/></a> </p>

<p>请求调试的输出:</p>

<ul>
<li>RAI 护理中心 - LARGO 是蓝色的</li>
<li>BMA - CLEARWATER 是绿色的</li>
<li>CLEARWATER, LLC 的 RENVIVA 透析中心是蓝色的</li>
<li>RAI 护理中心 - CLEARWATER 是蓝色的</li>
<li>FKC - 贝莱尔透析中心是蓝色的</li>
<li>FMC - BELLEAIR HOME THERAPIES 是蓝色的</li>
<li>GULF BREEZE DIALYSIS CENTER 是蓝色的</li>
<li>BAY BREEZE DIALYSIS CLINIC INC 是蓝色的</li>
<li>RAI-US 19 NORTH-CLEARWATER 是蓝色的</li>
<li>CORVA GULF COAST DIALYSIS CENTER 是蓝色的</li>
</ul></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试下面的调试代码。你能显示输出吗?</p>

<pre><code>    ...
    // Set the Colors
    if (annotation.favorite) {
      print(annotation.title, &#34;is green&#34;)
      view.markerTintColor = .green
      view.tintColor = .green
    } else {
      print(annotation.title, &#34;is blue&#34;)
      view.markerTintColor = .blue
      view.tintColor = .blue
    }
    ...
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios -map 注释有很多问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49532389/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49532389/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - map 注释有很多问题