MKMapView не кластеризует аннотацию при уменьшении карты в Swift - PullRequest
1 голос
/ 26 мая 2019

прежде чем дублировать мои квесты, пожалуйста, прочитайте все это. Я использую MkMapKit в своем приложении, и теперь мне нужно показывать людей в кластере при уменьшении карты, я достиг нумерации их до сих пор, используя этот ответ , используя класс кластеризации Apple по умолчанию. теперь я понятия не имею, как добавить и показать их все одним кружком с количеством отсчетов, я знаю кое-что, что должно быть связано с радиусом, но я не знаю, как я могу это сделать, поделившись своим кодом ниже, я надеюсь, что любая помощь будет оценили. Спасибо также показывает картину того, что я сделал:

enter image description here

Это мой UserAnnotationClass

class UserAnnotation: NSObject, MKAnnotation {

    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D

    let userProfile: UserProfile!
    let index: Int!
    let memberAnnotations: [UserProfile]!
    init(userProfile: UserProfile, at index: Int) {

        self.title = userProfile.fullName
        self.locationName = (userProfile.locationAddress != nil) ? userProfile.locationAddress : ""
        let userProfilePicture: String = (userProfile.profilePicture == nil || userProfile.profilePicture == "") ? "" : userProfile.profilePicture

        self.discipline = userProfilePicture

       // print("\(userProfile.fullName) \(userProfile.location.dist)")

        if (userProfile.isMapVisibility == true) {
            self.coordinate = CLLocationCoordinate2D(latitude: userProfile.location.lat, longitude: userProfile.location.lon)
        } else {
            self.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
        }
        memberAnnotations = [UserProfile]()
        memberAnnotations.append(userProfile)
        self.userProfile = userProfile
        self.index = index

        super.init()
    }

    var subtitle: String? {
        return locationName
    }

    // pinTintColor for disciplines: Sculpture, Plaque, Mural, Monument, other
    var markerTintColor: UIColor  {
        switch discipline {
        case "Monument":
            return .red
        case "Mural":
            return .cyan
        case "Plaque":
            return .blue
        case "Sculpture":
            return .purple
        default:
            return .clear
        }
    }

    // Annotation right callout accessory opens this mapItem in Maps app
    func mapItem() -> MKMapItem {
        let addressDict = [CNPostalAddressStreetKey: subtitle!]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDict)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = title


        return mapItem
    }
}

И это CLusterViewClass , который я использую для их кластеризации.

class ClusterView: MKAnnotationView {


    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if (hitView != nil)
        {

            if (hitView?.isKind(of: UIButton.self))! {

                let sender: UIButton = hitView as! UIButton

                sender.sendActions(for: .touchUpInside)

            }
            else {

                self.superview?.bringSubviewToFront(self)
            }
        }
        return hitView
    }
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let rect = self.bounds
        var isInside: Bool = rect.contains(point)
        if(!isInside)
        {
            for view in self.subviews
            {
                isInside = view.frame.contains(point)
                if isInside
                {
                    break
                }
            }
        }
        return isInside
    }
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        displayPriority = .defaultHigh
        collisionMode = .circle
        centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        willSet {

            canShowCallout = false

            if let cluster = newValue as? UserAnnotation {
                let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
                let count = cluster.memberAnnotations.count
                let uniCount = cluster.memberAnnotations.filter { member -> Bool in
                    //Log("Bool  \(member) , \(member.isMapVisibility == false) ?")

                    return member.isMapVisibility == true
                }.count
                //Log("COUNTS \(count) , \(uniCount) ❤️")
                image = renderer.image { _ in
                    // Fill full circle with tricycle color
                    if uniCount > 0 {

                        AppTheme.blueColor.setFill()
                        UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()

                        // Fill inner circle with white color
                        UIColor.white.setFill()
                        UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill()

                        // Finally draw count text vertically and horizontally centered
                        let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.black,
                                           NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]
                        //let text = "\(count)"
                        let text = "4"
                        let size = text.size(withAttributes: attributes)
                        let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
                        text.draw(in: rect, withAttributes: attributes)
                    }
                }
            }
        }
    }

}

И вот некоторые из моих функций MapKit

extension FeedsViewController: MKMapViewDelegate {

    //   1
      func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard let annotation = annotation as? UserAnnotation else { return nil }
        // 2
        let identifier = "marker"
        if #available(iOS 11.0, *) {
            var view: ClusterView

            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
                as? ClusterView { // 3
                dequeuedView.annotation = annotation
                view = dequeuedView
            } else {
                // 4
                view = ClusterView(annotation: annotation, reuseIdentifier: identifier)
            }
            return view
        } else {
            // Fallback on earlier versions

            return nil
        }

      }
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
//        let zoomWidth = mapView.visibleMapRect.size.width
//        let zoomFactor = Int(log2(zoomWidth))
//        print("...REGION DID CHANGE: ZOOM FACTOR \(zoomFactor)")
        let centralLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude:  mapView.centerCoordinate.longitude)

        Log("? Radius - \(self.getRadius(centralLocation: centralLocation))")


    }
    func getRadius(centralLocation: CLLocation) -> Double{
        let topCentralLat:Double = centralLocation.coordinate.latitude -  mapView.region.span.latitudeDelta/2
        let topCentralLocation = CLLocation(latitude: topCentralLat, longitude: centralLocation.coordinate.longitude)
        let radius = centralLocation.distance(from: topCentralLocation)
        return radius / 1000.0 // to convert radius to meters
    }
    func mapView(_ mapView: MKMapView,
                 didSelect view: MKAnnotationView)
    {
        // 1
        if view.annotation is MKUserLocation
        {
            // Don't proceed with custom callout
            return
        }
        // 2
        let annotation = view.annotation as! UserAnnotation

        let detailAnnotationView: UserDetailAnnotationView = UserDetailAnnotationView(frame: CGRect(x: 0, y: 0, width: 320, height: 74))

        let url = (annotation.discipline == "") ? nil : URL(string: annotation.discipline)!
        let range = 0.0..<0.9
        if annotation.userProfile.location.dist != nil {
            if range.contains(annotation.userProfile.location.dist) {
                let kMeters = Measurement(value: annotation.userProfile.location.dist, unit: UnitLength.kilometers)
                let meters = kMeters.converted(to: UnitLength.meters)
                detailAnnotationView.distancelbl.text = "\(String(describing: round(Double(meters.value)))) m Away"

            } else {
                detailAnnotationView.distancelbl.text = "\(String(describing: round(annotation.userProfile.location.dist))) Km Away"
            }
        }

        detailAnnotationView.set(Title: annotation.title!, imageUrl: url) { [weak self] (sender) in
            guard let self = self else { return }
            if self.isOpenChat {

                self.isOpenChat = false

                if annotation.userProfile.channel != "" {

                    self.appDelegate.pubNubAddPushNotifications([annotation.userProfile.channel]) { (status) in

                        print(status.description)
                    }

                    let chatViewController: ChatViewController = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController

                    chatViewController.userProfile = annotation.userProfile
                    chatViewController.loginUserProfile = self.loginUserProfile
                    self.navigationController?.pushViewController(chatViewController, animated: true)
                }
            }

        }

        detailAnnotationView.center = CGPoint(x: view.bounds.size.width / 2, y: -detailAnnotationView.bounds.size.height*0.52)

        view.addSubview(detailAnnotationView)

        mapView.setCenter((view.annotation?.coordinate)!, animated: true)

//        let calloutView = views?[0] as! CustomCalloutView
//        calloutView.starbucksName.text = starbucksAnnotation.name
//        calloutView.starbucksAddress.text = starbucksAnnotation.address
//        calloutView.starbucksPhone.text = starbucksAnnotation.phone
//        calloutView.starbucksImage.image = starbucksAnnotation.image
//        let button = UIButton(frame: calloutView.starbucksPhone.frame)
//        button.addTarget(self, action: #selector(ViewController.callPhoneNumber(sender:)), for: .touchUpInside)
//        calloutView.addSubview(button)
//        // 3
//        calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
//        view.addSubview(calloutView)
//        mapView.setCenter((view.annotation?.coordinate)!, animated: true)
    }

    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        if #available(iOS 11.0, *) {
            if view.isKind(of: ClusterView.self)
            {
                for subview in view.subviews
                {
                    subview.removeFromSuperview()
                }
            }
        } else {
            // Fallback on earlier versions
        }
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl) {
//        let location = view.annotation as! UserAnnotation
//        let launchOptions = [MKLaunchOptionsDirectionsModeKey:
//            MKLaunchOptionsDirectionsModeDriving]
//        location.mapItem().openInMaps(launchOptions: launchOptions)
    }
}

Вот так я настраиваю mapView ....

fileprivate func setupMapsLayout() {

    if self.userAnnotationList.count > 0 {
    // HereMap
        self.mapView.removeAnnotations(self.userAnnotationList)
    }

    self.mapView.delegate = self
    //    mapView.register(ArtworkMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    if #available(iOS 11.0, *) {
        // HereMap
        //self.mapView.register(UserAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
    } else {
        // Fallback on earlier versions
    }

    self.loadInitialData()

    self.mapView.addAnnotations(self.userAnnotationList)
    //self.mapView.topCenterCoordinate()
}

1 Ответ

0 голосов
/ 27 мая 2019

ОК, iOS 11 и более поздние решения довольно просты.У вас есть два вида аннотаций, один для ваших собственных аннотаций и один для кластеров аннотаций.Ваш основной вид аннотации просто должен указать clusteringIdentifier, когда он инициализируется и когда изменяется свойство annotation:

class UserAnnotationView: MKMarkerAnnotationView {
    static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserAnnotationView"

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = UserAnnotationView.preferredClusteringIdentifier
        collisionMode = .circle
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        willSet {
            clusteringIdentifier = UserAnnotationView.preferredClusteringIdentifier
        }
    }
}

И ваш вид аннотации кластера должен просто обновить свое изображение, когда его свойство annotationобновлено:

class UserClusterAnnotationView: MKAnnotationView {
    static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserClusterAnnotationView"

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        collisionMode = .circle
        updateImage()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? { didSet { updateImage() } }

    private func updateImage() {
        if let clusterAnnotation = annotation as? MKClusterAnnotation {
            self.image = image(count: clusterAnnotation.memberAnnotations.count)
        } else {
            self.image = image(count: 1)
        }
    }

    func image(count: Int) -> UIImage {
        let bounds = CGRect(origin: .zero, size: CGSize(width: 40, height: 40))

        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { _ in
            // Fill full circle with tricycle color
            AppTheme.blueColor.setFill()
            UIBezierPath(ovalIn: bounds).fill()

            // Fill inner circle with white color
            UIColor.white.setFill()
            UIBezierPath(ovalIn: bounds.insetBy(dx: 8, dy: 8)).fill()

            // Finally draw count text vertically and horizontally centered
            let attributes: [NSAttributedString.Key: Any] = [
                .foregroundColor: UIColor.black,
                .font: UIFont.boldSystemFont(ofSize: 20)
            ]

            let text = "\(count)"
            let size = text.size(withAttributes: attributes)
            let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
            let rect = CGRect(origin: origin, size: size)
            text.draw(in: rect, withAttributes: attributes)
        }
    }
}

Затем все, что вам нужно сделать, это зарегистрировать ваши классы:

mapView.register(UserAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

Нет mapView(_:viewFor:) реализация не требуется (и не желательно).Но приведенные выше результаты (с отображением анимации по умолчанию при уменьшении и увеличении масштаба):

enter image description here

Теперь, очевидно, вы можете изменить UserAnnotationViewкак пожелаешь.(Ваш вопрос не указал, как будет выглядеть стандартное однотарное представление аннотации).Но, установив clusteringIdentifier и зарегистрировав MKMapViewDefaultClusterAnnotationViewReuseIdentifier, вы довольно легко получаете кластеризацию в iOS 11 и более поздних версиях.

Если вы действительно хотите, чтобы представления аннотаций кластера выглядели как стандартные представления аннотаций, вы можете зарегистрироватьодин и тот же класс представлений аннотаций для обоих:

mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

Но тогда вам нужно дать кластерному аннотационному представлению тот же clusteringIdentifier, который мы ранее дали для стандартного аннотационного представления:

class UserClusterAnnotationView: MKAnnotationView {
    static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserClusterAnnotationView"

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = UserClusterAnnotationView.preferredClusteringIdentifier
        collisionMode = .circle
        updateImage()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        didSet {
            clusteringIdentifier = UserClusterAnnotationView.preferredClusteringIdentifier
            updateImage()
        }
    }

    private func updateImage() {
        if let clusterAnnotation = annotation as? MKClusterAnnotation {
            self.image = image(count: clusterAnnotation.memberAnnotations.count)
        } else {
            self.image = image(count: 1)
        }
    }

    func image(count: Int) -> UIImage {
        let bounds = CGRect(origin: .zero, size: CGSize(width: 40, height: 40))

        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { _ in
            // Fill full circle with tricycle color
            AppTheme.blueColor.setFill()
            UIBezierPath(ovalIn: bounds).fill()

            // Fill inner circle with white color
            UIColor.white.setFill()
            UIBezierPath(ovalIn: bounds.insetBy(dx: 8, dy: 8)).fill()

            // Finally draw count text vertically and horizontally centered
            let attributes: [NSAttributedString.Key: Any] = [
                .foregroundColor: UIColor.black,
                .font: UIFont.boldSystemFont(ofSize: 20)
            ]

            let text = "\(count)"
            let size = text.size(withAttributes: attributes)
            let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
            let rect = CGRect(origin: origin, size: size)
            text.draw(in: rect, withAttributes: attributes)
        }
    }
}

выходы:

enter image description here

Лично я думаю, что это немного сбивает с толку, но если это то, что вы собираетесь, это один из способов добиться этого.


Теперь, если вам действительно нужно поддерживать версии iOS до 11 и вам нужна кластеризация, вам придется самостоятельно выполнять всю эту логику кластеризации (или найти стороннюю библиотеку для этого).Apple показывает, как это сделать в WWDC 2011 Географическая визуализация информации с помощью MapKit .Концепция, которую они используют, это понятие деления видимой карты на сетку, и если в конкретной сетке есть несколько аннотаций, они удаляют их и добавляют одну «кластерную» аннотацию.И они иллюстрируют, как вы можете даже визуально анимировать перемещение аннотаций внутри и из кластера, чтобы пользователь мог понять, что происходит, когда они увеличивают и уменьшают масштаб.Это хорошая отправная точка, когда вы погрузитесь в это.

Это нетривиально, поэтому я долго и усердно думаю о том, хочу ли я реализовать это сам.Я либо откажусь от версий iOS до 11, либо найду стороннюю реализацию (и у этого вопроса, на который вы ссылаетесь есть множество примеров).

...