Drawing a line between the coordinates in MKMapView - iphone

I want to draw the lines between the coordinates in MKMapView..Here is the code
aRoute=[NSArray arrayWithObjects:[[CLLocation alloc] initWithLatitude:40.445418 longitude:-79.942714],[[CLLocation alloc] initWithLatitude:40.444469 longitude:-79.948628],[[CLLocation alloc] initWithLatitude:40.44569 longitude:-79.950388],[[CLLocation alloc] initWithLatitude:40.446967 longitude:-79.95053],[[CLLocation alloc] initWithLatitude:40.448874 longitude:-79.951715],[[CLLocation alloc] initWithLatitude:40.45134 longitude:-79.953175],[[CLLocation alloc] initWithLatitude:40.452213 longitude:-79.950895],[[CLLocation alloc] initWithLatitude:40.452564 longitude:-79.949838],[[CLLocation alloc] initWithLatitude:40.453528 longitude:-79.94641],[[CLLocation alloc] initWithLatitude:40.454109 longitude:-79.944487],[[CLLocation alloc] initWithLatitude:40.455858 longitude:-79.938618],[[CLLocation alloc] initWithLatitude:40.457224 longitude:-79.934069],[[CLLocation alloc] initWithLatitude:40.454777 longitude:-79.9932543],[[CLLocation alloc] initWithLatitude:40.453458 longitude:-79.931763],[[CLLocation alloc] initWithLatitude:40.452099 longitude:-79.931148],[[CLLocation alloc] initWithLatitude:40.451258 longitude:-79.930633],[[CLLocation alloc] initWithLatitude:40.449866 longitude:-79.929729],[[CLLocation alloc] initWithLatitude:40.448956 longitude:-79.932543],[[CLLocation alloc] initWithLatitude:40.44846 longitude:-79.934185],[[CLLocation alloc] initWithLatitude:40.447919 longitude:-79.937028], nil];
if (xapp.aRoute && xapp.aRoute .count > 0)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetAlpha(context, 0.5);
CGContextSetLineWidth(context, POLYLINE_WIDTH);
for (int i = 0; i < xapp.aRoute .count; i++) {
CLLocation* location =(CLLocation*) [xapp.aRoute objectAtIndex:i];
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude);
NSLog(#"%f%f",coordinate.longitude,coordinate.latitude);
CGPoint point = [_mapView convertCoordinate:coordinate toPointToView:_mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
NSLog(#"%f%f",point.x,point.y);
}
CGContextStrokePath(context);
}
Here the problem is convertCoordinate is not working for me.. I'm getting the CGPoints as {0,0} for all the coordinates

I write code from draw line on MapView are bellow...
-(void) updateRouteView
{
CGContextRef context = CGBitmapContextCreate(nil,
routeView.frame.size.width,
routeView.frame.size.height,
8,
4 * routeView.frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
for(int i = 0; i < routes.count; i++)
{
CLLocation* location1 = [routes objectAtIndex:i];
CGPoint point = [mapView convertCoordinate:location1.coordinate toPointToView:routeView];
if(i == 0)
{
CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
}
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];
routeView.image = img;
CGContextRelease(context);
}
See this Demo Serendipitor
may you get some idea from this....

Related

Draw line with border iPhone using CGContext

I want to draw line with border,
right now i am doing same thing twice, means first i draw line with stroke +3 and again draw line with normal stroke like 1,
so i do run loop twice for it,
is there any other way for better performance? or any anthor way to draw two line simultaniously.??
My code is
if (locations && ([locations length]/16 > 1)) {
CGPoint point;
polylinewidth=10;
CGContextSetLineWidth(context, polylinewidth);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
[loc drain];
}
CGContextStrokePath(context);
CGContextSetLineWidth(context, polylinewidth-5);
CGContextSetAlpha(context, 0.6);
CGContextSetStrokeColorWithColor(context, color.CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
[loc drain];
}
CGContextStrokePath(context);
}
Thanks.

How to connect 2 annotations with a coloured line inside a MKMapView?

How can i connect 2 annotations inside a Mkmapview with a coloured line ?
google direction api is available for that..pass the origin and destination location
//http://maps.googleapis.com/maps/api/directions/jsonorigin=origin_place&destination=destination_place&waypoints=Charlestown,MA|Lexington,MA&sensor=false
It will give the jSON response which you have parse and pull coordinates. Make a line with that
self.routeLine = [MKPolyline polylineWithPoints:arrPoints count:routeArray.count];
here create create object routeView as as UIImageView class and also lineColor as a UIColor class in .h file like bellow
UIImageView* routeView;
UIColor* lineColor;
after in viewDidLoad: method write this code..
routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
routeView.userInteractionEnabled = NO;
lineColor = [UIColor magentaColor];
[mapView addSubview:routeView];
and then update when you want to draw line.. and also routes is NSMutableArray in which we store CLLocation (lat-long)
-(void) updateRouteView
{
CGContextRef context = CGBitmapContextCreate(nil,
routeView.frame.size.width,
routeView.frame.size.height,
8,
4 * routeView.frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
for(int i = 0; i < routes.count; i++)
{
CLLocation* location1 = [routes objectAtIndex:i];
CGPoint point = [mapView convertCoordinate:location1.coordinate toPointToView:routeView];
if(i == 0)
{
CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
}
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];
routeView.image = img;
CGContextRelease(context);
}
also see this link iphone-how-to-draw-line-between-two-points-on-mapkit
This looks to complicated to me. I found another solution online. Where I first have to add the overlay to my mapView
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = start_location;
coordinateArray[1] = end_location;
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
/***********************************************/
[_mapView addOverlay:self.routeLine];
Then I set my viewcontroller as a mkmapviewdelegate and implementing the delegate method :
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;}
But it still does not show up, what am I doing wrong?

draw gradient orientation

So I have the following code:
-
(void) drawLinearGradient:(CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 0.3, 1.0 };
CGColorRef grayColor = [UIColor colorWithRed:37/255.f green:37/255.f
blue:37/255.f alpha:1.0].CGColor;
CGColorRef blueColor = [UIColor colorWithRed:23.0/255.0 green:171.0/255.0
blue:219.0/255.0 alpha:1.0].CGColor;
NSArray *colors = [NSArray arrayWithObjects: (id) grayColor, (id) grayColor, (id) blueColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
What this does is draw the gradient from top to bottom, how can I make it so it draws it from left to right?
Change these two lines:
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
to:
CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));

