1

地図上の 2 点間のパスを表示したいコードがあります。問題は、ポイントの変位とともに正しいルートが表示されていることです。つまり、開始点と終了点も結合しています。

しかし、ループを使用する代わりにループなしで実行すると、コードは正しく機能します。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