I have been facing problem which is mkoverlay color. When I open the mapview, sometimes instead of drawing walking path, it colors with biking activity. I do not know how to fix the problem. Even though I have not done any biking activity but it draws biking activity with blue color.
Here is the code implementation.
- (void)showLines {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray* coordinate_array = [[NSArray alloc] init];
int arrayCount = 0;
// walking
NSData *data =[def objectForKey:@"walking_coordinate"];
NSMutableArray *walking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:walking_array];
arrayCount = (int)[walking_array count];
color = 1;
[self parseArray:coordinate_array withArrayCount:arrayCount];
// driving
data =[def objectForKey:@"driving_coordinate"];
NSMutableArray *driving_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:driving_array];
arrayCount = (int)[driving_array count];
color = 2;
[self parseArray:coordinate_array withArrayCount:arrayCount];
// biking
data =[def objectForKey:@"biking_coordinate"];
NSMutableArray *biking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:biking_array];
arrayCount = (int)[biking_array count];
color = 3;
[self parseArray:coordinate_array withArrayCount:arrayCount];
}
- (void) parseArray:(NSArray *) coordinate_array withArrayCount:(int)arrayCount
{
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < arrayCount; i++) {
CoordinateModel *coord = [coordinate_array objectAtIndex:i];
[tempArray addObject:coord];
if ((int)coord.latitude == -1 || (int)coord.longitude == -1 || i == arrayCount-1) {
// this is end of one segment
[tempArray removeLastObject];
CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * [tempArray count]);
for (int j = 0; j < [tempArray count]; j++) {
CoordinateModel *point = [tempArray objectAtIndex:j];
CLLocationCoordinate2D old_coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
pointsCoordinate[j] = old_coordinate;
// NSLog(@"(%f, %f)", old_coordinate.latitude, old_coordinate.longitude);
}
if ([tempArray count] > 0) {
int countTemp = (int)[tempArray count];
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:countTemp];
[mapView addOverlay:polyline];
[tempArray removeAllObjects];
}
}
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *lineView = [[MKPolylineView alloc] initWithPolyline:overlay];
lineView.lineWidth = 8;
if (color == 1) {
// walking
lineView.strokeColor = [UIColor greenColor];
lineView.fillColor = [UIColor greenColor];
}
else if(color == 2) {
// driving
lineView.strokeColor = [UIColor redColor];
lineView.fillColor = [UIColor redColor];
}
else if(color == 3) {
// biking
lineView.strokeColor = [UIColor blueColor];
lineView.fillColor = [UIColor blueColor];
}
else {
lineView.strokeColor = [UIColor blackColor];
lineView.fillColor = [UIColor blackColor];
}
return lineView;
}
return nil;
}