-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationMapView.m
116 lines (89 loc) · 3.46 KB
/
LocationMapView.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// LocationMapView.m
// AvocadoTest1
//
// Created by Jake on 12-02-23.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "LocationMapView.h"
#import "LocationState.h"
#import "Location.h"
#import "FlexableDictionary.h"
#import "CLPinAnnotationView.h"
@implementation LocationMapView
@synthesize locationState;
-(id)initWithLocationState:(LocationState *)aLocationState AndFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
locationState = aLocationState;
annotationViewDict = [[FlexableDictionary alloc] init];
CLLocationCoordinate2D currentLocation = [[aLocationState selectedLocation] coordinate];
MKCoordinateRegion viewRegion = MKCoordinateRegionMake(currentLocation, MKCoordinateSpanMake(0.8f, 0.8f));
// 3
MKCoordinateRegion adjustedRegion = [self regionThatFits:viewRegion];
// 4
[self setRegion:adjustedRegion animated:YES];
[self setDelegate:self];
[self performSelectorOnMainThread:@selector(addAnnotations) withObject:nil waitUntilDone:YES];
}
return self;
}
-(void) addAnnotations
{
[self addAnnotations:[locationState locations]];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// Use custom annotation view for anything but the user's locaiton pin
CLPinAnnotationView *clAnnotationView = (CLPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: @"CLPin"];
if (!clAnnotationView)
{
clAnnotationView = [[CLPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CLPin"];
}
else
{
clAnnotationView.annotation = annotation;
clAnnotationView.titleLabel.text = annotation.title;
clAnnotationView.subtitleLabel.text = annotation.subtitle;
}
if ([annotation isEqual:[locationState selectedLocation]])
{
[clAnnotationView setPinColor:MKPinAnnotationColorGreen];
if (!lastSelectedView) {
lastSelectedView = clAnnotationView;
}
}
else
[clAnnotationView setPinColor:MKPinAnnotationColorRed];
[clAnnotationView setAnimatesDrop:YES];
[annotationViewDict setAssociativeTuple:clAnnotationView :annotation];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(centerMapToAnnotation:) name:@"annotationSelected" object:clAnnotationView];
return clAnnotationView;
}
-(Location *)annotationForAnnotationView:(MKAnnotationView *)view
{
return [annotationViewDict objectForKey:view];
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
lastSelectedView = view;
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
[(MKPinAnnotationView *)lastSelectedView setPinColor:MKPinAnnotationColorRed];
[locationState setSelectedLocation:[self annotationForAnnotationView:view]];
[(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorGreen];
}
-(void) centerMapToAnnotation:(NSNotification *)note
{
// Type dispatching annotation object
CLPinAnnotationView *annotation = (CLPinAnnotationView *)[note object];
// animate the map to the pin's coordinates
[self setCenterCoordinate:annotation.annotation.coordinate animated: YES];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end