MapView叠加绘图

时间:2014-05-23 02:44:12

标签: ios xcode mapkit

我一直面临着mkoverlay颜色的问题。当我打开mapview时,有时不是绘制行走路径,而是使用自行车活动进行着色。我不知道如何解决这个问题。即使我没有做过任何自行车活动,但它还是用蓝色绘制自行车活动。 enter image description here

这是代码实现。

- (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;
}

2 个答案:

答案 0 :(得分:0)

viewForOverlay委托方法中,使用外部变量color设置叠加颜色,该变量在为每种类型的叠加层调用parseArray之前设置。

但是,无法保证地图视图何时调用委托方法,并且可以在之后为同一个覆盖多次调用委托方法叠加(例如,如果您缩放/平移地图并且叠加层返回到​​视图中)。

由于您设置的最后一个color值为3(对于“骑自行车”),因此在添加了叠加层之后地图视图对委托方法所做的任何调用都将最终使用自行车颜色绘制叠加层


要解决此问题,您需要能够使用overlay参数的某些属性(并且不依赖于某些外部变量)来确定在委托方法本身内部绘制overlay的颜色。

最简单的方法是使用MKPolyline的{​​{1}}属性 请参阅this answerthis answer,说明title具有MKPolyline属性的事实。

所以你在你的情况下可以做的是:

  1. title参数传递给color方法。
  2. parseArray方法中,创建parseArray后,将其polyline设置为颜色:

    title
  3. polyline.title = [NSString stringWithFormat:@"%d", color]; 中,检查叠加层的viewForOverlay属性并相应地设置颜色。请参阅different coloured polygon overlays中的具体示例(它显示多边形但可以使用折线完成相同的操作)。

答案 1 :(得分:0)

这是基于Anna回答的答案,以帮助任何想要在代码中看到的人。

 - (void)showLines2 {


        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 withColor:color];

            // 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 withColor:color];

            // 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 withColor:color];

    }


- (void) parseArray:(NSArray *) coordinate_array withArrayCount:(int)arrayCount withColor:(int)polyColor
{
    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];
                polyline.title = [NSString stringWithFormat:@"%d", color];
                [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;

    //      ActivityType currentActivityType = [DataManager sharedInstance].activityType;
            if ([overlay.title isEqualToString:@"1"]) {
                // walking
                lineView.strokeColor = [UIColor greenColor];
                lineView.fillColor = [UIColor greenColor];
            }
            else if([overlay.title isEqualToString:@"2"]) {
                // driving
                lineView.strokeColor = [UIColor redColor];
                lineView.fillColor = [UIColor redColor];
            }
            else if([overlay.title isEqualToString:@"3"]) {
                // biking
                lineView.strokeColor = [UIColor blueColor];
                lineView.fillColor = [UIColor blueColor];
            }
            else {
                lineView.strokeColor = [UIColor blackColor];
                lineView.fillColor = [UIColor blackColor];
            }

            return lineView;
        }
        return nil;
    }