forked from nippysaurus/WeatherRock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.m
133 lines (104 loc) · 4.35 KB
/
Location.m
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
//
// Location.m
// BrissyBom
//
// Created by Michael Dawson on 15/02/11.
// Copyright 2011 Nippysaurus. All rights reserved.
//
#import "Location.h"
// --- private interface ---------------------------------------------------------------------------
@interface Location ()
-(void)alertLocationDetected:(NSString*)location;
@end
// --- class implementation ------------------------------------------------------------------------
@implementation Location
-(id)init
{
if (self = [super init])
{
radarRadius = 128 * 1000;
// init locations
Adelaide = [[CLLocation alloc] initWithLatitude:-34.929 longitude:138.601]; // 1
Brisbane = [[CLLocation alloc] initWithLatitude:-27.467778 longitude:153.027778];
Canberra = [[CLLocation alloc] initWithLatitude:-35.308056 longitude:149.124444]; // 1
Darwin = [[CLLocation alloc] initWithLatitude:-12.45 longitude:130.833333]; // 1
Hobart = [[CLLocation alloc] initWithLatitude:-42.880556 longitude:147.325]; // 1
Melbourne = [[CLLocation alloc] initWithLatitude:-37.813611 longitude:144.963056]; // 1
Perth = [[CLLocation alloc] initWithLatitude:-31.952222 longitude:115.858889]; // 1
Sydney = [[CLLocation alloc] initWithLatitude:-33.859972 longitude:151.211111]; // 1
// init location manager
locationManager = [[CLLocationManager alloc] init]; // 1
locationManager.delegate = self;
// check for existing location (in user preferences)
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"CurrentLocation"] /* 1 */ != nil)
{
NSString* location = [[NSUserDefaults standardUserDefaults] valueForKey:@"CurrentLocation"]; // auto
[self alertLocationDetected:location];
return self;
}
if ([locationManager locationServicesEnabled] == YES)
{
NSLog(@"location services enabled");
[locationManager startUpdatingLocation];
}
else
{
NSLog(@"location services disabled");
}
}
return self;
}
-(void)alertLocationDetected:(NSString*)location
{
NSLog(@"detected location: %@", location);
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:location, @"location", nil]; // auto
[[NSNotificationCenter defaultCenter] postNotificationName:@"LocationAvaiable"
object:nil
userInfo:userInfo];
}
-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
// no need to wait for further location updates
[locationManager stopUpdatingLocation];
NSLog(@"CoreLocation found location: %@", newLocation);
CLLocationDistance fromAdelaide = [newLocation distanceFromLocation:Adelaide];
CLLocationDistance fromBrisbane = [newLocation distanceFromLocation:Brisbane];
CLLocationDistance fromCanberra = [newLocation distanceFromLocation:Canberra];
CLLocationDistance fromDarwin = [newLocation distanceFromLocation:Darwin];
CLLocationDistance fromHobart = [newLocation distanceFromLocation:Hobart];
CLLocationDistance fromMelbourne = [newLocation distanceFromLocation:Melbourne];
CLLocationDistance fromPerth = [newLocation distanceFromLocation:Perth];
CLLocationDistance fromSydney = [newLocation distanceFromLocation:Sydney];
NSLog(@"distance from Adelaide : %f km", fromAdelaide / 1000);
NSLog(@"distance from Brisbane : %f km", fromBrisbane / 1000);
NSLog(@"distance from Canberra : %f km", fromCanberra / 1000);
NSLog(@"distance from Darwin : %f km", fromDarwin / 1000);
NSLog(@"distance from Hobart : %f km", fromHobart / 1000);
NSLog(@"distance from Melbourne : %f km", fromMelbourne / 1000);
NSLog(@"distance from Perth : %f km", fromPerth / 1000);
NSLog(@"distance from Sydney : %f km", fromSydney / 1000);
if (fromAdelaide < radarRadius)
[self alertLocationDetected:@"Adelaide"];
if (fromBrisbane < radarRadius)
[self alertLocationDetected:@"Brisbane"];
if (fromCanberra < radarRadius)
[self alertLocationDetected:@"Canberra"];
if (fromDarwin < radarRadius)
[self alertLocationDetected:@"Darwin"];
if (fromHobart < radarRadius)
[self alertLocationDetected:@"Hobart"];
if (fromMelbourne < radarRadius)
[self alertLocationDetected:@"Melbourne"];
if (fromPerth < radarRadius)
[self alertLocationDetected:@"Perth"];
if (fromSydney < radarRadius)
[self alertLocationDetected:@"Sydney"];
}
-(void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error
{
NSLog(@"CoreLocation encountered an error: %@", error);
}
@end