Skip to content

Instantly share code, notes, and snippets.

@ebinnion
Created January 4, 2012 16:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ebinnion/1560714 to your computer and use it in GitHub Desktop.
Save ebinnion/1560714 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.
@DonDinhIT
Copy link

Hi @ebinnion
With your code:
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);

How to get that all necessary CLLocationCoordinate2D with a city/state?
I'm looking forward your reply!
Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment