summaryrefslogtreecommitdiff
path: root/navit/vehicle/iphone/corelocation.m
blob: 5f02500f34c778181465bc71484d3764f7a1493f (plain)
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
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#define VEHICLE_IPHONE_OBJC
#import "corelocation.h"

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/* global location structure */
corelocation *locationcontroller = NULL;

/** C update procedure */
void corelocation_update(double lat, double lng, double dir, double spd, char * str_time, double radius) {
	FT_LOCATION_CB pf_cb = locationcontroller->pf_cb;
	void * pv_arg = locationcontroller->pv_arg;
	if(pf_cb) pf_cb(pv_arg, lat, lng, dir, spd, str_time, radius);
}

/** C init procedure */
void corelocation_init(void * pv_arg, FT_LOCATION_CB pf_cb) {
	locationcontroller = [[corelocation alloc] init];

	/** Save callbacks */
	locationcontroller->pv_arg = pv_arg;
	locationcontroller->pf_cb = pf_cb;

	/** Start location process */
	[locationcontroller.locationManager startUpdatingLocation];
}

/** C exit procedure */
void corelocation_exit(void) {
	[locationcontroller dealloc];
}

/** Core location implementation */
@implementation corelocation

@synthesize locationManager;
@synthesize dateFormatter;
@synthesize eventDate;
@synthesize pv_arg;
@synthesize pf_cb;

/** Init corelocation */
- (id) init {
	self = [super init];
	if (self != nil) {
		self.locationManager = [[[CLLocationManager alloc] init] autorelease];
		self.locationManager.distanceFilter = kCLDistanceFilterNone;
		self.locationManager.delegate = self; // send loc updates to myself
		self.pf_cb = NULL;
		self.pv_arg = NULL;
		self.eventDate = [NSDate date];
		self.dateFormatter = [[NSDateFormatter alloc] init];
		[self.dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
	}
	return self;
}

/** New Data EVENT */
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
	NSLog(@"New Location: %@", [newLocation description]);
	NSString *newDateString = [self.dateFormatter stringFromDate:newLocation.timestamp];
	const char* cString = [newDateString cStringUsingEncoding:NSASCIIStringEncoding];

	if(self.pf_cb) {
		self.pf_cb(
				self.pv_arg,
				(double)newLocation.coordinate.latitude,
				(double) newLocation.coordinate.longitude,
				(double) newLocation.course,
				(double) newLocation.speed,
				cString,
				(double) newLocation.horizontalAccuracy
			  );
	}
}

/** Error EVENT */
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
	NSLog(@"Error: %@", [error description]);
}

/** Destructor */
- (void)dealloc {
	[self.locationManager release];
	[super dealloc];
}

@end