【问题标题】:ios map issue- mulitiple lines are showing on ios mapios地图问题-ios地图上显示多行
【发布时间】:2013-03-18 07:07:18
【问题描述】:

我正在使用 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 地图。但我需要当用户移动他的手指时,只会显示一条直线。我该怎么做:请看这个屏幕截图。

任何人都可以帮我解决这个问题。

【问题讨论】:

    标签: iphone objective-c ios4 ios6


    【解决方案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;

    【讨论】:

    • 非常感谢您的帮助。但朋友很抱歉。现在在同一代码中遇到另一个问题。如果使用断点进行调试,它就是神奇的代码,它工作正常。如果我在没有断点的情况下运行代码,我会得到这样的输出:postimage.org/image/vnpf0vdcv/61b61049 并且请查看我的编辑代码
    • @kannan 我无法解释这个问题,但必须有一个零值 (0,0) 的坐标 - 可能变量未初始化。我也不能保证我的代码可以同时进行多次触摸。
    • 但在 nslog 中获取值
    • 抱歉打扰了。是的,我得到了答案。但现在我可以这样画了:postimage.org/image/knfbbltj7/630881bb。现在我只想画一条直线。不需要画一个圆等等。我能做些什么。我是新手。如此而已。请给我一个想法,这就足够了。请帮帮我。
    • 您可以将开始坐标存储在 touchesBegan 中,将结束坐标存储在 touchesEnded 中,然后在这两点之间绘制一条折线。您也可以在 touchesMoved 中绘制线,但始终创建​​一条新的折线并删除旧的折线,因为 MKPolyline 是不可变的。
    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多