/
MKMapView.j
286 lines (239 loc) · 7.18 KB
/
MKMapView.j
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
@import <AppKit/CPView.j>
@import "MKMapScene.j"
@import "MKMarker.j"
@import "MKLocation.j"
@import "MKPolyline.j"
/* a "class" variable that will hold the domWin.google.maps object/"namespace" */
var gmNamespace = nil;
MKLoadingMarkupWhiteSpinner = @"<div style='position: absolute; top:50%; left:50%;'><img src='Frameworks/MapKit/Resources/spinner-white.gif'/></div>";
MKLoadingMarkupBlackSpinner = @"<div style='position: absolute; top:50%; left:50%;'><img src='Frameworks/MapKit/Resources/spinner-black.gif'/></div>";
@implementation CPWebView(ScrollFixes)
{
- (void)loadHTMLStringWithoutMessingUpScrollbars:(CPString)aString
{
[self _startedLoading];
_ignoreLoadStart = YES;
_ignoreLoadEnd = NO;
_url = null;
_html = aString;
[self _load];
}
}
@end
@implementation MKMapView : CPWebView
{
CPString _apiKey;
DOMElement _DOMMapElement;
JSObject _gMap @accessors(property=gMap);
MKMapScene _scene @accessors(property=scene);
BOOL _mapReady;
BOOL _googleAjaxLoaded;
id delegate @accessors;
BOOL hasLoaded;
MKLocation _center;
CPString _centerName;
int _zoomLevel;
CPView _loadingView @accessors(property=loadingView);
}
- (id)initWithFrame:(CGRect)aFrame apiKey:(CPString)apiKey
{
return [self initWithFrame:aFrame apiKey:apiKey center:nil];
}
- (id)initWithFrame:(CGRect)aFrame apiKey:(CPString)apiKey center:(MKLocation)aLocation
{
return [self initWithFrame:aFrame apiKey:apiKey center:aLocation loadingMarkup:nil];
}
- (id)initWithFrame:(CGRect)aFrame
apiKey:(CPString)apiKey
center:(MKLocation)aLocation
loadingMarkup:(CPString)someLoadingMarkup
{
return [self initWithFrame:aFrame apiKey:apiKey center:aLocation loadingMarkup:someLoadingMarkup loadingView:nil];
}
- (id)initWithFrame:(CGRect)aFrame
apiKey:(CPString)apiKey
center:(MKLocation)aLocation
loadingView:(CPString)aLoadingView
{
return [self initWithFrame:aFrame apiKey:apiKey center:aLocation loadingMarkup:nil loadingView:aLoadingView];
}
- (id)initWithFrame:(CGRect)aFrame
apiKey:(CPString)apiKey
center:(MKLocation)aLocation
loadingMarkup:(CPString)someLoadingMarkup
loadingView:(CPString)aLoadingView
{
_apiKey = apiKey;
_center = aLocation;
_zoomLevel = 6;
if (!_center)
{
_center = [MKLocation locationWithLatitude:52 andLongitude:-1];
}
if (!someLoadingMarkup)
{
someLoadingMarkup = @"";
}
if (self = [super initWithFrame:aFrame])
{
_scene = [[MKMapScene alloc] initWithMapView:self];
_iframe.allowTransparency = true;
var bounds = [self bounds];
[self setFrameLoadDelegate:self];
[self loadHTMLStringWithoutMessingUpScrollbars:@"<html><head></head><body style='padding:0px; margin:0px; background-color:transparent'><div id='MKMapViewDiv' style='left: 0px; top: 0px; width: 100%; height: 100%'>" + someLoadingMarkup + "</div></body><script type=\"text/javascript\" src=\"http://www.google.com/jsapi?key=" + _apiKey + "\"></script></html>"];
if (aLoadingView)
{
_loadingView = aLoadingView;
[self addSubview:_loadingView];
}
}
return self;
}
- (void)webView:(CPWebView)aWebView didFinishLoadForFrame:(id)aFrame
{
// this is called twice for some reason
if(!hasLoaded)
{
[self loadGoogleMapsWhenReady];
}
hasLoaded = YES;
}
- (void)loadGoogleMapsWhenReady()
{
var domWin = [self DOMWindow];
if (typeof(domWin.google) === 'undefined')
{
domWin.window.setTimeout(function() {[self loadGoogleMapsWhenReady];}, 100);
}
else
{
var googleScriptElement = domWin.document.createElement('script');
domWin.mapsJsLoaded = function ()
{
//alert('mapsJsLoaded!');
_googleAjaxLoaded = YES;
_DOMMapElement = domWin.document.getElementById('MKMapViewDiv');
[self createMap];
};
googleScriptElement.innerHTML = "google.load('maps', '2', {'callback': mapsJsLoaded});"
domWin.document.getElementsByTagName('head')[0].appendChild(googleScriptElement);
}
}
- (void)createMap
{
var domWin = [self DOMWindow];
//remember the google maps namespace, but only once because it's a class variable
if (!gmNamespace)
{
gmNamespace = domWin.google.maps;
}
// for some things the current google namespace needs to be used...
var localGmNamespace = domWin.google.maps;
//console.log("Creating map");
_gMap = new localGmNamespace.Map2(_DOMMapElement, {backgroundColor: 'transparent'});
//_gMap.addMapType(G_SATELLITE_3D_MAP);
_gMap.setMapType(localGmNamespace.G_PHYSICAL_MAP);
_gMap.setUIToDefault();
_gMap.enableContinuousZoom();
_gMap.setCenter([_center googleLatLng], 8);
_gMap.setZoom(_zoomLevel);
// Hack to get mouse up event to work
localGmNamespace.Event.addDomListener(document.body, 'mouseup', function() { try { localGmNamespace.Event.trigger(domWin, 'mouseup'); } catch(e){} });
_mapReady = YES;
if (_loadingView) {
[_loadingView removeFromSuperview];
}
if (delegate && [delegate respondsToSelector:@selector(mapViewIsReady:)])
{
[delegate mapViewIsReady:self];
}
}
- (void)setFrameSize:(CGSize)aSize
{
[super setFrameSize:aSize];
var bounds = [self bounds];
if (_gMap)
{
_gMap.checkResize();
}
}
/* Overriding CPWebView's implementation */
- (BOOL)_resizeWebFrame
{
var width = [self bounds].size.width,
height = [self bounds].size.height;
_iframe.setAttribute("width", width);
_iframe.setAttribute("height", height);
[_frameView setFrameSize:CGSizeMake(width, height)];
}
- (void)viewDidMoveToSuperview
{
if (!_mapReady && _googleAjaxLoaded)
{
[self createMap];
}
[super viewDidMoveToSuperview];
}
- (void)setCenter:(MKLocation)aLocation
{
_center = aLocation;
if (_mapReady)
{
_gMap.setCenter([aLocation googleLatLng]);
}
}
- (MKLocation)center
{
return _center;
}
- (void)setZoom:(int)aZoomLevel
{
_zoomLevel = aZoomLevel;
if (_mapReady)
{
_gMap.setZoom(_zoomLevel);
}
}
- (MKMarker)addMarker:(MKMarker)aMarker atLocation:(MKLocation)aLocation
{
if (_mapReady)
{
var gMarker = [aMarker gMarker];
gMarker.setLatLng([aLocation googleLatLng]);
_gMap.addOverlay(gMarker);
}
else
{
// TODO some sort of queue?
}
return marker;
}
- (void)clearOverlays
{
if (_mapReady)
{
_gMap.clearOverlays();
}
}
- (void)addMapItem:(MKMapItem)mapItem
{
[mapItem addToMapView:self];
}
- (BOOL)isMapReady
{
return _mapReady;
}
- (JSObject)gmNamespace
{
var domWin = [self DOMWindow];
if (domWin && _mapReady)
{
return domWin.google.maps;
}
return nil;
}
+ (JSObject)gmNamespace
{
return gmNamespace;
}
@end