Subclassing UIScrollview

I have been trying desperately to draw some images into a view. The view should be inside a scrollview. For that I subclassed UIScrollview and override the drawRect method in it. And added this as my UIView's subview.
#interface DrawAnotherViewClass : UIScrollView<UIScrollViewDelegate> {
}
#end
#implementation DrawAnotherViewClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
self.frame = fullScreenRect;
self.contentSize = CGSizeMake(600, 600);
self.showsHorizontalScrollIndicator = YES;
self.showsVerticalScrollIndicator = NO;
self.pagingEnabled = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 10.0f, 50.0f);
CGContextAddLineToPoint(context, 10.0f, 200.0f);
CGContextStrokePath(context);
CGContextMoveToPoint(context, 8.0f, 77.0f);
CGContextAddLineToPoint(context, 300.0f, 77.0f);
CGContextStrokePath(context);
CGContextSetRGBFillColor(context, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(context, 0, 0, 255, 1);
CGContextStrokeEllipseInRect(context, CGRectMake(65.0, 33.5, 25, 25));
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
CGPoint drawPoint = CGPointMake(0.0f, 10.0f);
[image2 drawAtPoint:drawPoint];
for(int i =1; i<20; i++){
CGPoint drawPointOne = CGPointMake(40.0f * i, 40.0f);
[image1 drawAtPoint:drawPointOne];
}
}
Am I missing something here. Is this the right way to go.
If the view that should perform the drawing resides in that UIScrollView, you have to put the - (void)drawRect:(CGRect)rect method into that view's class method and not into the UIScrollView subclass.

Why are CALayers stacking ontop of each other as I scroll my UITableView?

I have a UITableViewCell with a UIImage that I'm drawing. As I scroll, I get a ton of sublayers getting added which makes performance really jerky. How can I make sure my CALayer only gets added once?
- (void) drawContentView:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] set];
CGContextFillRect(context, rect);
NSString* caption = [NSString stringWithFormat:#"%#",[info objectForKey:#"caption"]];
NSString* text = [info stringForKey:#"text"];
CGFloat widthr = self.frame.size.width - 70;
[[UIColor grayColor] set];
[text drawInRect:CGRectMake(63.0, 25.0, widthr, 20.0) withFont:system14 lineBreakMode:UILineBreakModeTailTruncation];
if (self.image) {
UIImage *imageToDisplay;
imageToDisplay = self.image;
imageToDisplay = [self imageWithImage:imageToDisplay scaledToSize:CGSizeMake(imageToDisplay.size.width / 1.5, imageToDisplay.size.height / 1.5)];
CGFloat width;
CGFloat height;
CGRect r;
if (imageToDisplay.size.width < 310 && imageToDisplay.size.height > 290) {
imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(0, 20, imageToDisplay.size.width, 270)];
}
else if (imageToDisplay.size.width > 310 && imageToDisplay.size.height < 20) {
imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, imageToDisplay.size.height)];
}
else {
if (![caption isEqualToString:#""]) {
imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 230)];
}
else {
imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 270)];
}
}
width = imageToDisplay.size.width;
height = imageToDisplay.size.height;
r = CGRectMake(5.0, 5.0, width, height);
//[imageToDisplay drawInRect:r];
CALayer *sublayer = [CALayer layer];
sublayer.contents = (id)imageToDisplay.CGImage;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height);
[self.layer addSublayer:sublayer];
//Experimental shadow stuff with images
/*CALayer *layer = [CALayer layer];
layer = [CALayer layer];
layer.bounds = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height);
layer.position = CGPointMake(150, 140);
layer.contents = (id)imageToDisplay.CGImage;
layer.shadowOffset = CGSizeMake(0, 2);
layer.shadowOpacity = 0.70;
[self.layer addSublayer:layer];
[self bezierPathWithCurvedShadowForRect:layer.bounds];*/
[[UIColor blackColor] set];
[caption drawInRect:CGRectMake(10.0, height + 20 , widthr, 20.0) withFont:system14 lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}
}
drawContentView: is not the right method from which to call addSublayer:. You should add that layer at the point when you construct your object with an image, or when you set an image on an existing object. Alternatively, you can draw the image yourself in the drawContentView:, but it is probably not going to be as fast.