-
Notifications
You must be signed in to change notification settings - Fork 920
Expand file tree
/
Copy pathPluginMap.m
More file actions
1030 lines (849 loc) · 40.7 KB
/
PluginMap.m
File metadata and controls
1030 lines (849 loc) · 40.7 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PluginMap.m
// cordova-googlemaps-plugin v2
//
// Created by Masashi Katsumata.
//
//
#import "PluginMap.h"
@implementation PluginMap
-(void)setPluginViewController:(PluginViewController *)viewCtrl
{
self.mapCtrl = (PluginMapViewController *)viewCtrl;
}
- (void)pluginInitialize
{
if (self.mapCtrl.objects) {
return;
}
self.initialized = YES;
// Initialize this plugin
}
- (void)pluginUnload
{
// Plugin destroy
self.isRemoved = YES;
// Load the GoogleMap.m
//CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
//CordovaGoogleMaps *googlemaps = [cdvViewController getCommandInstance:@"CordovaGoogleMaps"];
[self clear:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapCtrl.map removeFromSuperview];
[self.mapCtrl.view removeFromSuperview];
[self.mapCtrl.view setFrame:CGRectMake(0, -1000, 100, 100)];
[self.mapCtrl.view setNeedsDisplay];
NSArray *keys = [self.mapCtrl.plugins allKeys];
CDVPlugin<IPluginProtocol> *plugin;
for (int i = 0; i < [keys count]; i++) {
plugin = [self.mapCtrl.plugins objectForKey:[keys objectAtIndex:i]];
[plugin pluginUnload];
plugin = nil;
}
[self.mapCtrl.plugins removeAllObjects];
self.mapCtrl.map = nil;
self.mapCtrl = nil;
});
}
- (void)loadPlugin:(CDVInvokedUrlCommand*)command {
NSString *className = [command.arguments objectAtIndex:0];
NSString *pluginName = [NSString stringWithFormat:@"%@", className];
className = [NSString stringWithFormat:@"Plugin%@", className];
@synchronized (self.mapCtrl.plugins) {
CDVPluginResult* pluginResult = nil;
CDVPlugin<IPluginProtocol> *plugin;
NSString *pluginId = [NSString stringWithFormat:@"%@-%@", self.mapCtrl.overlayId, [pluginName lowercaseString]];
plugin = [self.mapCtrl.plugins objectForKey:pluginId];
if (!plugin) {
plugin = [[NSClassFromString(className)alloc] init];
if (!plugin) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:[NSString stringWithFormat:@"Class not found: %@", className]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
// Hack:
// In order to load the plugin instance of the same class but different names,
// register the map plugin instance into the pluginObjects directly.
//
// Don't use the registerPlugin() method of the CDVViewController.
// Problem is at
// https://github.com/apache/cordova-ios/blob/582e35776f01ee03f32f0986de181bcf5eb4d232/CordovaLib/Classes/Public/CDVViewController.m#L577
//
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
if ([plugin respondsToSelector:@selector(setViewController:)]) {
[plugin setViewController:cdvViewController];
}
if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
[plugin setCommandDelegate:cdvViewController.commandDelegate];
}
[cdvViewController.pluginObjects setObject:plugin forKey:pluginId];
[cdvViewController.pluginsMap setValue:pluginId forKey:pluginId];
[plugin pluginInitialize];
//NSLog(@"--->loadPlugin : %@ className : %@, plugin : %@", pluginId, className, plugin);
[self.mapCtrl.plugins setObject:plugin forKey:pluginId];
[plugin setPluginViewController:self.mapCtrl];
}
//plugin.commandDelegate = self.commandDelegate;
SEL selector = NSSelectorFromString(@"create:");
if ([plugin respondsToSelector:selector]){
[plugin performSelectorInBackground:selector withObject:command];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:[NSString stringWithFormat:@"method not found: %@ in %@ class", @"create", className]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
}
- (void)getMap:(CDVInvokedUrlCommand*)command {
if ([command.arguments count] == 1) {
//-----------------------------------------------------------------------
// case: plugin.google.maps.getMap([options]) (no the mapDiv is given)
//-----------------------------------------------------------------------
[self setOptions:command];
//[self.mapCtrl.view setHidden:true];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
NSDictionary *meta = [command.arguments objectAtIndex:0];
self.mapCtrl.viewDepth = [[meta objectForKey:@"depth"] integerValue];
NSDictionary *initOptions = [command.arguments objectAtIndex:1];
if ([initOptions valueForKey:@"camera"] && [initOptions valueForKey:@"camera"] != [NSNull null]) {
double delayInSeconds = 1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self _setOptions:initOptions requestMethod:@"getMap" command:command];
});
} else {
[self _setOptions:initOptions requestMethod:@"getMap" command:command];
}
}
- (void)setDiv:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Load the GoogleMap.m
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
CordovaGoogleMaps *googlemaps = [cdvViewController getCommandInstance:@"CordovaGoogleMaps"];
// Detach the map view
if ([command.arguments count] == 0) {
[googlemaps.pluginLayer removePluginOverlay:self.mapCtrl];
self.mapCtrl.attached = NO;
self.mapCtrl.view = nil;
} else {
self.mapCtrl.view = self.mapCtrl.map;
[googlemaps.pluginLayer addPluginOverlay:self.mapCtrl];
NSString *mapDivId = [command.arguments objectAtIndex:0];
self.mapCtrl.divId = mapDivId;
self.mapCtrl.attached = YES;
self.mapCtrl.isRenderedAtOnce = NO; //prevent unexpected animation
[googlemaps.pluginLayer updateViewPosition:self.mapCtrl];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)attachToWebView:(CDVInvokedUrlCommand*)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
// Load the GoogleMap.m
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
CordovaGoogleMaps *googlemaps = [cdvViewController getCommandInstance:@"CordovaGoogleMaps"];
[googlemaps.pluginLayer addPluginOverlay:self.mapCtrl];
self.mapCtrl.attached = YES;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)detachFromWebView:(CDVInvokedUrlCommand*)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
// Load the GoogleMap.m
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
CordovaGoogleMaps *googlemaps = [cdvViewController getCommandInstance:@"CordovaGoogleMaps"];
[googlemaps.pluginLayer removePluginOverlay:self.mapCtrl];
self.mapCtrl.attached = NO;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)resizeMap:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSString *mapDivId = self.mapCtrl.divId;
if (!mapDivId) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
// Load the GoogleMap.m
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
CordovaGoogleMaps *googlemaps = [cdvViewController getCommandInstance:@"CordovaGoogleMaps"];
// Save the map rectangle.
if (![googlemaps.pluginLayer.pluginScrollView.HTMLNodes objectForKey:self.mapCtrl.divId]) {
NSMutableDictionary *dummyInfo = [[NSMutableDictionary alloc] init];;
[dummyInfo setObject:@"{{0,-3000} - {50,50}}" forKey:@"size"];
[dummyInfo setObject:[NSNumber numberWithDouble:-999] forKey:@"depth"];
[googlemaps.pluginLayer.pluginScrollView.HTMLNodes setObject:dummyInfo forKey:self.mapCtrl.divId];
}
dispatch_async(dispatch_get_main_queue(), ^{
[googlemaps.pluginLayer updateViewPosition:self.mapCtrl];
//[googlemaps.pluginLayer updateViewPosition:self.mapCtrl];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
});
}];
}
-(void)setClickable:(CDVInvokedUrlCommand *)command
{
[self.mapCtrl.executeQueue addOperationWithBlock:^{
Boolean isClickable = [[command.arguments objectAtIndex:0] boolValue];
self.mapCtrl.clickable = isClickable;
//self.debugView.clickable = isClickable;
//[self.pluginScrollView.debugView setNeedsDisplay];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
/**
* Clear all markups
*/
- (void)clear:(CDVInvokedUrlCommand *)command {
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapCtrl.map clear];
});
CDVViewController *cdvViewController = (CDVViewController*)self.viewController;
CDVPlugin<IPluginProtocol> *plugin;
NSString *pluginName;
NSArray *keys = [self.mapCtrl.plugins allKeys];
for (int j = 0; j < [keys count]; j++) {
pluginName = [keys objectAtIndex:j];
plugin = [self.mapCtrl.plugins objectForKey:pluginName];
[plugin pluginUnload];
[cdvViewController.pluginObjects removeObjectForKey:pluginName];
[cdvViewController.pluginsMap setValue:nil forKey:pluginName];
//plugin = nil;
}
[self.mapCtrl.plugins removeAllObjects];
if (command != (id)[NSNull null]) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
/**
* Move the center of the map
*/
- (void)setCameraTarget:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
float latitude = [[command.arguments objectAtIndex:0] floatValue];
float longitude = [[command.arguments objectAtIndex:1] floatValue];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.mapCtrl.map animateToLocation:CLLocationCoordinate2DMake(latitude, longitude)];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setMyLocationEnabled:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSDictionary *params =[command.arguments objectAtIndex:0];
self.mapCtrl.map.settings.myLocationButton = [[params valueForKey:@"myLocationButton"] boolValue];
self.mapCtrl.map.myLocationEnabled = [[params valueForKey:@"myLocation"] boolValue];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setIndoorEnabled:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
Boolean isEnabled = [[command.arguments objectAtIndex:0] boolValue];
self.mapCtrl.map.settings.indoorPicker = isEnabled;
self.mapCtrl.map.indoorEnabled = isEnabled;
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setTrafficEnabled:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
Boolean isEnabled = [[command.arguments objectAtIndex:0] boolValue];
self.mapCtrl.map.trafficEnabled = isEnabled;
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setCompassEnabled:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
Boolean isEnabled = [[command.arguments objectAtIndex:0] boolValue];
self.mapCtrl.map.settings.compassButton = isEnabled;
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setVisible:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
BOOL isVisible = [[command.arguments objectAtIndex:0] boolValue];
[self.mapCtrl.view setHidden:!isVisible];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setCameraTilt:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
double angle = [[command.arguments objectAtIndex:0] doubleValue];
if (angle >=0 && angle <= 90) {
GMSCameraPosition *camera = self.mapCtrl.map.camera;
camera = [GMSCameraPosition cameraWithLatitude:camera.target.latitude
longitude:camera.target.longitude
zoom:camera.zoom
bearing:camera.bearing
viewingAngle:angle];
[self.mapCtrl.map setCamera:camera];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setCameraBearing:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
double bearing = [[command.arguments objectAtIndex:0] doubleValue];
GMSCameraPosition *camera = self.mapCtrl.map.camera;
camera = [GMSCameraPosition cameraWithLatitude:camera.target.latitude
longitude:camera.target.longitude
zoom:camera.zoom
bearing:bearing
viewingAngle:camera.viewingAngle];
[self.mapCtrl.map setCamera:camera];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setAllGesturesEnabled:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
Boolean isEnabled = [[command.arguments objectAtIndex:0] boolValue];
[self.mapCtrl.map.settings setAllGesturesEnabled:isEnabled];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
/**
* Change the zoom level
*/
- (void)setCameraZoom:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
float zoom = [[command.arguments objectAtIndex:0] floatValue];
CLLocationCoordinate2D center = [self.mapCtrl.map.projection coordinateForPoint:self.mapCtrl.map.center];
[self.mapCtrl.map setCamera:[GMSCameraPosition cameraWithTarget:center zoom:zoom]];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
/**
* Pan by
*/
- (void)panBy:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
int x = [[command.arguments objectAtIndex:0] intValue];
int y = [[command.arguments objectAtIndex:1] intValue];
[self.mapCtrl.map animateWithCameraUpdate:[GMSCameraUpdate scrollByX:x * -1 Y:y * -1]];
}];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
/**
* Change the Map Type
*/
- (void)setMapTypeId:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
CDVPluginResult* pluginResult = nil;
NSString *typeStr = [command.arguments objectAtIndex:0];
NSDictionary *mapTypes = [NSDictionary dictionaryWithObjectsAndKeys:
^() {return kGMSTypeHybrid; }, @"MAP_TYPE_HYBRID",
^() {return kGMSTypeSatellite; }, @"MAP_TYPE_SATELLITE",
^() {return kGMSTypeTerrain; }, @"MAP_TYPE_TERRAIN",
^() {return kGMSTypeNormal; }, @"MAP_TYPE_NORMAL",
^() {return kGMSTypeNone; }, @"MAP_TYPE_NONE",
nil];
typedef GMSMapViewType (^CaseBlock)();
GMSMapViewType mapType;
CaseBlock caseBlock = mapTypes[typeStr];
if (caseBlock) {
// Change the map type
mapType = caseBlock();
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.mapCtrl.map.mapType = mapType;
}];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
// Error : User specifies unknow map type id
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:[NSString
stringWithFormat:@"Unknow MapTypeID is specified:%@", typeStr]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
/**
* Move the map camera with animation
*/
-(void)animateCamera:(CDVInvokedUrlCommand *)command
{
[self updateCameraPosition:@"animateCamera" command:command];
}
/**
* Move the map camera
*/
-(void)moveCamera:(CDVInvokedUrlCommand *)command
{
[self updateCameraPosition:@"moveCamera" command:command];
}
-(void)_changeCameraPosition: (NSString*)action requestMethod:(NSString *)requestMethod params:(NSDictionary *)json command:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
__block double bearing;
if ([json valueForKey:@"bearing"] && [json valueForKey:@"bearing"] != [NSNull null]) {
bearing = [[json valueForKey:@"bearing"] doubleValue];
} else {
bearing = self.mapCtrl.map.camera.bearing;
}
double angle;
if ([json valueForKey:@"tilt"] && [json valueForKey:@"tilt"] != [NSNull null]) {
angle = [[json valueForKey:@"tilt"] doubleValue];
} else {
angle = self.mapCtrl.map.camera.viewingAngle;
}
double zoom;
if ([json valueForKey:@"zoom"] && [json valueForKey:@"zoom"] != [NSNull null]) {
zoom = [[json valueForKey:@"zoom"] doubleValue];
} else {
zoom = self.mapCtrl.map.camera.zoom;
}
double cameraPadding = 20;
if ([json valueForKey:@"padding"] && [json valueForKey:@"zoom"] != [NSNull null]) {
cameraPadding = [[json valueForKey:@"padding"] doubleValue];
}
NSDictionary *latLng = nil;
double latitude;
double longitude;
GMSCameraPosition *cameraPosition;
GMSCoordinateBounds *cameraBounds = nil;
CGFloat scale = self.mapCtrl.screenScale;
UIEdgeInsets paddingUiEdgeInsets = UIEdgeInsetsMake(cameraPadding / scale, cameraPadding / scale, cameraPadding / scale, cameraPadding / scale);
if ([json objectForKey:@"target"]) {
NSString *targetClsName = [[json objectForKey:@"target"] className];
if ([targetClsName isEqualToString:@"__NSCFArray"] || [targetClsName isEqualToString:@"__NSArrayM"] ) {
//--------------------------------------------
// cameraPosition.target = [
// new plugin.google.maps.LatLng(),
// ...
// new plugin.google.maps.LatLng()
// ]
//---------------------------------------------
int i = 0;
NSArray *latLngList = [json objectForKey:@"target"];
GMSMutablePath *path = [GMSMutablePath path];
for (i = 0; i < [latLngList count]; i++) {
latLng = [latLngList objectAtIndex:i];
latitude = [[latLng valueForKey:@"lat"] doubleValue];
longitude = [[latLng valueForKey:@"lng"] doubleValue];
[path addLatitude:latitude longitude:longitude];
}
cameraBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
//CLLocationCoordinate2D center = cameraBounds.center;
cameraPosition = [self.mapCtrl.map cameraForBounds:cameraBounds insets:paddingUiEdgeInsets];
} else {
//------------------------------------------------------------------
// cameraPosition.target = new plugin.google.maps.LatLng();
//------------------------------------------------------------------
latLng = [json objectForKey:@"target"];
latitude = [[latLng valueForKey:@"lat"] doubleValue];
longitude = [[latLng valueForKey:@"lng"] doubleValue];
cameraPosition = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:zoom
bearing:bearing
viewingAngle:angle];
}
} else {
cameraPosition = [GMSCameraPosition cameraWithLatitude:self.mapCtrl.map.camera.target.latitude
longitude:self.mapCtrl.map.camera.target.longitude
zoom:zoom
bearing:bearing
viewingAngle:angle];
}
float duration = 5.0f;
if ([json objectForKey:@"duration"]) {
duration = [[json objectForKey:@"duration"] floatValue] / 1000;
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
if ([action isEqual: @"animateCamera"]) {
[CATransaction begin]; {
[CATransaction setAnimationDuration: duration];
//[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
if (self.isRemoved) {
return;
}
if (cameraBounds != (id)[NSNull null]){
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:self.mapCtrl.map.camera.target.latitude
longitude:self.mapCtrl.map.camera.target.longitude
zoom:self.mapCtrl.map.camera.zoom
bearing:bearing
viewingAngle:angle];
[self.mapCtrl.map setCamera:cameraPosition2];
} else {
if (bearing == 0) {
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:self.mapCtrl.map.projection.visibleRegion];
[self.mapCtrl.map cameraForBounds:bounds insets:paddingUiEdgeInsets];
}
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
[self.mapCtrl.map animateToCameraPosition: cameraPosition];
}[CATransaction commit];
}
if ([action isEqual: @"moveCamera"]) {
[self.mapCtrl.map setCamera:cameraPosition];
if (cameraBounds != (id)[NSNull null]){
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:self.mapCtrl.map.camera.target.latitude
longitude:self.mapCtrl.map.camera.target.longitude
zoom:self.mapCtrl.map.camera.zoom
bearing:bearing
viewingAngle:angle];
if (self.isRemoved) {
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId];
return;
}
[self.mapCtrl.map setCamera:cameraPosition2];
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:self.mapCtrl.map.projection.visibleRegion];
[self.mapCtrl.map cameraForBounds:bounds insets:paddingUiEdgeInsets];
[self.mapCtrl.view setHidden:NO];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
});
});
} else {
[self.mapCtrl.view setHidden:NO];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
}];
}
-(void)updateCameraPosition: (NSString*)action command:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSDictionary *json = [command.arguments objectAtIndex:0];
[self _changeCameraPosition:action requestMethod:@"updateCameraPosition" params:json command:command];
}];
}
- (void)setActiveMarkerId:(CDVInvokedUrlCommand*)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSString *markerId = [command.arguments objectAtIndex:0];
GMSMarker *marker = [self.mapCtrl.objects objectForKey:markerId];
if (marker != (id)[NSNull null]) {
self.mapCtrl.map.selectedMarker = marker;
self.mapCtrl.activeMarker = marker;
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)toDataURL:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSDictionary *opts = [command.arguments objectAtIndex:0];
BOOL uncompress = NO;
if ([opts objectForKey:@"uncompress"]) {
uncompress = [[opts objectForKey:@"uncompress"] boolValue];
}
if (uncompress) {
UIGraphicsBeginImageContextWithOptions(self.mapCtrl.view.frame.size, NO, 0.0f);
} else {
UIGraphicsBeginImageContext(self.mapCtrl.view.frame.size);
}
[self.mapCtrl.view drawViewHierarchyInRect:self.mapCtrl.map.layer.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSData *imageData = UIImagePNGRepresentation(image);
NSString* base64Encoded = [imageData base64EncodedStringWithOptions:0];
NSString* base64EncodedWithData = [@"data:image/png;base64," stringByAppendingString:base64Encoded];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:base64EncodedWithData];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}];
}
/**
* Maps an Earth coordinate to a point coordinate in the map's view.
*/
- (void)fromLatLngToPoint:(CDVInvokedUrlCommand*)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
float latitude = [[command.arguments objectAtIndex:0] floatValue];
float longitude = [[command.arguments objectAtIndex:1] floatValue];
CGPoint point = [self.mapCtrl.map.projection
pointForCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSMutableArray *pointJSON = [[NSMutableArray alloc] init];
[pointJSON addObject:[NSNumber numberWithDouble:point.x]];
[pointJSON addObject:[NSNumber numberWithDouble:point.y]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:pointJSON];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}];
}
/**
* Maps a point coordinate in the map's view to an Earth coordinate.
*/
- (void)fromPointToLatLng:(CDVInvokedUrlCommand*)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
float pointX = [[command.arguments objectAtIndex:0] floatValue];
float pointY = [[command.arguments objectAtIndex:1] floatValue];
CLLocationCoordinate2D latLng = [self.mapCtrl.map.projection
coordinateForPoint:CGPointMake(pointX, pointY)];
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSMutableArray *latLngJSON = [[NSMutableArray alloc] init];
[latLngJSON addObject:[NSNumber numberWithDouble:latLng.latitude]];
[latLngJSON addObject:[NSNumber numberWithDouble:latLng.longitude]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:latLngJSON];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}];
}
- (void)_setOptions:(NSDictionary *)initOptions requestMethod:(NSString *)requestMethod command:(CDVInvokedUrlCommand *)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"options=%@", initOptions);
BOOL isEnabled = NO;
//controls
NSDictionary *controls = [initOptions objectForKey:@"controls"];
if (controls) {
//compass
if ([controls valueForKey:@"compass"] != (id)[NSNull null]) {
isEnabled = [[controls valueForKey:@"compass"] boolValue];
if (isEnabled == true) {
self.mapCtrl.map.settings.compassButton = YES;
} else {
self.mapCtrl.map.settings.compassButton = NO;
}
}
//myLocationButton
if ([controls valueForKey:@"myLocationButton"] != (id)[NSNull null]) {
isEnabled = [[controls valueForKey:@"myLocationButton"] boolValue];
if (isEnabled == true) {
self.mapCtrl.map.settings.myLocationButton = YES;
} else {
self.mapCtrl.map.settings.myLocationButton = NO;
}
}
//myLocation
if ([controls valueForKey:@"myLocation"] != (id)[NSNull null]) {
isEnabled = [[controls valueForKey:@"myLocation"] boolValue];
if (isEnabled == true) {
self.mapCtrl.map.myLocationEnabled = YES;
} else {
self.mapCtrl.map.myLocationEnabled = NO;
}
}
//indoorPicker
if ([controls valueForKey:@"indoorPicker"] != (id)[NSNull null]) {
isEnabled = [[controls valueForKey:@"indoorPicker"] boolValue];
if (isEnabled == true) {
self.mapCtrl.map.settings.indoorPicker = YES;
} else {
self.mapCtrl.map.settings.indoorPicker = NO;
}
}
} else {
self.mapCtrl.map.settings.compassButton = YES;
}
//gestures
NSDictionary *gestures = [initOptions objectForKey:@"gestures"];
if (gestures) {
//rotate
if ([gestures valueForKey:@"rotate"] != (id)[NSNull null]) {
isEnabled = [[gestures valueForKey:@"rotate"] boolValue];
self.mapCtrl.map.settings.rotateGestures = isEnabled;
}
//scroll
if ([gestures valueForKey:@"scroll"] != (id)[NSNull null]) {
isEnabled = [[gestures valueForKey:@"scroll"] boolValue];
self.mapCtrl.map.settings.scrollGestures = isEnabled;
}
//tilt
if ([gestures valueForKey:@"tilt"] != (id)[NSNull null]) {
isEnabled = [[gestures valueForKey:@"tilt"] boolValue];
self.mapCtrl.map.settings.tiltGestures = isEnabled;
}
//zoom
if ([gestures valueForKey:@"zoom"] != (id)[NSNull null]) {
isEnabled = [[gestures valueForKey:@"zoom"] boolValue];
self.mapCtrl.map.settings.zoomGestures = isEnabled;
}
}
//preferences
NSDictionary *preferences = [initOptions objectForKey:@"preferences"];
if (preferences) {
//padding
if ([preferences valueForKey:@"padding"] != (id)[NSNull null]) {
NSDictionary *padding = [preferences valueForKey:@"padding"];
UIEdgeInsets current = self.mapCtrl.map.padding;
if ([padding objectForKey:@"left"] != (id)[NSNull null]) {
current.left = [[padding objectForKey:@"left"] floatValue];
}
if ([padding objectForKey:@"top"] != (id)[NSNull null]) {
current.top = [[padding objectForKey:@"top"] floatValue];
}
if ([padding objectForKey:@"bottom"] != (id)[NSNull null]) {
current.bottom = [[padding objectForKey:@"bottom"] floatValue];
}
if ([padding objectForKey:@"right"] != (id)[NSNull null]) {
current.right = [[padding objectForKey:@"right"] floatValue];
}
UIEdgeInsets newPadding = UIEdgeInsetsMake(current.top, current.left, current.bottom, current.right);
[self.mapCtrl.map setPadding:newPadding];
}
//zoom
if ([preferences valueForKey:@"zoom"] != (id)[NSNull null]) {
NSDictionary *zoom = [preferences valueForKey:@"zoom"];
float minZoom = self.mapCtrl.map.minZoom;
float maxZoom = self.mapCtrl.map.maxZoom;
if ([zoom objectForKey:@"minZoom"] != (id)[NSNull null]) {
minZoom = [[zoom objectForKey:@"minZoom"] doubleValue];
}
if ([zoom objectForKey:@"maxZoom"] != (id)[NSNull null]) {
maxZoom = [[zoom objectForKey:@"maxZoom"] doubleValue];
}
[self.mapCtrl.map setMinZoom:minZoom maxZoom:maxZoom];
}
// restriction
NSDictionary *restriction = [preferences valueForKey:@"restriction"];
if (restriction == (id)[NSNull null]) {
[self _setCameraRestriction:nil];
} else {
NSDictionary *restriction = [preferences objectForKey:@"restriction"];
[self _setCameraRestriction:restriction];
}
// building
if ([preferences valueForKey:@"building"] != nil) {
self.mapCtrl.map.buildingsEnabled = [[preferences valueForKey:@"building"] boolValue];
}
}
//styles
NSString *styles = [initOptions valueForKey:@"styles"];
if (styles) {
NSError *error;
GMSMapStyle *mapStyle = [GMSMapStyle styleWithJSONString:styles error:&error];
if (mapStyle != (id)[NSNull null]) {
self.mapCtrl.map.mapStyle = mapStyle;
self.mapCtrl.map.mapType = kGMSTypeNormal;
} else {
NSLog(@"Your specified map style is incorrect : %@", error.description);
}
} else {
//mapType
NSString *typeStr = [initOptions valueForKey:@"mapType"];
if (typeStr) {
NSDictionary *mapTypes = [NSDictionary dictionaryWithObjectsAndKeys:
^() {return kGMSTypeHybrid; }, @"MAP_TYPE_HYBRID",
^() {return kGMSTypeSatellite; }, @"MAP_TYPE_SATELLITE",
^() {return kGMSTypeTerrain; }, @"MAP_TYPE_TERRAIN",
^() {return kGMSTypeNormal; }, @"MAP_TYPE_NORMAL",
^() {return kGMSTypeNone; }, @"MAP_TYPE_NONE",
nil];
typedef GMSMapViewType (^CaseBlock)();
GMSMapViewType mapType;
CaseBlock caseBlock = mapTypes[typeStr];
if (caseBlock) {
// Change the map type
mapType = caseBlock();
self.mapCtrl.map.mapType = mapType;
}
}
}
// Redraw the map mandatory
[self.mapCtrl.map setNeedsDisplay];
if ([initOptions valueForKey:@"camera"] && [initOptions valueForKey:@"camera"] != [NSNull null]) {
//------------------------------------------
// Case : The camera option is specified.
//------------------------------------------
NSDictionary *cameraOpts = [initOptions objectForKey:@"camera"];
[self _changeCameraPosition:@"moveCamera" requestMethod:requestMethod params:cameraOpts command:command];
} else {
[self.mapCtrl mapView:self.mapCtrl.map didChangeCameraPosition:self.mapCtrl.map.camera];
[self.mapCtrl.view setHidden:NO];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
};
- (void)_setCameraRestriction:(NSDictionary *)params {
if (params == (id)[NSNull null] || params == nil) {
self.mapCtrl.map.cameraTargetBounds = nil;
double minZoom = 0;
double maxZoom = 23;
[self.mapCtrl.map setMinZoom:minZoom maxZoom:maxZoom];
} else {
GMSMutablePath *path = [GMSMutablePath path];
[path
addCoordinate: CLLocationCoordinate2DMake([[params objectForKey:@"south"] doubleValue], [[params objectForKey:@"west"] doubleValue])];
[path
addCoordinate: CLLocationCoordinate2DMake([[params objectForKey:@"north"] doubleValue], [[params objectForKey:@"east"] doubleValue])];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
self.mapCtrl.map.cameraTargetBounds = bounds;
double minZoom = [[params objectForKey:@"minZoom"] doubleValue];
double maxZoom = [[params objectForKey:@"maxZoom"] doubleValue];
[self.mapCtrl.map setMinZoom:minZoom maxZoom:maxZoom];
}
}
- (void)setOptions:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSDictionary *initOptions = [command.arguments objectAtIndex:0];
[initOptions setValue:@"setOptions" forKeyPath:@"method"];
[self _setOptions:initOptions requestMethod:@"setOptions" command:command];
}];
}
- (void)setPadding:(CDVInvokedUrlCommand *)command {
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSDictionary *paddingJson = [command.arguments objectAtIndex:0];
float top = [[paddingJson objectForKey:@"top"] floatValue];
float left = [[paddingJson objectForKey:@"left"] floatValue];
float right = [[paddingJson objectForKey:@"right"] floatValue];
float bottom = [[paddingJson objectForKey:@"bottom"] floatValue];
UIEdgeInsets padding = UIEdgeInsetsMake(top, left, bottom, right);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.mapCtrl.map setPadding:padding];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}];
}
- (void)getFocusedBuilding:(CDVInvokedUrlCommand*)command {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
GMSIndoorBuilding *building = self.mapCtrl.map.indoorDisplay.activeBuilding;
if (building != (id)[NSNull null] || [building.levels count] == 0) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
GMSIndoorLevel *activeLevel = self.mapCtrl.map.indoorDisplay.activeLevel;
[self.mapCtrl.executeQueue addOperationWithBlock:^{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSUInteger activeLevelIndex = [building.levels indexOfObject:activeLevel];
[result setObject:[NSNumber numberWithInteger:activeLevelIndex] forKey:@"activeLevelIndex"];
[result setObject:[NSNumber numberWithInteger:building.defaultLevelIndex] forKey:@"defaultLevelIndex"];