#import "mapViewController.h"
@interface mapViewController (){
NSString *latitudeStr,*longitudeStr;
NSArray *latitudeArray,*longitudeArray;
}
@property (strong, nonatomic) NSMutableArray * points;
@property (assign, nonatomic) BOOL isTracking;
@property (assign, nonatomic) NSInteger currIdx;
@end
@implementation mapViewController
@synthesize isTracking = _isTracking;
@synthesize currIdx = _currIdx;
@synthesize points = _points;
- (NSMutableArray *)points
{
if (_points == nil)
_points = [NSMutableArray arrayWithCapacity:10];
return _points;
}
static const double meters_per_hour = 50.0;
static const int frames_per_second = 32;
static const double time_per_frame = 1.0/frames_per_second;
- (void)startTracking
{
// Points array is being updated, so we make a copy of it first to get a snapshot
// of the points at this instant.
l=0;
NSArray * pts = [self.points copy];
// NSArray *pts=[[NSArray alloc]init];
// pts=self.points;
// We have a single point, no animation.
if ([pts count] == 1)
{
NSDictionary * pt = [pts objectAtIndex:0];
CLLocationCoordinate2D cpoint = CLLocationCoordinate2DMake([[pt objectForKey:@"lat"] floatValue],
[[pt objectForKey:@"lng"] floatValue]);
[self performSelectorOnMainThread:@selector(updateMyLocation:) withObject:[NSValue valueWithPointer:&cpoint] waitUntilDone:NO];
self.currIdx += 1;
}
// More than one points ... lets interpolate.
else if ([pts count] > 1)
{
NSInteger i = self.currIdx;
// Grab the two points to interpolate between
NSDictionary * pt1 = [pts objectAtIndex:i-1];
NSDictionary * pt2 = [pts objectAtIndex:i];
CLLocationCoordinate2D c1 = CLLocationCoordinate2DMake([[pt1 objectForKey:@"latt"] floatValue], [[pt1 objectForKey:@"long"] floatValue]);
CLLocationCoordinate2D c2 = CLLocationCoordinate2DMake([[pt2 objectForKey:@"latt"] floatValue], [[pt2 objectForKey:@"long"] floatValue]);
// Interpolation interval computation.
double dist = MKMetersBetweenMapPoints(MKMapPointForCoordinate(c1), MKMapPointForCoordinate(c2));
NSTimeInterval totalTime = dist / meters_per_hour;
int num_frames = MAX(totalTime * frames_per_second, 2); // At least get two frames
totalTime = num_frames / (frames_per_second*1.0);
// Create and fill the interpolated points buffer, i.e., a point for each frame.
// NOTE: Very bad idea to malloc here. A large enough buffer should be pre-created
// and used throughout the life of the app. I'm being lazy and malloc/free'ing this here.
CLLocationCoordinate2D * cpoints = malloc(sizeof(CLLocationCoordinate2D) * num_frames);
for (int j=0; j<num_frames; j++)
{
double t = ((j * time_per_frame) / totalTime);
cpoints[j] = CLLocationCoordinate2DMake(c1.latitude + (c2.latitude - c1.latitude) * t, c1.longitude + (c2.longitude - c1.longitude) * t);
}
// Draw the frames in sequence with the appropriate sleep timein between.
// NOTE: I'm assuming the drawing takes no time, so I'm sleeping for the entire duration
// of the frame.
for (int j=0; j<num_frames; j++)
{
[self performSelectorOnMainThread:@selector(updateMyLocation:) withObject:[NSValue valueWithPointer:&cpoints[j]] waitUntilDone:NO];
[NSThread sleepForTimeInterval:time_per_frame];
}
// Dont drawing. Increment the current index.
free(cpoints);
self.currIdx += 1;
}
// Keep checking for new points ...
while ([self.points count] <= self.currIdx)
[NSThread sleepForTimeInterval:1];
// got a new point! Interpolate.
[self performSelectorInBackground:@selector(startTracking) withObject:nil];
}
- (void)updateMyLocation:(NSValue *)val
{
CLLocationCoordinate2D coord = *(CLLocationCoordinate2D *)[val pointerValue];
if (CLLocationCoordinate2DIsValid(coord))
self.playbackMapView.centerCoordinate = coord;
}
- (void)startGettingPoints
{
NSMutableArray *he=[[NSMutableArray alloc]init];
for (int i=0; i<[latitudeArray count]; i++) {
NSNumber *latStr=[latitudeArray objectAtIndex:i];
[he addObject:latStr];
NSNumber *longStr=[longitudeArray objectAtIndex:i];
[self.points addObject:@{@"latt": [NSNumber numberWithFloat:(latStr.floatValue-0.001058)],@"long": [NSNumber numberWithFloat:(longStr.floatValue-0.000008)]}];
if (!self.isTracking)
{
self.isTracking = TRUE;
[self performSelectorInBackground:@selector(startTracking) withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
ad=[[UIApplication sharedApplication]delegate];
latitudeArray =[[NSArray alloc]init];
longitudeArray=[[NSArray alloc]init];
latitudeArray=[NSMutableArray arrayWithObjects:@"17.495000",@"17.495097",@"17.495325", nil];
longitudeArray=[NSMutableArray arrayWithObjects:@"78.325776",@"78.331696",@"78.338398", nil];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.span.latitudeDelta = 0.006;
region.span.longitudeDelta = 0.006;
[self.playbackMapView setRegion:region animated:YES];
// Mechanic image overlay ... always stays in the center of the view.
UILabel * lab = [[UILabel alloc] init];
lab.font = [UIFont systemFontOfSize:12];
lab.textAlignment = NSTextAlignmentCenter;
lab.text = @"M";
lab.textColor = [UIColor whiteColor];
lab.backgroundColor = [UIColor blackColor];
lab.frame = CGRectMake(self.playbackMapView.frame.size.width/2 - 10, self.playbackMapView.frame.size.height/2 - 10 - 30, 20, 20);
[self.view addSubview:lab];
[self performSelectorInBackground:@selector(startGettingPoints) withObject:nil];
}
@end
@interface mapViewController (){
NSString *latitudeStr,*longitudeStr;
NSArray *latitudeArray,*longitudeArray;
}
@property (strong, nonatomic) NSMutableArray * points;
@property (assign, nonatomic) BOOL isTracking;
@property (assign, nonatomic) NSInteger currIdx;
@end
@implementation mapViewController
@synthesize isTracking = _isTracking;
@synthesize currIdx = _currIdx;
@synthesize points = _points;
- (NSMutableArray *)points
{
if (_points == nil)
_points = [NSMutableArray arrayWithCapacity:10];
return _points;
}
static const double meters_per_hour = 50.0;
static const int frames_per_second = 32;
static const double time_per_frame = 1.0/frames_per_second;
- (void)startTracking
{
// Points array is being updated, so we make a copy of it first to get a snapshot
// of the points at this instant.
l=0;
NSArray * pts = [self.points copy];
// NSArray *pts=[[NSArray alloc]init];
// pts=self.points;
// We have a single point, no animation.
if ([pts count] == 1)
{
NSDictionary * pt = [pts objectAtIndex:0];
CLLocationCoordinate2D cpoint = CLLocationCoordinate2DMake([[pt objectForKey:@"lat"] floatValue],
[[pt objectForKey:@"lng"] floatValue]);
[self performSelectorOnMainThread:@selector(updateMyLocation:) withObject:[NSValue valueWithPointer:&cpoint] waitUntilDone:NO];
self.currIdx += 1;
}
// More than one points ... lets interpolate.
else if ([pts count] > 1)
{
NSInteger i = self.currIdx;
// Grab the two points to interpolate between
NSDictionary * pt1 = [pts objectAtIndex:i-1];
NSDictionary * pt2 = [pts objectAtIndex:i];
CLLocationCoordinate2D c1 = CLLocationCoordinate2DMake([[pt1 objectForKey:@"latt"] floatValue], [[pt1 objectForKey:@"long"] floatValue]);
CLLocationCoordinate2D c2 = CLLocationCoordinate2DMake([[pt2 objectForKey:@"latt"] floatValue], [[pt2 objectForKey:@"long"] floatValue]);
// Interpolation interval computation.
double dist = MKMetersBetweenMapPoints(MKMapPointForCoordinate(c1), MKMapPointForCoordinate(c2));
NSTimeInterval totalTime = dist / meters_per_hour;
int num_frames = MAX(totalTime * frames_per_second, 2); // At least get two frames
totalTime = num_frames / (frames_per_second*1.0);
// Create and fill the interpolated points buffer, i.e., a point for each frame.
// NOTE: Very bad idea to malloc here. A large enough buffer should be pre-created
// and used throughout the life of the app. I'm being lazy and malloc/free'ing this here.
CLLocationCoordinate2D * cpoints = malloc(sizeof(CLLocationCoordinate2D) * num_frames);
for (int j=0; j<num_frames; j++)
{
double t = ((j * time_per_frame) / totalTime);
cpoints[j] = CLLocationCoordinate2DMake(c1.latitude + (c2.latitude - c1.latitude) * t, c1.longitude + (c2.longitude - c1.longitude) * t);
}
// Draw the frames in sequence with the appropriate sleep timein between.
// NOTE: I'm assuming the drawing takes no time, so I'm sleeping for the entire duration
// of the frame.
for (int j=0; j<num_frames; j++)
{
[self performSelectorOnMainThread:@selector(updateMyLocation:) withObject:[NSValue valueWithPointer:&cpoints[j]] waitUntilDone:NO];
[NSThread sleepForTimeInterval:time_per_frame];
}
// Dont drawing. Increment the current index.
free(cpoints);
self.currIdx += 1;
}
// Keep checking for new points ...
while ([self.points count] <= self.currIdx)
[NSThread sleepForTimeInterval:1];
// got a new point! Interpolate.
[self performSelectorInBackground:@selector(startTracking) withObject:nil];
}
- (void)updateMyLocation:(NSValue *)val
{
CLLocationCoordinate2D coord = *(CLLocationCoordinate2D *)[val pointerValue];
if (CLLocationCoordinate2DIsValid(coord))
self.playbackMapView.centerCoordinate = coord;
}
- (void)startGettingPoints
{
NSMutableArray *he=[[NSMutableArray alloc]init];
for (int i=0; i<[latitudeArray count]; i++) {
NSNumber *latStr=[latitudeArray objectAtIndex:i];
[he addObject:latStr];
NSNumber *longStr=[longitudeArray objectAtIndex:i];
[self.points addObject:@{@"latt": [NSNumber numberWithFloat:(latStr.floatValue-0.001058)],@"long": [NSNumber numberWithFloat:(longStr.floatValue-0.000008)]}];
if (!self.isTracking)
{
self.isTracking = TRUE;
[self performSelectorInBackground:@selector(startTracking) withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
ad=[[UIApplication sharedApplication]delegate];
latitudeArray =[[NSArray alloc]init];
longitudeArray=[[NSArray alloc]init];
latitudeArray=[NSMutableArray arrayWithObjects:@"17.495000",@"17.495097",@"17.495325", nil];
longitudeArray=[NSMutableArray arrayWithObjects:@"78.325776",@"78.331696",@"78.338398", nil];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.span.latitudeDelta = 0.006;
region.span.longitudeDelta = 0.006;
[self.playbackMapView setRegion:region animated:YES];
// Mechanic image overlay ... always stays in the center of the view.
UILabel * lab = [[UILabel alloc] init];
lab.font = [UIFont systemFontOfSize:12];
lab.textAlignment = NSTextAlignmentCenter;
lab.text = @"M";
lab.textColor = [UIColor whiteColor];
lab.backgroundColor = [UIColor blackColor];
lab.frame = CGRectMake(self.playbackMapView.frame.size.width/2 - 10, self.playbackMapView.frame.size.height/2 - 10 - 30, 20, 20);
[self.view addSubview:lab];
[self performSelectorInBackground:@selector(startGettingPoints) withObject:nil];
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon