Closed eleev closed 7 years ago
can u post a snippet? want to test it out.
Of course! 🙂
Here are the .gif
preview and the source code.
//
// 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)
]
}
Hello,
I've faced an issue where I have a map with about
25
pins. Half of them is located inAsia
and the other half inUS
. When I zoom out the map and look at theAsian
cluster everything works great. But when I swipe the map to theUS
cluster, it simply does not display at the same zoom level. When zoom it in a little bit, then it appears.US
cluster is a little bit smaller e.g. Asian 14 pins vs 11 pins in US.As I understand that behaviour has something with spatial division of pins in clusters. The Asian cluster is a 'parent' one for US, that is why they both cannot be displayed at the same zoom level.
The question is: How to display the
US
cluster when map is zoomed out at max distance?