2013-03-18 89 views
2

我在ios地圖上工作。在這裏,當我使用覆蓋層在ios地圖上繪製一條線時,它顯示多行。現在我只想繪製一條直線。我怎樣才能做到這一點。 下面我附上了屏幕截圖的代碼。ios地圖問題 - 在ios地圖上顯示多條線條

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    if ([touches count]>1) 
    { 
     [super touchesBegan:touches withEvent:event]; 
     return; 
    } 
    UITouch *touch = [touches anyObject]; 
    tappedPoint1 = [touch locationInView:_mapView]; 
// _startCoord = [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView]; 
    coord1= [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView]; 
    polyline = nil; 
} 

    - (void)updatePolylineWithCoordinate:(CLLocationCoordinate2D)coord6 
{ 
    if (!_polyline) 
    { 
     MKMapPoint points[2]; 
     points[0] = MKMapPointForCoordinate(coord1); 
     points[1] = MKMapPointForCoordinate(coord6); 
     line2 = [MKPolyline polylineWithPoints:points count:2]; 
    } 
    else 
    { 
     NSUInteger count = _polyline.pointCount + 1; 
     // create new point array and add new coord 
     CLLocationCoordinate2D coords[count]; 
     [_polyline getCoordinates:coords range:NSMakeRange(0, count-1)]; 
     coords[count - 1] = coord6; 
     [_mapView removeOverlay:_polyline]; 
     line2 = [MKPolyline polylineWithCoordinates:coords count:count]; 
    } 
    line2.title = @"line"; 
    [_mapView addOverlay: line2]; 

// _polyline = line2; 
} 

     - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
     { 
      UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:_mapView]; 
     CLLocationCoordinate2D coord5= [_mapView convertPoint:currentPoint toCoordinateFromView:_mapView]; 
     NSLog(@"_startCoord.latitude : %f", coord5.latitude); 
     NSLog(@"_startCoord.lontitude : %f", coord5.longitude); 
     [self updatePolylineWithCoordinate:coord5]; 
     } 

     - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
     { 
      if ([overlay isKindOfClass:[MKPolyline class]]) 
      { 
       lineview=[[[MKPolylineView alloc] initWithOverlay:overlay] autorelease]; 
       lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5]; 
       lineview.fillColor = [[UIColor redColor]colorWithAlphaComponent:0.5]; 
       lineview.lineWidth=2.0; 
       return [lineview autorelease]; 
      } 

      return nil; 
     } 
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    [_mapView removeOverlay:line2]; 

    MKMapPoint points[2]; 
    points[0] = MKMapPointForCoordinate(coord1); 
    points[1] = MKMapPointForCoordinate(coord5); 
    line2 = [MKPolyline polylineWithPoints:points count:2]; 
    [_mapView addOverlay: line2]; 
    } 

現在,當我畫一條線使用觸摸運動時,它在地圖上隨處可見,取決於ios地圖。但我需要當用戶移動他的手指只有一條直線會顯示。我該如何做到這一點:請看看這個屏幕截圖。 enter image description here

可以任何機構請幫我解決這個問題。

+0

@nitin Gohel謝謝 – kannan 2013-03-18 11:00:08

回答

1

試試這個:

- (void)updatePolylineWithCoordinate:(CLLocationCoordinate2D)coord 
{ 
    MKPolyline* line; 
    if (!_polyline) { 
     MKMapPoint points[2]; 
     points[0] = MKMapPointForCoordinate(_startCoord); 
     points[1] = MKMapPointForCoordinate(coord);   
     line = [MKPolyline polylineWithPoints:points count:2]; 
    } else { 
     NSUInteger count = _polyline.pointCount + 1; 
     // create new point array and add new coord 
     CLLocationCoordinate2D coords[count]; 
     [_polyline getCoordinates:coords range:NSMakeRange(0, count-1)]; 
     coords[count - 1] = coord; 
     [_mapView removeOverlay:_polyline]; 
     line = [MKPolyline polylineWithCoordinates:coords count:count]; 
    } 
    line.title = @"line"; 
    [_mapView addOverlay: line]; 
    _polyline = line; 


} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if ([touches count]>1) { 
     [super touchesBegan:touches withEvent:event]; 
     return; 
    } 
    UITouch *touch = [touches anyObject]; 
    CGPoint tappedPoint1 = [touch locationInView:_mapView]; 
    _startCoord = [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView]; 

    DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease]; 
    annotation.subtitle = [NSString stringWithFormat:@"%f,%f", annotation.coordinate.latitude, annotation.coordinate.longitude]; 
    [_mapView addAnnotation:annotation]; 

    _polyline = nil; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:_mapView]; 
    CLLocationCoordinate2D coord= [_mapView convertPoint:currentPoint toCoordinateFromView:_mapView]; 
    [self updatePolylineWithCoordinate:coord]; 
} 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKPolyline class]]) 
    { 
     MKPolylineView* lineview= [[MKPolylineView alloc] initWithOverlay:overlay]; 
     lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5]; 
     lineview.fillColor = [[UIColor redColor]colorWithAlphaComponent:0.5]; 
     lineview.lineWidth=2.0; 
     return [lineview autorelease]; 
    } 
    return nil; 
} 

你只需添加2實例變量:

  • CLLocationCoordinate2D _startCoord;
  • MKPolyline * _polyline;
+0

非常感謝你的幫助,但朋友對不起。現在我有相同的代碼中的另一個問題。這是魔術代碼,如果我使用斷點進行調試,它工作正常。如果我運行沒有中斷點的代碼,得到這樣的輸出:http://postimage.org/image/vnpf0vdcv/61b61049/,也請看我的編輯代碼 – kannan 2013-03-18 12:25:09

+0

@ kannan我無法解釋這個問題,但必須有零值(0,0)的座標 - 可能變量未初始化。此外,我不能保證我的代碼可以同時處理多個觸摸。 – Felix 2013-03-18 13:50:01

+0

,但我在nslog – kannan 2013-03-18 14:05:38