Skip to content

Commit

Permalink
Added support for drawing polylines on snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
IjzerenHein authored and kfiroo committed Oct 13, 2016
1 parent 83054eb commit 0138236
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions ios/AirMaps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
1125B2D31C4AD3DA007D0023 /* AIRMapPolyline.h */,
1125B2D51C4AD3DA007D0023 /* AIRMapPolylineManager.h */,
1125B2D61C4AD3DA007D0023 /* AIRMapPolylineManager.m */,
21B6E2D21D99B886007E664F /* AIRMapSnapshot.h */,
1125B2F01C4AD445007D0023 /* SMCalloutView.h */,
1125B2F11C4AD445007D0023 /* SMCalloutView.m */,
1125B2D81C4AD3DA007D0023 /* RCTConvert+MoreMapKit.h */,
Expand Down
6 changes: 6 additions & 0 deletions ios/AirMaps/AIRMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ - (void)takeMapSnapshot:(AIRMap *)mapView
[pin.image drawAtPoint:point];
}
}

for (id <AIRMapSnapshot> overlay in mapView.overlays) {
if ([overlay respondsToSelector:@selector(drawToSnapshot:context:)]) {
[overlay drawToSnapshot:snapshot context:UIGraphicsGetCurrentContext()];
}
}

UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();

Expand Down
39 changes: 39 additions & 0 deletions ios/AirMaps/AIRMapPolyline.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,43 @@ - (BOOL)canReplaceMapContent
return NO;
}


#pragma mark AIRMapSnapshot implementation

- (void) drawToSnapshot:(MKMapSnapshot *) snapshot context:(CGContextRef) context
{
// Prepare context
CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetLineCap(context, self.lineCap);
CGContextSetLineJoin(context, self.lineJoin);
CGContextSetMiterLimit(context, self.miterLimit);
CGFloat dashes[self.lineDashPattern.count];
for (NSUInteger i = 0; i < self.lineDashPattern.count; i++) {
dashes[i] = self.lineDashPattern[i].floatValue;
}
CGContextSetLineDash(context, self.lineDashPhase, dashes, self.lineDashPattern.count);

// Begin path
CGContextBeginPath(context);

// Get coordinates
CLLocationCoordinate2D coordinates[[self.polyline pointCount]];
[self.polyline getCoordinates:coordinates range:NSMakeRange(0, [self.polyline pointCount])];

// Draw line segments
for(int i = 0; i < [self.polyline pointCount]; i++) {
CGPoint point = [snapshot pointForCoordinate:coordinates[i]];
if (i == 0) {
CGContextMoveToPoint(context,point.x, point.y);
}
else{
CGContextAddLineToPoint(context,point.x, point.y);
}
}

// Finish path
CGContextStrokePath(context);
}

@end
17 changes: 17 additions & 0 deletions ios/AirMaps/AIRMapSnapshot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AIRMapSnapshot.h
// AirMaps
//
// Created by Hein Rutjes on 26/09/16.
// Copyright © 2016 Christopher. All rights reserved.
//

#ifndef AIRMapSnapshot_h
#define AIRMapSnapshot_h

@protocol AIRMapSnapshot <NSObject>
@optional
- (void) drawToSnapshot:(MKMapSnapshot *) snapshot context:(CGContextRef) context;
@end

#endif /* AIRMapSnapshot_h */

0 comments on commit 0138236

Please sign in to comment.