MapCoordinates.m
1.44 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
//
// MapCoordinates.m
// freemap-iphone
//
// Created by Michel Barakat on 8/31/08.
// Copyright 2008 Høgskolen i Østfold. All rights reserved.
//
#import "MapCoordinates.h"
@implementation MapCoordinates
@synthesize latitude;
@synthesize longitude;
- (id)initWithLatitude:(double)initLatitude Longitude:(double)initLongitude {
[self setLatitude:initLatitude];
[self setLongitude:initLongitude];
return self;
}
- (void)setLatitude:(double)newLatitude {
// This limit is equivalent to arctan(sinh(PI)). Using it on latitude edges
// causes the map to be a perfect square.
static double maxLatitude = 85.051129;
if (newLatitude > maxLatitude) {
NSLog(@"Latitude %f out of bounds, reduced to %f", newLatitude,
maxLatitude);
newLatitude = maxLatitude;
} else if (newLatitude < -maxLatitude) {
NSLog(@"Latitude %f out of bounds, reduced to %f", newLatitude,
-maxLatitude);
newLatitude = -maxLatitude;
}
latitude = newLatitude;
}
- (void)setLongitude:(double)newLongitude {
static const double maxLongitude = 180.0;
static const double fullCycle = 360.0;
// Equivalent to newLongitude % 360.0
newLongitude = newLongitude - ((int)(newLongitude / fullCycle) * fullCycle);
// Keep longitude in [-180.0 180.0] range.
if (newLongitude > maxLongitude) {
newLongitude = newLongitude - fullCycle;
} else if (newLongitude < -maxLongitude) {
newLongitude = newLongitude + fullCycle;
}
longitude = newLongitude;
}
@end