在iphone中的地图上绘制折线
朋友
我正在尝试在地图上绘制折线。为此,我使用这段代码。效果很好。在这里我使用 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;
}
Friend
I am trying to Drawing the polyline on the map. For that I use this code. It work fine. Over here I use Fake_location(CSV file) but if I to want use the database location... My database table is like this:
PK LocationID VARCHAR(20)
_id INTEGER
FK1 JourneyID VARCHAR(20)
Altitude FLOAT
Longitude FLOAT
Latitude FLOAT
Speed FLOAT
Accuracy FLOAT
LocationDate DATETIME
LastSyncDate DATETIME
How can I use Longitude & Latitude ?
-(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论