forked from nst/CocoaSlideShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSSMapController.m
178 lines (134 loc) · 5.56 KB
/
CSSMapController.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// MapController.m
// CocoaSlideShow
//
// Created by Nicolas Seriot on 14.11.08.
// Copyright 2008 Sen:te. All rights reserved.
//
#import "CSSMapController.h"
#import "CSSImageInfo.h"
#import "AppDelegate.h"
#import "NSImage+CSS.h"
NSString *const G_NORMAL_MAP = @"G_NORMAL_MAP";
NSString *const G_HYBRID_MAP = @"G_HYBRID_MAP";
NSString *const G_SATELLITE_MAP = @"G_SATELLITE_MAP";
NSString *const G_PHYSICAL_MAP = @"G_PHYSICAL_MAP";
static NSString *const kMapStyle = @"mapStyle";
static NSString *const kMapZoom = @"mapZoom";
@implementation CSSMapController
- (void)awakeFromNib {
displayedImages = [[NSMutableSet alloc] init];
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"gmap" ofType:@"html"];
//
// NSURL *url = [NSURL fileURLWithPath:filePath];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// // FIXME: webView frame should be set according to current window size, but it doesn't work..
// [webView setFrame:NSMakeRect(0.0, 0.0, 1500.0, 1000.0)];
//
// [webView setFrameLoadDelegate:self];
// [[webView mainFrame] loadRequest:request];
//
// scriptObject = [webView windowScriptObject];
// [scriptObject setValue:self forKey:@"mapController"];
}
/*
+ (NSString *)webScriptNameForSelector:(SEL)sel {
if (sel == @selector(setMapType:))
return @"setMapType";
return nil;
}
*/
- (void)mapTypeDidChange:(id)mapType {
//NSLog(@"-- mapTypeDidChange:%@", mapType);
if([[self mapStyles] containsObject:mapType]) {
[[NSUserDefaults standardUserDefaults] setValue:mapType forKey:kMapStyle];
}
}
- (void)zoomLevelDidChange:(id)zoomLevel {
//NSLog(@"-- zoomLevelDidChange:%@", zoomLevel);
[[NSUserDefaults standardUserDefaults] setValue:zoomLevel forKey:kMapZoom];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }
- (NSArray *)mapStyles {
return [NSArray arrayWithObjects:G_PHYSICAL_MAP, G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, nil];
}
- (void)clearMap {
// [[webView mainFrame] loadRequest:nil];
assert([mapView isKindOfClass:[MKMapView class]]);
NSArray *annotations = [mapView annotations];
NSLog(@"-- %@", annotations);
[mapView removeAnnotations:annotations];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if(object == imagesController && [keyPath isEqualToString:@"selectedObjects"]) {
[self evaluateNewJavaScriptOnSelectedObjectsChange];
} else if(object == imagesController && [keyPath isEqualToString:@"arrangedObjects"]) {
[self evaluateNewJavaScriptOnArrangedObjectsChange];
}
}
- (void)evaluateNewJavaScriptOnSelectedObjectsChange {
//NSLog(@"-- evaluateNewJavaScriptOnSelectedObjectsChange");
NSMutableSet *toShow = [NSMutableSet setWithArray:[imagesController selectedObjects]];
[toShow minusSet:displayedImages];
NSMutableSet *toHide = [displayedImages mutableCopy];
[toHide minusSet:[NSMutableSet setWithArray:[imagesController selectedObjects]]];
NSMutableArray *jsCommands = [NSMutableArray array];
for(CSSImageInfo *imageInfo in toHide) {
NSString *jsHidePoint = [imageInfo jsHidePoint];
if(!jsHidePoint) continue;
[jsCommands addObject:jsHidePoint];
[displayedImages removeObject:imageInfo];
}
for(CSSImageInfo *imageInfo in toShow) {
NSString *jsShowPoint = [imageInfo jsShowPoint];
if(!jsShowPoint) continue;
[jsCommands addObject:jsShowPoint];
[displayedImages addObject:imageInfo];
}
CSSImageInfo *lastObject = [[imagesController selectedObjects] lastObject];
NSString *lat = [lastObject prettyLatitude];
NSString *lon = [lastObject prettyLongitude];
NSString *zoom = [[NSUserDefaults standardUserDefaults] valueForKey:kMapZoom];
if([lat length] && [lon length] && zoom) {
[jsCommands addObject:[NSString stringWithFormat:@"centerToLatitudeAndLongitudeWithZoom(%@, %@, %@);", lat, lon, zoom]];
}
//NSString *js = [jsCommands componentsJoinedByString:@"\n"];
//NSLog(@"-- \n%@", js);
// [webView stringByEvaluatingJavaScriptFromString:js];
}
- (void)evaluateNewJavaScriptOnArrangedObjectsChange {
//NSLog(@"-- evaluateNewJavaScriptOnArrangedObjectsChange");
NSMutableSet *toAdd = [NSMutableSet setWithArray:[imagesController arrangedObjects]];
[toAdd minusSet:displayedImages];
NSMutableSet *toRemove = [displayedImages mutableCopy];
[toRemove minusSet:[NSSet setWithArray:[imagesController arrangedObjects]]];
NSMutableArray *jsCommands = [NSMutableArray array];
for(CSSImageInfo *imageInfo in toRemove) {
NSString *jsRemovePoint = [imageInfo jsRemovePoint];
if(!jsRemovePoint) continue;
[jsCommands addObject:jsRemovePoint];
[displayedImages removeObject:imageInfo];
}
for(CSSImageInfo *imageInfo in toAdd) {
NSString *jsAddPoint = [imageInfo jsAddPoint];
if(!jsAddPoint) continue;
[jsCommands addObject:jsAddPoint];
//NSLog(@" -- add %d", [imageInfo hash]);
}
//NSString *js = [jsCommands componentsJoinedByString:@"\n"];
//NSLog(@"-- \n%@", js);
// [webView stringByEvaluatingJavaScriptFromString:js];
[self evaluateNewJavaScriptOnSelectedObjectsChange];
}
#pragma mark WebFrameLoadDelegate
//- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
//
// NSString *mapStyle = [[NSUserDefaults standardUserDefaults] stringForKey:kMapStyle];
// if(!mapStyle || ![[self mapStyles] containsObject:mapStyle]) {
// mapStyle = G_PHYSICAL_MAP;
// [[NSUserDefaults standardUserDefaults] setValue:mapStyle forKey:kMapStyle];
// }
// [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setMapStyle(%@);", mapStyle]];
//}
@end