2013-02-25 46 views
1

我想使用MKmapkit在iOS地图上画一条直线,而不需要移动地图。如何在不移动地图的情况下在iOS地图上绘制直线使用MKmapkit

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

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

+0

的帮助下没有'UIMapKit'。有'MKMapKit',但由于你的问题被标记为iOS 6,'MKMapKit'使用苹果地图,而不是谷歌地图。 – 2013-02-25 06:52:52

+0

@斯科特对不起,错了。感谢您的回复。 – kannan 2013-02-25 07:01:04

回答

0

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

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

你必须使用MKOverlayView象下面这样:在

NSMutablrArray *routeLatitudes 

然后

在接口文件

MKPolyline* _routeLine; 
MKPolylineView* _routeLineView; 

在实现文件

存储所有坐标

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; 
} 
+0

检查我编辑的答案。 – 2013-02-25 07:15:16

+0

@siva Prasad Hota谢谢。 – kannan 2013-02-25 07:17:32

+0

@siva Prasad Hota我想要用户触摸屏幕并在ios地图上绘制直线而不移动ios地图(MKMapKit)。 – kannan 2013-02-25 07:53:43

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; 

} 
+0

drawimage是UIImageView的一个实例,我将它用作覆盖在MAP上。在viewController的头文件中创建该实例。 – 2013-02-25 09:40:00

+0

不好意思打扰了。该代码工作正常。但是当我画圆时,它会画。但我不想画圆。我怎样才能删除(或)我如何绘制直线只。 – kannan 2013-02-27 14:56:50

相关问题