如何在iPhone地图视图中将图像添加为折线?

时间:2012-01-10 06:45:57

标签: ios cocoa-touch mapkit

我有折线图像,但我想在地图视图中添加此折线图像。我已经对此进行了研究,尽管努力了,但没有发现任何相关内容。

这是我的代码

 CGSize polyimagesize = CGSizeMake(768, 1024);

    UIGraphicsBeginImageContextWithOptions(polyimagesize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    //  CGContextSetAlpha(context, 0.6);   
    MKMapPoint *pointArr = malloc(sizeof(CLLocationCoordinate2D) * segments.count);

    for(NSDictionary *route in segments) {

        NSString *locations = [route valueForKey:@"Locations"];
        double speed = [[route valueForKey:@"Speed"] doubleValue];
        NSString *roadNumber = [route valueForKey:@"RoadNumber"];

        if ([roadNumber isEqualToString:@"A"] && aRoadFlag == NO) {
            continue;            
        }
        else if ([roadNumber isEqualToString:@"N"] && nRoadFlag == NO) {
            continue;
        }
        else if ([roadNumber isEqualToString:@"Others"] && othersRoadFlag == NO) {
            continue;
        }            

        if (locations && ([locations length]/16 > 1)) {       

            UIColor *color;
            if (speed <= 20) {
                color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
            }
            else if (speed <= 40) {
                color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0];
            }
            else if (speed <= 60) {
                color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0];
            }
            else if (speed <=80) {
                color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0];
            }
            else if (speed >80) {
                color = [UIColor colorWithRed:42/255.0 green:176/255.0 blue:39/255.0 alpha:1.0];
            }

            CGContextSetStrokeColorWithColor(context, color.CGColor);

            for (int i = 0; i <= locations.length - 32; i += 32) {

                CLLocationCoordinate2D coordinates;
                coordinates.latitude = hexDecode1([locations substringWithRange:NSMakeRange(i, 16)]);
                coordinates.longitude = hexDecode1([locations substringWithRange:NSMakeRange(i+16, 16)]);
                 MKMapPoint point1 = MKMapPointForCoordinate(coordinates);

                NSLog(@"coordinates.latitude=%g",coordinates.latitude);

                CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];

                if (i == 0)
                {
                    CGContextMoveToPoint(context, point.x, point.y);
                }
                else{
                    CGContextAddLineToPoint(context, point.x, point.y);
                }

                 pointArr[i] = point1;
                i++;
            }


            CGContextStrokePath(context);
        }        
    }   


    polyimage =  UIGraphicsGetImageFromCurrentImageContext(); 
   UIGraphicsEndImageContext();

我可以在上下文中绘制折线图像,然后在我的图像中获取该上下文。但现在我想在地图视图中将此图像添加为折线。

请帮助我, 在此先感谢。

2 个答案:

答案 0 :(得分:1)

只需阅读Apple文档。

ANYWAY:

//在C中分配:

int count = ... // number or points.
CLLocationCoordinate2D* arrayOfPoints =(CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) * (1+count) );
.... // fill a std C array... NOTE the +1, as we must add a closing point equal to the first.

    // now add first to close:
arrayOfPoints[count]  = arrayOfPoints[0];

MKPolyline * myPolyLine = [MKPolyline polylineWithCoordinates:arrayOfPoints count: count+1];
[myMapView addOverlay: myPolyLine];
free(arrayOfPoints);

...

// You need a call back:

 -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
 {
 MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
polylineView.strokeColor = [UIColor greenColor];
polylineView.lineWidth = 12.0;
return polylineView;    
}

答案 1 :(得分:0)