-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathMKMapViewExtensions.swift
More file actions
66 lines (58 loc) · 2.75 KB
/
MKMapViewExtensions.swift
File metadata and controls
66 lines (58 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// MKMapViewExtensions.swift - Copyright 2025 SwifterSwift
#if canImport(MapKit) && !os(watchOS)
import MapKit
public extension MKMapView {
/// SwifterSwift: Dequeue reusable MKAnnotationView using class type.
///
/// - Parameters:
/// - name: MKAnnotationView type.
/// - Returns: optional MKAnnotationView object.
func dequeueReusableAnnotationView<T: MKAnnotationView>(withClass name: T.Type) -> T? {
return dequeueReusableAnnotationView(withIdentifier: String(describing: name)) as? T
}
/// SwifterSwift: Register MKAnnotationView using class type.
///
/// - Parameter name: MKAnnotationView type.
func register<T: MKAnnotationView>(annotationViewWithClass name: T.Type) {
register(T.self, forAnnotationViewWithReuseIdentifier: String(describing: name))
}
/// SwifterSwift: Dequeue reusable MKAnnotationView using class type.
///
/// - Parameters:
/// - name: MKAnnotationView type.
/// - annotation: annotation of the mapView.
/// - Returns: optional MKAnnotationView object.
func dequeueReusableAnnotationView<T: MKAnnotationView>(withClass name: T.Type,
for annotation: any MKAnnotation) -> T? {
guard let annotationView = dequeueReusableAnnotationView(
withIdentifier: String(describing: name),
for: annotation) as? T else {
fatalError("Couldn't find MKAnnotationView for \(String(describing: name))")
}
return annotationView
}
/// SwifterSwift: Zooms in on multiple mapView coordinates.
///
/// - Parameters:
/// - coordinates: Gets the array of type CLLocationCoordinate2D.
/// - meter: If arrays have a single item, they take the value of meters (Double). The map zooms in at the given
/// meters.
/// - edgePadding: The amount of additional space (measured in screen points) to make visible around the specified
/// rectangle
/// - animated: The animation control takes the Boolean value. Enter the true value for zooming with the
/// animation.
func zoom(to coordinates: [CLLocationCoordinate2D], meter: Double, edgePadding: SFEdgeInsets, animated: Bool) {
guard !coordinates.isEmpty else { return }
if coordinates.count == 1 {
let coordinateRegion = MKCoordinateRegion(
center: coordinates.first!,
latitudinalMeters: meter,
longitudinalMeters: meter)
setRegion(coordinateRegion, animated: true)
} else {
let mkPolygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
setVisibleMapRect(mkPolygon.boundingMapRect, edgePadding: edgePadding, animated: animated)
}
}
}
#endif