-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDMapUtil.m
98 lines (80 loc) · 3 KB
/
PDMapUtil.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// PDMapUtil.m
// uDelivered
//
// Created by Peter on 6/06/13.
//
//
#import "PDMapUtil.h"
@implementation PDMapUtil
// ref: http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/
+ (void)zoomMapToFitAnnotations:(MKMapView*)mapView horizontalPadding:(float) horizontalPadding verticalPadding:(float) verticalPadding minimumLatitudeSpan:(float)minLatSpan animated:(BOOL)animated
{
if ([mapView.annotations count] == 0)
{
return;
}
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in mapView.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
// add padding at the sides
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) + verticalPadding;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) + horizontalPadding;
region.span.latitudeDelta = MAX(minLatSpan, region.span.latitudeDelta);
region = [mapView regionThatFits:region];
if (!CLLocationCoordinate2DIsValid(region.center))
{
region.center = CLLocationCoordinate2DMake(0, 0);
}
@try
{
[mapView setRegion:region animated:animated];
}
@catch (NSException * e)
{
//DBG_ASSERT1(false, @"%@", e.reason);
}
}
+ (void)zoomMapToFitAnnotations:(MKMapView*)mapView
{
[self zoomMapToFitAnnotations:mapView horizontalPadding:1.2 verticalPadding:1.2 minimumLatitudeSpan:0 animated:YES];
}
+ (void) zoomMapToUserLocation:(MKMapView*)mapView
{
if (mapView.userLocation.location)
{
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
}
+ (BOOL) regionIsValid:(MKCoordinateRegion)region
{
// check for sane span values
if (region.span.latitudeDelta <= 0.0f || region.span.longitudeDelta <= 0.0f)
{
return false;
}
// check for sane center values
if (region.center.latitude > 90.0f || region.center.latitude < -90.0f ||
region.center.longitude > 360.0f || region.center.longitude < -180.0f
) {
return false;
}
return true;
}
@end