我想使用MKmapkit在iOS地圖上畫一條直線,而不需要移動地圖。如何在不移動地圖的情況下在iOS地圖上繪製直線使用MKmapkit
例如,如果用戶在地圖上的兩個地方之間繪製了一條直線而沒有將手指從地圖上移開,我不希望地圖視圖在用戶繪製線條時四處移動。
我已經在谷歌搜索,但沒有找到答案/解決方案。
請有人可以幫我嗎?
我想使用MKmapkit在iOS地圖上畫一條直線,而不需要移動地圖。如何在不移動地圖的情況下在iOS地圖上繪製直線使用MKmapkit
例如,如果用戶在地圖上的兩個地方之間繪製了一條直線而沒有將手指從地圖上移開,我不希望地圖視圖在用戶繪製線條時四處移動。
我已經在谷歌搜索,但沒有找到答案/解決方案。
請有人可以幫我嗎?
您可以在兩個或多個點/座標之間繪製多邊形線。
這是一個回答的問題。來源: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;
}
禁用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;
}
drawimage是UIImageView的一個實例,我將它用作覆蓋在MAP上。在viewController的頭文件中創建該實例。 – 2013-02-25 09:40:00
不好意思打擾了。該代碼工作正常。但是當我畫圓時,它會畫。但我不想畫圓。我怎樣才能刪除(或)我如何繪製直線只。 – kannan 2013-02-27 14:56:50
的幫助下沒有'UIMapKit'。有'MKMapKit',但由於你的問題被標記爲iOS 6,'MKMapKit'使用蘋果地圖,而不是谷歌地圖。 – 2013-02-25 06:52:52
@斯科特對不起,錯了。感謝您的回覆。 – kannan 2013-02-25 07:01:04