Skip to content

Instantly share code, notes, and snippets.

@tommypeps
Forked from ebinnion/.m file
Created May 1, 2016 15:51
Show Gist options
  • Save tommypeps/f8b8347b32f12007ac64f7d33061b5a9 to your computer and use it in GitHub Desktop.
Save tommypeps/f8b8347b32f12007ac64f7d33061b5a9 to your computer and use it in GitHub Desktop.
Add polygon overlay to mapkit iOS
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView* aView = [[MKPolygonView alloc]initWithPolygon:(MKPolygon*)overlay];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
- (void)viewDidLoad
{
// Adds a polygon over the commuter parking in front of the library
CLLocationCoordinate2D libComPark[4];
libComPark[0] = CLLocationCoordinate2DMake(33.874689,-98.520148);
libComPark[1] = CLLocationCoordinate2DMake(33.87469,-98.519692);
libComPark[2] = CLLocationCoordinate2DMake(33.874314,-98.519687);
libComPark[3] = CLLocationCoordinate2DMake(33.874316,-98.520146);
MKPolygon *polLibcomPark = [MKPolygon polygonWithCoordinates:libComPark count:4];
[msuMap addOverlay:polLibcomPark];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Declare an array that will hold the coordinates within the viewDidLoad function/method. Then fill the array with the coordinates. Then define a MKPolygon pointer that will use that array. Last, add the overlay to the map.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment