This blog is about adding custom overlay to a map. Overlays like polygon and circle can be added to the map. For instance you want to highlight or outline an area of a state or states in a country. In my application i have outlined all the states in US.
MKMapView _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 28, 360, 250)];
MKCoordinateRegion region;
region.center.latitude = 42.5116;
region.center.longitude = -90.6290;
region.span.longitudeDelta = 26.0;
region.span.latitudeDelta = 26.0;
_mapView.showsUserLocation = NO;
[_mapView setScrollEnabled:YES];
[_mapView setRegion:region];
[_mapView setDelegate:self];
[self.view addSubview:_mapView];
NSMutableArray* points; (contains coordinates of particular state)
CLLocationCoordinate2D *coords =
malloc(sizeof(CLLocationCoordinate2D) * [points count]);
for(int idx = 0; idx < [points count]; idx++) {
CLLocation* location = [points objectAtIndex:idx];
coords[idx] = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
}
polygon = [MKPolygon polygonWithCoordinates:coords count:[points count]];
free(coords);
[_mapView addOverlay:polygon];
3. Implement Delegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
{
MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
polygonView.lineWidth = 0.5;
polygonView.strokeColor = [UIColor whitecolor];
polygonView.fillColor = [[UIColor colorWithRed:0.9176 green:0.9098 blue:0.8117 alpha:1.0] colorWithAlphaComponent:0.5];;
return polygonView;
}
