kannan
kannan

Reputation: 421

ios map issue- mulitiple lines are showing on ios map

Am working in ios map. Here when i draw a line on ios map using overlayer it is showing multiple lines. Now i want to draw only one straight line. How can i do this. Below i have attached the code with screen shot.

    - (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];
  }

Now when i draw a line using touch movement it is draw on everywhere on map depend on ios map. but i need when the user move his finger only one straight line will showing. how can i do this: please see this screen shot. enter image description here

could any body please help me to solve this issue.

Upvotes: 2

Views: 314

Answers (1)

Felix
Felix

Reputation: 35384

Try this:

- (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;
}

You'll need to add only 2 instance variables:

  • CLLocationCoordinate2D _startCoord;
  • MKPolyline* _polyline;

Upvotes: 1

Related Questions