如何在iOS地图上绘制直线而不使用MKmapkit移动地图

时间:2013-02-25 06:21:54

标签: iphone cocoa-touch ios6 mapkit

我想使用MKmapkit在iOS地图上绘制一条直线,而不会移动地图。

例如,如果用户在地图上的两个地方之间绘制一条直线而不将手指从地图上移开,我不希望地图视图在用户绘制线条时移动。

我在google上搜索过,但没有找到答案/解决方案 请有人帮帮我吗?

2 个答案:

答案 0 :(得分:2)

禁用mapkit滚动

_mapView.scrollEnabled=NO;

然后借助核心图形绘制线条

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

    UITouch *touch = [touches anyObject];


    if (drawImage==nil) {
        drawImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [self.view addSubview:drawImage];
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;
    }

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

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:drawImage];
    currentPoint.y -= 20;

    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

答案 1 :(得分:0)

您可以在两个或多个点/坐标之间绘制多边形线。

这是一个回答的问题。资料来源:Draw a polyline between two coordinates in Xcode

你必须使用如下的MKOverlayView:

在接口文件

MKPolyline* _routeLine;
MKPolylineView* _routeLineView;

在实施文件

将所有坐标存储在

NSMutablrArray *routeLatitudes

然后

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 
for(int idx = 0; idx < [routeLatitudes count]; idx++)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLongitudes objectAtIndex:idx] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   
// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]];
[mapViewHome addOverlay:self.routeLine];
free(pointArr);

和重叠代表

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
  {
   MKOverlayView* overlayView = nil;

 if(overlay == routeLine)
{
    routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]  autorelease];
    routeLineView.fillColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957  alpha:1];
    routeLineView.strokeColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957 alpha:1];
    routeLineView.lineWidth = 4;

    overlayView = routeLineView;
}
return overlayView;
}
相关问题