在MKMapView中的坐标之间绘制一条线

时间:2013-04-19 09:52:53

标签: iphone mkmapview cllocation cgcontextref

我想在MKMapView中的坐标之间绘制线条。这是代码

aRoute=[NSArray arrayWithObjects:[[CLLocation alloc] initWithLatitude:40.445418 longitude:-79.942714],[[CLLocation alloc] initWithLatitude:40.444469 longitude:-79.948628],[[CLLocation alloc] initWithLatitude:40.44569 longitude:-79.950388],[[CLLocation alloc] initWithLatitude:40.446967 longitude:-79.95053],[[CLLocation alloc] initWithLatitude:40.448874 longitude:-79.951715],[[CLLocation alloc] initWithLatitude:40.45134 longitude:-79.953175],[[CLLocation alloc] initWithLatitude:40.452213 longitude:-79.950895],[[CLLocation alloc] initWithLatitude:40.452564 longitude:-79.949838],[[CLLocation alloc] initWithLatitude:40.453528 longitude:-79.94641],[[CLLocation alloc] initWithLatitude:40.454109 longitude:-79.944487],[[CLLocation alloc] initWithLatitude:40.455858 longitude:-79.938618],[[CLLocation alloc] initWithLatitude:40.457224 longitude:-79.934069],[[CLLocation alloc] initWithLatitude:40.454777 longitude:-79.9932543],[[CLLocation alloc] initWithLatitude:40.453458 longitude:-79.931763],[[CLLocation alloc] initWithLatitude:40.452099 longitude:-79.931148],[[CLLocation alloc] initWithLatitude:40.451258 longitude:-79.930633],[[CLLocation alloc] initWithLatitude:40.449866 longitude:-79.929729],[[CLLocation alloc] initWithLatitude:40.448956 longitude:-79.932543],[[CLLocation alloc] initWithLatitude:40.44846 longitude:-79.934185],[[CLLocation alloc] initWithLatitude:40.447919 longitude:-79.937028], nil];
if (xapp.aRoute && xapp.aRoute .count > 0)
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetAlpha(context, 0.5);

    CGContextSetLineWidth(context, POLYLINE_WIDTH);

    for (int i = 0; i < xapp.aRoute .count; i++) {

        CLLocation* location =(CLLocation*) [xapp.aRoute  objectAtIndex:i];
        CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude);
        NSLog(@"%f%f",coordinate.longitude,coordinate.latitude);
        CGPoint point = [_mapView convertCoordinate:coordinate toPointToView:_mapView];
        if (i == 0)
            CGContextMoveToPoint(context, point.x, point.y);
        else
            CGContextAddLineToPoint(context, point.x, point.y);
         NSLog(@"%f%f",point.x,point.y);
            }

    CGContextStrokePath(context);

}

此问题是convertCoordinate对我不起作用..我的所有坐标都为CGPoints {0,0}

1 个答案:

答案 0 :(得分:1)

我在MapView上用绘制线编写的代码如下...

-(void) updateRouteView 
{
    CGContextRef context =  CGBitmapContextCreate(nil, 
                                                  routeView.frame.size.width, 
                                                  routeView.frame.size.height, 
                                                  8, 
                                                  4 * routeView.frame.size.width,
                                                  CGColorSpaceCreateDeviceRGB(),
                                                  kCGImageAlphaPremultipliedLast);

    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 3.0);

    for(int i = 0; i < routes.count; i++) 
    {
        CLLocation* location1 = [routes objectAtIndex:i];
        CGPoint point = [mapView convertCoordinate:location1.coordinate toPointToView:routeView];
        if(i == 0) 
        {
            CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
        }
        else 
        {
            CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
        }
    }

    CGContextStrokePath(context);

    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:image];

    routeView.image = img;
    CGContextRelease(context);

}

查看此演示Serendipitor

你可以从中得到一些想法......

我希望这对你有帮助......