不再显示MapKit注释 [英] MapKit Annotations Disappearing

查看:109
本文介绍了不再显示MapKit注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个纬度数组和另一个经度数组,我将它们添加到CLLocationCoordinate2D类型的数组中.然后,我使用新数组来注释地图上的多个点.一些或大部分或什至所有注释都显示在地图上,但是当我放大(是,放大)时,某些注释会消失,然后返回或不显示.关于如何保持所有可见的任何想法?这是我在缩小而不是放大时会期望的行为.

I have an array of latitudes and another array of longitudes that I add to an array of type CLLocationCoordinate2D. I then use the new array to annotate multiple points on the map. Some, or most, or maybe even all of the annotations are displaying on the map but as I zoom in (yes, zoom IN), some of the annotations disappear, then come back, or dont. Any ideas on how to keep them all visible? This is behavior I would expect while zooming out, not in.

这是我上面描述的代码.

Here is the code i'm using for what i've described above.

import UIKit
import MapKit
import CoreLocation

class MultiMapVC: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var multiEventMap: MKMapView!

var latDouble = Double()
var longDouble = Double()
let manager = CLLocationManager()
var receivedArrayOfLats = [Double]()
var receivedArrayOfLongs = [Double]()
var locations = [CLLocationCoordinate2D]()


func locationManager(_ manager: CLLocationManager, didUpdateLocations uLocation: [CLLocation]) {
    let userLocation = uLocation[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.3, 0.3)
    let usersLocation = userLocation.coordinate
    let region:MKCoordinateRegion = MKCoordinateRegionMake(usersLocation, span)
    multiEventMap.setRegion(region, animated: true)
    manager.distanceFilter = 1000
    self.multiEventMap.showsUserLocation = true
}

func multiPoint() {

    var coordinateArray: [CLLocationCoordinate2D] = []
    print ("Received Longitude Count = \(receivedArrayOfLongs.count)")
    print ("Received Latitude Count = \(receivedArrayOfLats.count)")
    if receivedArrayOfLats.count == receivedArrayOfLongs.count {
        for i in 0 ..< receivedArrayOfLats.count {
            let eventLocation = CLLocationCoordinate2DMake(receivedArrayOfLats[i], receivedArrayOfLongs[i])
            coordinateArray.append(eventLocation)
            print (coordinateArray.count)
        }
    }

    for events in coordinateArray {
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: events.latitude, longitude: events.longitude)
        multiEventMap.addAnnotation(annotation)
        }
}


override func viewDidLoad() {
    super.viewDidLoad()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    multiPoint()
        }


override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    multiEventMap.removeFromSuperview()
    self.multiEventMap = nil

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

推荐答案

NiltiakSivad的解决方案可以工作,但会恢复到旧的iOS 10外观.如果您想为iOS 11保留新的iOS 11气球标记,并仅对较旧的iOS版本使用旧的引脚外观,则可以实现以下委托方法:

NiltiakSivad's solution works but it reverts to the old iOS 10 look. If you want to keep the new iOS 11 balloon markers for iOS 11 and use the old pin look only for older iOS versions then you can implement the delegate method as below:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "annotationView"
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
    if #available(iOS 11.0, *) {
        if view == nil {
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
        view?.displayPriority = .required
    } else {
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
    }
    view?.annotation = annotation
    view?.canShowCallout = true
    return view
}

这篇关于不再显示MapKit注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