Giter Site home page Giter Site logo

Comments (2)

eleev avatar eleev commented on July 17, 2024 1

Of course! 🙂

Here are the .gif preview and the source code.

cluster issue

//
//  ViewController.swift
//  ClusteringDemo
//
//  Created by Astemir Eleev on 08/05/2017.
//  Copyright © 2017 Astemir Eleev. All rights reserved.
//

import UIKit
import MapKit

class ViewController: UIViewController {
    // MARK: - Outlets
    @IBOutlet weak var mapView: MKMapView!
    
    
    // MARK: - Prperties
    fileprivate let manager = ClusterManager()
    fileprivate lazy var regularPinImage: UIImage! = {
        let regPinImage = UIImage(named: "ReviewMapPin")
        return regPinImage
    }()
    
    // MARK: - Life cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        mapView.delegate = self
        prepareDataSource()
    }

    // MARK: - Data source utility 
    
    private func prepareDataSource() {
        for location in DataSource.locations {
            let annotation = Annotation()
            annotation.coordinate = location.coordinate
            annotation.title = "Annotation"
            
            manager.add(annotation)
        }
    }
}


// MARK: - Mapview Delegate extension
extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        let color = UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
        let clusterIdentifier = "Cluster"
        
        if let annotation = annotation as? ClusterAnnotation {
            var view = mapView.dequeueReusableAnnotationView(withIdentifier: clusterIdentifier)
            if view == nil {
                if let annotation = annotation.annotations.first as? Annotation, let type = annotation.type {
                    view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: clusterIdentifier, type: type)
                } else {
                    view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: clusterIdentifier, type: .color(color, radius: 25))
                }
            } else {
                view?.annotation = annotation
            }
            return view
        } else {
            let identifier = "Pin"
            
            // Excludes custom pin image setting to the user loation pin
            if annotation is Annotation {
                // Prepare annotation with custom image pin
                let pinAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                pinAnnotationView.canShowCallout = true
                pinAnnotationView.image = regularPinImage
                
                if let pinAnnotation = pinAnnotationView.annotation as? Annotation, let _ = pinAnnotation.cardId {
                    // Perform custom callout configuration
//                    configureDetailView(annotationView: pinAnnotationView)
                    
                }
                return pinAnnotationView
            }
            
            return nil
        }
    }
    
    
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        self.manager.reload(mapView, visibleMapRect: mapView.visibleMapRect)
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let annotation = view.annotation else { return }
        
        if let cluster = annotation as? ClusterAnnotation {
            mapView.removeAnnotations(mapView.annotations)
            
            var zoomRect = MKMapRectNull
            for annotation in cluster.annotations {
                let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)
                let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0)
                if MKMapRectIsNull(zoomRect) {
                    zoomRect = pointRect
                } else {
                    zoomRect = MKMapRectUnion(zoomRect, pointRect)
                }
            }
            manager.reload(mapView, visibleMapRect: zoomRect)
            mapView.setVisibleMapRect(zoomRect, animated: true)
        }
    }
    
}

// MARK: - Data source

struct DataSource {
    static let locations: [CLLocation] = [
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 37.761022315226072, longitude: -122.4344554611051),
        CLLocation(latitude: 37.701507476193548, longitude: -121.97959008638),
        CLLocation(latitude: 43.033212903070712, longitude: 44.679960692836502),
        CLLocation(latitude: 36.975306174752284, longitude: -122.0270734840278),
        CLLocation(latitude: 40.747549999999997, longitude: -73.991950000000003),
        CLLocation(latitude: 43.039627595647246, longitude: 44.225292529742532),
        CLLocation(latitude: 37.379781072994113, longitude: -122.0166141261552),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 43.033197209289654, longitude: 44.679963974842437),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 40.77552, longitude: -73.962333333333333),
        CLLocation(latitude: 37.787358900000001, longitude: -122.408227),
        CLLocation(latitude: 51.509979999999999, longitude: -0.13370000000000001),
        CLLocation(latitude: 37.379785347764731, longitude: -122.0165545308236),
        CLLocation(latitude: 43.033215152341221, longitude: 44.679941378531574),
        CLLocation(latitude: 36.975315897759963, longitude: -122.0270727296565),
        CLLocation(latitude: 43.035080105072637, longitude: 44.681543907747567),
        CLLocation(latitude: 36.976180030067432, longitude: -122.0262914524619),
        CLLocation(latitude: 43.021397166666667, longitude: 44.703524999999999),
        CLLocation(latitude: 40.747549999999997, longitude: -73.991950000000003),
        CLLocation(latitude: 51.509979999999999, longitude: -0.13370000000000001),
        CLLocation(latitude: 43.033227473529656, longitude: 44.679957786668083),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 37.379808272269898, longitude: -122.01647942897119),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 43.039779544720801, longitude: 44.225445709065923),
        CLLocation(latitude: 35.702069100000003, longitude: 139.77532690000001),
        CLLocation(latitude: 37.760688825545607, longitude: -122.4347840064414),
        CLLocation(latitude: 20.99019666666667, longitude: -156.66481166666671),
        CLLocation(latitude: 21.284588333333328, longitude: -157.83918833333331),
        CLLocation(latitude: 37.701379903627277, longitude: -121.97937467146851),
        CLLocation(latitude: -33.863399999999999, longitude: 151.21100000000001),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 43.023850826920601, longitude: 44.706958224137757),
        CLLocation(latitude: 37.761002177670107, longitude: -122.4344365942359),
        CLLocation(latitude: 43.023310635667087, longitude: 44.654483590101691),
        CLLocation(latitude: 37.701418167015262, longitude: -121.9794777688775),
        CLLocation(latitude: 51.509979999999999, longitude: -0.13370000000000001)
        
    ]
}

from cluster.

efremidze avatar efremidze commented on July 17, 2024

can u post a snippet? want to test it out.

from cluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.