Skip to content

Commit fa8cbe0

Browse files
committed
issue #647 The target property of map.getMap(), map.setOptions(), moveCamera(), and animateCamera() accept Array<LatLng> with tilt and bearing properties. (iOS)
1 parent b04e47e commit fa8cbe0

2 files changed

Lines changed: 123 additions & 9 deletions

File tree

src/ios/GoogleMaps/GoogleMapsViewController.m

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ - (void)viewDidLoad
8282
float latitude;
8383
float longitude;
8484
GMSCameraPosition *camera;
85-
GMSCoordinateBounds *initBounds = nil;
85+
GMSCoordinateBounds *cameraBounds = nil;
8686

8787
if ([cameraOpts objectForKey:@"target"]) {
8888
NSString *targetClsName = [[cameraOpts objectForKey:@"target"] className];
@@ -102,9 +102,9 @@ - (void)viewDidLoad
102102
}
103103
[[UIScreen mainScreen] scale];
104104

105-
initBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
105+
cameraBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
106106

107-
CLLocationCoordinate2D center = initBounds.center;
107+
CLLocationCoordinate2D center = cameraBounds.center;
108108

109109
camera = [GMSCameraPosition cameraWithLatitude:center.latitude
110110
longitude:center.longitude
@@ -224,13 +224,22 @@ - (void)viewDidLoad
224224
[self.view addSubview: self.map];
225225

226226
dispatch_async(dispatch_get_main_queue(), ^{
227-
if (initBounds != nil) {
227+
if (cameraBounds != nil) {
228228
float scale = 1;
229229
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
230230
scale = [[UIScreen mainScreen] scale];
231231
}
232232
[[UIScreen mainScreen] scale];
233-
[self.map moveCamera:[GMSCameraUpdate fitBounds:initBounds withPadding:10 * scale]];
233+
[self.map moveCamera:[GMSCameraUpdate fitBounds:cameraBounds withPadding:10 * scale]];
234+
235+
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:cameraBounds.center.latitude
236+
longitude:cameraBounds.center.longitude
237+
zoom:self.map.camera.zoom
238+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
239+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
240+
241+
[self.map setCamera:cameraPosition2];
242+
234243
}
235244
});
236245
}

src/ios/GoogleMaps/Map.m

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ -(void)updateCameraPosition: (NSString*)action command:(CDVInvokedUrlCommand *)c
185185
float latitude;
186186
float longitude;
187187
GMSCameraPosition *cameraPosition;
188+
GMSCoordinateBounds *cameraBounds = nil;
189+
188190

189191
if ([json objectForKey:@"target"]) {
190192
NSString *targetClsName = [[json objectForKey:@"target"] className];
@@ -204,8 +206,11 @@ -(void)updateCameraPosition: (NSString*)action command:(CDVInvokedUrlCommand *)c
204206
}
205207
[[UIScreen mainScreen] scale];
206208

207-
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
208-
cameraPosition = [self.mapCtrl.map cameraForBounds:bounds insets:UIEdgeInsetsMake(10 * scale, 10* scale, 10* scale, 10* scale)];
209+
cameraBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
210+
//CLLocationCoordinate2D center = cameraBounds.center;
211+
212+
cameraPosition = [self.mapCtrl.map cameraForBounds:cameraBounds insets:UIEdgeInsetsMake(10 * scale, 10* scale, 10* scale, 10* scale)];
213+
209214
} else {
210215
latLng = [json objectForKey:@"target"];
211216
latitude = [[latLng valueForKey:@"lat"] floatValue];
@@ -240,6 +245,16 @@ -(void)updateCameraPosition: (NSString*)action command:(CDVInvokedUrlCommand *)c
240245
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
241246

242247
[CATransaction setCompletionBlock:^{
248+
if (cameraBounds != nil){
249+
250+
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:cameraBounds.center.latitude
251+
longitude:cameraBounds.center.longitude
252+
zoom:self.mapCtrl.map.camera.zoom
253+
bearing:[[json objectForKey:@"bearing"] doubleValue]
254+
viewingAngle:[[json objectForKey:@"tilt"] doubleValue]];
255+
256+
[self.mapCtrl.map setCamera:cameraPosition2];
257+
}
243258
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
244259
}];
245260

@@ -249,6 +264,18 @@ -(void)updateCameraPosition: (NSString*)action command:(CDVInvokedUrlCommand *)c
249264

