Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.mapView.delegate = self;
  4. self.mapView.showsUserLocation = YES;
  5. }
  6.  
  7. // ...
  8.  
  9. // Later, I retrieve some models and generate CustomMapOverlay instances from them...
  10. for (Model *model in models) {
  11. CustomMapOverlay *customMapOverlay = [[CustomMapOverlay alloc] initWithModel:model];
  12. [self.mapView addOverlay:customMapOverlay];
  13. }
  14.  
  15. // ...
  16.  
  17. // Implement MKMapViewDelegate protocol
  18. - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
  19. MKPolygonRenderer *polygonRenderer = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
  20. polygonRenderer.lineWidth = 2;
  21. polygonRenderer.strokeColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
  22. polygonRenderer.fillColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.5];
  23. return polygonRenderer;
  24. }
  25.  
  26. @implementation CustomMapOverlay
  27.  
  28. // Required by MKOverlay protocol
  29. @synthesize coordinate,
  30. boundingMapRect;
  31.  
  32. - (instancetype)initWithModel:(Model *)model {
  33. coordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
  34. double radiusInPoints = MKMapPointsPerMeterAtLatitude(model.latitude) * model.radius;
  35. boundingMapRect = MKMapRectMake(model.latitude, model.longitude, radiusInPoints, radiusInPoints);
  36. return self;
  37. }
  38.  
  39. @end
  40.  
  41. CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
  42. MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCoordinate, model.radius, model.radius);
  43.  
  44. int numCoords = 4;
  45. CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numCoords);
  46. coords[0] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
  47. coords[1] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
  48. coords[2] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
  49. coords[3] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
  50. MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:numCoords];
  51. free(coords);
  52.  
  53. [self.mapView addOverlay:polygon];
  54.  
  55. coords[0] = CLLocationCoordinate2DMake(
  56. (region.center.longitude - 0.5*region.span.longitudeDelta),
  57. (region.center.latitude + 0.5*region.span.latitudeDelta));
  58.  
  59. coords[0] = CLLocationCoordinate2DMake(
  60. (region.center.latitude + 0.5*region.span.latitudeDelta,
  61. (region.center.longitude - 0.5*region.span.longitudeDelta));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement