1

我有一个代码,我想在其中显示地图上两点之间的路径。问题是它显示了正确的路线以及点的位移,即它也连接了起点和终点。

但是,如果我不使用循环而不是使用循环,则代码可以正常工作。是不是像 CGContextCloseGraph() 在循环中自动调用?

请帮帮我。这是我的代码。

     - (void)drawRect:(CGRect)rect
     {

     if(!self.hidden && nil != self.points && self.points.count > 0)
     {
       CGContextRef context = UIGraphicsGetCurrentContext(); 

       if(nil == self.lineColor)
        self.lineColor = [UIColor blueColor];

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


       CGContextSetLineWidth(context, 2.0);

               // This Loop doesnt works..

       for(int i = 0; i < self.points.count; i++)
       {
           CLLocation* location = [self.points objectAtIndex:i];
             CGPoint point = [_mapView convertCoordinate:location.coordinate toPointToView:self];


        if(i == 0)
        {
            // move to the first point
            CGContextMoveToPoint(context, point.x, point.y);
        }
        else
        {
            CGContextAddLineToPoint(context, point.x, point.y);
        }
        }
               // This works..

              /*CLLocation* location = [self.points objectAtIndex:0];
              CGPoint point = [_mapView convertCoordinate:location.coordinate toPointToView:self];

            CGContextMoveToPoint(context, point.x, point.y);

            location = [self.points objectAtIndex:1];
            point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
            CGContextAddLineToPoint(context, point.x, point.y);

           location = [self.points objectAtIndex:2];
           point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
           CGContextAddLineToPoint(context, point.x, point.y);
           location = [self.points objectAtIndex:3];
           point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
           CGContextAddLineToPoint(context, point.x, point.y);
           location = [self.points objectAtIndex:4];
           point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
           CGContextAddLineToPoint(context, point.x, point.y);
           location = [self.points objectAtIndex:5];
           point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
           CGContextAddLineToPoint(context, point.x, point.y);*/


    CGContextStrokePath(context);
    }
     }
4

0 回答 0