250265
if ([action isEqual: @"moveCamera"]) {
251266
[self.mapCtrl.map setCamera:cameraPosition];
267+
268+
if (cameraBounds != nil){
269+
270+
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:cameraBounds.center.latitude
271+
longitude:cameraBounds.center.longitude
272+
zoom:self.mapCtrl.map.camera.zoom
273+
bearing:[[json objectForKey:@"bearing"] doubleValue]
274+
viewingAngle:[[json objectForKey:@"tilt"] doubleValue]];
275+
276+
[self.mapCtrl.map setCamera:cameraPosition2];
277+
}
278+
252279
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
253280
}
254281

@@ -348,7 +375,7 @@ - (void)getVisibleRegion:(CDVInvokedUrlCommand*)command {
348375

349376
- (void)setOptions:(CDVInvokedUrlCommand *)command {
350377
NSDictionary *initOptions = [command.arguments objectAtIndex:1];
351-
378+
/*
352379
if ([initOptions valueForKey:@"camera"]) {
353380
// camera position
354381
NSDictionary *cameraOpts = [initOptions objectForKey:@"camera"];
@@ -370,7 +397,85 @@ - (void)setOptions:(CDVInvokedUrlCommand *)command {
370397
371398
self.mapCtrl.map.camera = camera;
372399
}
373-
400+
*/
401+
402+
if ([initOptions valueForKey:@"camera"]) {
403+
NSDictionary *cameraOpts = [initOptions objectForKey:@"camera"];
404+
NSMutableDictionary *latLng = [NSMutableDictionary dictionary];
405+
[latLng setObject:[NSNumber numberWithFloat:0.0f] forKey:@"lat"];
406+
[latLng setObject:[NSNumber numberWithFloat:0.0f] forKey:@"lng"];
407+
float latitude;
408+
float longitude;
409+
GMSCameraPosition *camera;
410+
GMSCoordinateBounds *cameraBounds = nil;
411+
412+
if ([cameraOpts objectForKey:@"target"]) {
413+
NSString *targetClsName = [[cameraOpts objectForKey:@"target"] className];
414+
if ([targetClsName isEqualToString:@"__NSCFArray"] || [targetClsName isEqualToString:@"__NSArrayM"] ) {
415+
int i = 0;
416+
NSArray *latLngList = [cameraOpts objectForKey:@"target"];
417+
GMSMutablePath *path = [GMSMutablePath path];
418+
for (i = 0; i < [latLngList count]; i++) {
419+
latLng = [latLngList objectAtIndex:i];
420+
latitude = [[latLng valueForKey:@"lat"] floatValue];
421+
longitude = [[latLng valueForKey:@"lng"] floatValue];
422+
[path addLatitude:latitude longitude:longitude];
423+
}
424+
float scale = 1;
425+
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
426+
scale = [[UIScreen mainScreen] scale];
427+
}
428+
[[UIScreen mainScreen] scale];
429+
430+
cameraBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
431+
432+
CLLocationCoordinate2D center = cameraBounds.center;
433+
434+
camera = [GMSCameraPosition cameraWithLatitude:center.latitude
435+
longitude:center.longitude
436+
zoom:[[cameraOpts valueForKey:@"zoom"] floatValue]
437+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
438+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
439+
440+
} else {
441+
latLng = [cameraOpts objectForKey:@"target"];
442+
latitude = [[latLng valueForKey:@"lat"] floatValue];
443+
longitude = [[latLng valueForKey:@"lng"] floatValue];
444+
445+
camera = [GMSCameraPosition cameraWithLatitude:latitude
446+
longitude:longitude
447+
zoom:[[cameraOpts valueForKey:@"zoom"] floatValue]
448+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
449+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
450+
}
451+
} else {
452+
camera = [GMSCameraPosition
453+
cameraWithLatitude: [[latLng valueForKey:@"lat"] floatValue]
454+
longitude: [[latLng valueForKey:@"lng"] floatValue]
455+
zoom: [[cameraOpts valueForKey:@"zoom"] floatValue]
456+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
457+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
458+
}
459+
self.mapCtrl.map.camera = camera;
460+
461+
if (cameraBounds != nil){
462+
float scale = 1;
463+
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
464+
scale = [[UIScreen mainScreen] scale];
465+
}
466+
[[UIScreen mainScreen] scale];
467+
468+
[self.mapCtrl.map moveCamera:[GMSCameraUpdate fitBounds:cameraBounds withPadding:10 * scale]];
469+
GMSCameraPosition *cameraPosition2 = [GMSCameraPosition cameraWithLatitude:cameraBounds.center.latitude
470+
longitude:cameraBounds.center.longitude
471+
zoom:self.mapCtrl.map.camera.zoom
472+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
473+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
474+
475+
[self.mapCtrl.map setCamera:cameraPosition2];
476+
}
477+
478+
}
374479

375480
BOOL isEnabled = NO;
376481
//controls

0 commit comments

Comments
 (0)