|
| 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 |
0 commit comments