Skip to content

Commit b04e47e

Browse files
committed
issue #647 Add target property, deprecate latLng for mapOptions. (iOS)
The `target` property accepts Array<LatLng>
1 parent 541eb72 commit b04e47e

8 files changed

Lines changed: 192 additions & 7 deletions

File tree

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@
174174
<header-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.h" />
175175
<header-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.podspec" />
176176
<source-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.m" />
177+
<header-file src="src/ios/GoogleMaps/MFGoogleMapAdditions/GMSCoordinateBounds+Geometry.h" />
178+
<source-file src="src/ios/GoogleMaps/MFGoogleMapAdditions/GMSCoordinateBounds+Geometry.m" />
177179
<header-file src="src/ios/GoogleMaps/MyPluginLayer.h" />
178180
<source-file src="src/ios/GoogleMaps/MyPluginLayer.m" />
179181
<header-file src="src/ios/GoogleMaps/MyReachability.h" />

src/ios/GoogleMaps/GoogleMapsViewController.m

Lines changed: 19 additions & 6 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-
GMSCameraPosition *initCamera;
85+
GMSCoordinateBounds *initBounds = nil;
8686

8787
if ([cameraOpts objectForKey:@"target"]) {
8888
NSString *targetClsName = [[cameraOpts objectForKey:@"target"] className];
@@ -92,7 +92,6 @@ - (void)viewDidLoad
9292
GMSMutablePath *path = [GMSMutablePath path];
9393
for (i = 0; i < [latLngList count]; i++) {
9494
latLng = [latLngList objectAtIndex:i];
95-
NSLog(@"---->latLng=%@", latLng);
9695
latitude = [[latLng valueForKey:@"lat"] floatValue];
9796
longitude = [[latLng valueForKey:@"lng"] floatValue];
9897
[path addLatitude:latitude longitude:longitude];
@@ -103,12 +102,15 @@ - (void)viewDidLoad
103102
}
104103
[[UIScreen mainScreen] scale];
105104

106-
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
105+
initBounds = [[GMSCoordinateBounds alloc] initWithPath:path];
107106

108-
NSLog(@"---->bounds=%f,%f - ,%f,%f", bounds.southWest.latitude,bounds.southWest.longitude, bounds.northEast.latitude, bounds.northEast.longitude);
109-
110-
initCamera = [self.map cameraForBounds:bounds insets:UIEdgeInsetsMake(10 * scale, 10* scale, 10* scale, 10* scale)];
107+
CLLocationCoordinate2D center = initBounds.center;
111108

109+
camera = [GMSCameraPosition cameraWithLatitude:center.latitude
110+
longitude:center.longitude
111+
zoom:[[cameraOpts valueForKey:@"zoom"] floatValue]
112+
bearing:[[cameraOpts objectForKey:@"bearing"] doubleValue]
113+
viewingAngle:[[cameraOpts objectForKey:@"tilt"] doubleValue]];
112114

113115
} else {
114116
latLng = [cameraOpts objectForKey:@"target"];
@@ -220,6 +222,17 @@ - (void)viewDidLoad
220222
}
221223

222224
[self.view addSubview: self.map];
225+
226+
dispatch_async(dispatch_get_main_queue(), ^{
227+
if (initBounds != nil) {
228+
float scale = 1;
229+
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
230+
scale = [[UIScreen mainScreen] scale];
231+
}
232+
[[UIScreen mainScreen] scale];
233+
[self.map moveCamera:[GMSCameraUpdate fitBounds:initBounds withPadding:10 * scale]];
234+
}
235+
});
223236
}
224237

225238

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// GMSCoordinateBounds+Geometry.h
3+
//
4+
// Created by Marius Feldmann on 8/12/14.
5+
// Copyright (c) 2014 Marius Feldmann. All rights reserved.
6+
//
7+
8+
#import <GoogleMaps/GoogleMaps.h>
9+
10+
@interface GMSCoordinateBounds (MFAdditions)
11+
12+
/** The North-West corner of these bounds. */
13+
- (CLLocationCoordinate2D)southEast;
14+
15+
/** The South-East corner of these bounds. */
16+
- (CLLocationCoordinate2D)northWest;
17+
18+
/** The center coordinate of these bounds. */
19+
- (CLLocationCoordinate2D)center;
20+
21+
/** Return the path of the rect. */
22+
- (GMSPath *)path;
23+
24+
/**
25+
* Returns an NSArray of GMSCoordinateBounds
26+
* Divides the current rectangular bounding box
27+
* into |numberOfRects| smaller boxes.
28+
*/
29+
- (NSArray *)divideIntoNumberOfBoxes:(NSInteger)numberOfBoxesl;
30+
31+
@end
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// GMSCoordinateBounds+Geometry.m
3+
//
4+
// Created by Marius Feldmann on 8/12/14.
5+
// Copyright (c) 2014 Marius Feldmann. All rights reserved.
6+
//
7+
8+
#import "GMSCoordinateBounds+Geometry.h"
9+
10+
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
11+
12+
@implementation GMSCoordinateBounds (MFAdditions)
13+
14+
- (CLLocationCoordinate2D)southEast {
15+
CLLocationDegrees southEastLat = self.southWest.latitude;
16+
CLLocationDegrees southEastLng = self.northEast.longitude;
17+
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(southEastLat, southEastLng);
18+
19+
return southEast;
20+
}
21+
22+
23+
- (CLLocationCoordinate2D)northWest {
24+
CLLocationDegrees northWestLat = self.northEast.latitude;
25+
CLLocationDegrees northWestLng = self.southWest.longitude;
26+
CLLocationCoordinate2D northWest = CLLocationCoordinate2DMake(northWestLat, northWestLng);
27+
28+
return northWest;
29+
}
30+
31+
32+
- (CLLocationCoordinate2D)center {
33+
double fraction = 0.5;
34+
CLLocationCoordinate2D center = GMSGeometryInterpolate(self.northEast, self.southWest, fraction);
35+
36+
return center;
37+
}
38+
39+
40+
- (GMSPath *)path {
41+
GMSMutablePath *path = [[GMSMutablePath alloc] init];
42+
[path addCoordinate:self.northWest];
43+
[path addCoordinate:self.northEast];
44+
[path addCoordinate:self.southEast];
45+
[path addCoordinate:self.southWest];
46+
47+
return path;
48+
}
49+
50+
51+
- (NSArray *)divideIntoNumberOfBoxes:(NSInteger)numberOfBoxes {
52+
NSMutableArray *rectArray = [[NSMutableArray alloc] init];
53+
54+
NSInteger columns = ceil(sqrt(numberOfBoxes));
55+
NSInteger fullRows = numberOfBoxes / columns;
56+
NSInteger orphans = numberOfBoxes % columns;
57+
58+
double width = GMSGeometryDistance(self.northWest, self.northEast);
59+
double boxWidth = width / columns;
60+
61+
double height = GMSGeometryDistance(self.northEast, self.southEast);
62+
double boxHeight = height / (orphans == 0 ? fullRows : (fullRows+1));
63+
64+
for (int y=0; y<fullRows; y++) {
65+
for (int x=0; x<columns; x++) {
66+
// Calculate the diagonal of the rect
67+
double diagonal = sqrt(pow(boxWidth, 2) + pow(boxHeight, 2));
68+
69+
// Get the angle of the diagonal
70+
double angle = 180 + RADIANS_TO_DEGREES(atan2(boxWidth, boxHeight));
71+
72+
// Get the northEast Coord by moving the northEast Coord to the right
73+
CLLocationCoordinate2D northEastCoord = GMSGeometryOffset(self.northWest, boxWidth*(x+1), 90);
74+
75+
// Make sure we are in the right row
76+
northEastCoord = GMSGeometryOffset(northEastCoord, boxHeight*y, 180);
77+
78+
// Get the southWest Coord by following the diagonal line from the northEast Coord
79+
CLLocationCoordinate2D southWestCoord = GMSGeometryOffset(northEastCoord, diagonal, angle);
80+
81+
// Create the new rect
82+
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWestCoord coordinate:northEastCoord];
83+
[rectArray addObject:bounds];
84+
}
85+
}
86+
87+
if (orphans > 0) {
88+
NSInteger orphanWidth = width / orphans;
89+
90+
for (int x=0; x<orphans; x++) {
91+
double diagonal = sqrt(pow(orphanWidth, 2) + pow(boxHeight, 2));
92+
double angle = 180 + RADIANS_TO_DEGREES(atan2(orphanWidth, boxHeight));
93+
94+
CLLocationCoordinate2D northEastCoord = GMSGeometryOffset(self.northWest, orphanWidth*(x+1), 90);
95+
northEastCoord = GMSGeometryOffset(northEastCoord, boxHeight*fullRows+1, 180);
96+
CLLocationCoordinate2D southWestCoord = GMSGeometryOffset(northEastCoord, diagonal, angle);
97+
98+
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWestCoord coordinate:northEastCoord];
99+
[rectArray addObject:bounds];
100+
}
101+
}
102+
103+
return rectArray;
104+
}
105+
106+
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) {{{year}}} {{{fullname}}}
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# MFGoogleMapAdditions
2+
3+
A couple of helpful categories for the Google Maps SDK.
4+
5+
6+
## Installation
7+
8+
Simply add the `GMSCoordinateBounds+Geometry.h` to your project
9+
10+
11+
Enjoy.

src/ios/GoogleMaps/PluginUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "MainViewController.h"
1515
#import <QuartzCore/QuartzCore.h>
1616
#import <objc/runtime.h>
17+
#import "MFGoogleMapAdditions/GMSCoordinateBounds+Geometry.h"
1718

1819
typedef void (^MYCompletionHandler)(NSError *error);
1920

src/ios/GoogleMaps/PluginUtil.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ - (void)webViewDidFinishLoad:(UIWebView*)theWebView
141141
}
142142
#endif
143143
@end
144-
145144
@implementation PluginUtil
146145
+ (BOOL)isIOS7_OR_OVER
147146
{
@@ -214,6 +213,7 @@ + (NSString *)getAbsolutePathFromCDVFilePath:(UIView*)webView cdvFilePath:(NSStr
214213

215214
return filePath;
216215
}
216+
217217
@end
218218

219219

0 commit comments

Comments
 (0)