1

我想在没有地图移动的情况下使用 MKmapkit 在 iOS 地图上绘制一条直线。

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

我在谷歌上搜索过,但没有找到答案/解决方案。
请问有人可以帮我吗?

4

2 回答 2

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;

}
于 2013-02-25T08:50:50.007 回答
0

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

这是一个已回答的问题。来源:在 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;
}
于 2013-02-25T06:47:25.290 回答