Reputation: 421
I want to draw a straight line on an iOS map using MKmapkit without the map moving around.
For example, if the user draws a straight line between two places on the map without removing the finger off the map, I don't want the map view to move around while the user is drawing the line.
I've searched on google, but didn't find an answer/solution.
Please can somebody help me?
Upvotes: 1
Views: 2910
Reputation: 7733
Disable the mapkit scroll
_mapView.scrollEnabled=NO;
And then draw line with the help of core graphics
- (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;
}
Upvotes: 2
Reputation: 4789
You can draw poly line between two or more points/ coordinates.
Here is a question which is answered. Source: Draw a polyline between two coordinates in Xcode
You have to use MKOverlayView like below :
In Interface file
MKPolyline* _routeLine;
MKPolylineView* _routeLineView;
In Implementation file
Store all the coordinates in
NSMutablrArray *routeLatitudes
then
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);
and Overlay delegate
- (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;
}
Upvotes: 0