在iphone中绘制地图上的折线

时间:2011-05-25 12:03:51

标签: iphone objective-c

朋友
我正在尝试在地图上绘制折线。为此,我使用此代码。它工作正常。在这里我使用Fake_location(CSV文件),但如果我想使用数据库位置...我的数据库表是这样的:

PK LocationID VARCHAR(20)
_id INTEGER
FK1 JourneyID VARCHAR(20)
Altitude FLOAT
Longitude FLOAT
Latitude FLOAT
Speed FLOAT
Accuracy FLOAT
LocationDate DATETIME
LastSyncDate DATETIME 

我如何使用经度&纬度?

-(id)init {
    self = [super init];
    NSString* filePath = [[NSBundle mainBundle] pathForResource:ROUTE_FILE_NAME ofType:@"csv"];
    NSString *fake_location = [[NSString alloc] initWithContentsOfFile:filePath];
    pointsArray = [[fake_location componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];
    pointsArrayIndex = 0;
    oldLocationsIndex = 0;

    [fake_location release];

    oldLocations = [[NSMutableArray alloc] init];
    return self;
}

-(void)startUpdatingLocation {
aTimer = [NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self selector:@selector(injectNextLocation) userInfo:nil repeats:YES];
}

-(void)stopUpdatingLocation {
[aTimer invalidate];
}

-(void)injectNextLocation{

if (pointsArray.count == pointsArrayIndex) {
    [self stopUpdatingLocation];
    return;
}

CLLocation *oldLocation = nil;

if ([oldLocations count] > 0) {
    oldLocation = [oldLocations objectAtIndex:oldLocationsIndex];
    oldLocationsIndex++;
}

NSString *pointString = [pointsArray objectAtIndex:pointsArrayIndex];
NSArray *latLong = [pointString componentsSeparatedByString:@","];
//NSLog(@"%@",latLong);

CLLocationCoordinate2D coords;
coords.latitude = [[latLong objectAtIndex:0] doubleValue];
coords.longitude = [[latLong objectAtIndex:1] doubleValue];

float altitude = [self getRandomValue:200 toMax:220];
CLLocation *myLocation = [[CLLocation alloc] initWithCoordinate:coords altitude:altitude horizontalAccuracy:1 verticalAccuracy:1 timestamp:[NSDate date]];

[self.delegate locationManager:self didUpdateToLocation:myLocation fromLocation:oldLocation];

[oldLocations addObject:myLocation];
[myLocation release];

pointsArrayIndex++;
}


-(void)dealloc {
[pointsArray release];
[aTimer release];
[oldLocations release];
[super dealloc];
}

#pragma mark -
#pragma mark functions

- (float) getRandomValue:(float)min toMax:(float)max {
NSNumber *rand;
rand = [NSNumber numberWithUnsignedInt:arc4random()];
return fmod([rand floatValue],(max-min+1))+min;
}

0 个答案:

没有答案