forked from openvehicles/Open-Vehicle-Monitoring-System
/
ovmsLocationViewController.m
180 lines (148 loc) · 4.87 KB
/
ovmsLocationViewController.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// ovmsLocationViewController.m
// ovms
//
// Created by Mark Webb-Johnson on 16/11/11.
// Copyright (c) 2011 Hong Hay Villa. All rights reserved.
//
#import "ovmsLocationViewController.h"
@implementation TeslaAnnotation
@synthesize name = _name;
@synthesize description = _description;
@synthesize coordinate = _coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{
self=[super init];
if(self){
_coordinate=coordinate;
}
return self;
}
-(void) dealloc{
self.name = nil;
self.description = nil;
}
@end
@implementation ovmsLocationViewController
@synthesize myMapView;
@synthesize m_car_location;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(void) updateLocation
{
CLLocationCoordinate2D location = [ovmsAppDelegate myRef].car_location;
MKCoordinateRegion region = myMapView.region;
if ( (region.center.latitude != location.latitude)&&
(region.center.longitude != location.longitude) )
{
// Remove all existing annotations
for (int k=0; k < [myMapView.annotations count]; k++)
{
if ([[myMapView.annotations objectAtIndex:k] isKindOfClass:[TeslaAnnotation class]])
{
[myMapView removeAnnotation:[myMapView.annotations objectAtIndex:k]];
}
}
TeslaAnnotation *pa = [[TeslaAnnotation alloc] initWithCoordinate:location];
pa.name = @"EV915";
pa.description = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[myMapView addAnnotation:pa];
self.m_car_location = pa;
region.center=location;
[myMapView setRegion:region animated:YES];
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[NSThread detachNewThreadSelector:@selector(displayMYMap) toTarget:self withObject:nil];
[ovmsAppDelegate myRef].location_delegate = self;
self.navigationItem.title = [ovmsAppDelegate myRef].sel_label;
}
- (void)viewDidUnload
{
[self setMyMapView:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.title = [ovmsAppDelegate myRef].sel_label;
[self displayMYMap];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
-(void)displayMYMap
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
CLLocationCoordinate2D location = [ovmsAppDelegate myRef].car_location;
region.span=span;
region.center=location;
// Remove all existing annotations
for (int k=0; k < [myMapView.annotations count]; k++)
{
if ([[myMapView.annotations objectAtIndex:k] isKindOfClass:[TeslaAnnotation class]])
{
[myMapView removeAnnotation:[myMapView.annotations objectAtIndex:k]];
}
}
if ((location.latitude != 0)||(location.longitude != 0))
{
// Add in the new annotation for current car location
TeslaAnnotation *pa = [[TeslaAnnotation alloc] initWithCoordinate:location];
pa.name = @"EV915";
pa.description = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[myMapView addAnnotation:pa];
self.m_car_location = pa;
}
[myMapView setRegion:region animated:YES];
[myMapView regionThatFits:region];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *teslaAnnotationIdentifier=@"TeslaAnnotationIdentifier";
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if([annotation isKindOfClass:[TeslaAnnotation class]]){
//Try to get an unused annotation, similar to uitableviewcells
// MKAnnotationView *annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:teslaAnnotationIdentifier];
//If one isn't available, create a new one
// if(!annotationView){
MKAnnotationView *annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:teslaAnnotationIdentifier];
//Here's where the magic happens
annotationView.image=[UIImage imageNamed:[ovmsAppDelegate myRef].sel_imagepath];
annotationView.contentMode = UIViewContentModeScaleAspectFill;
annotationView.bounds = CGRectMake(0, 0, 32, 32);
// }
return annotationView;
}
return nil;
}
@end