在iphone中的地图上绘制折线

发布于 2024-11-09 08:10:31 字数 2385 浏览 0 评论 0原文

朋友
我正在尝试在地图上绘制折线。为此,我使用这段代码。效果很好。在这里我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文