Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually a rectangle doesn't have a radius, a circle has. So I guess you meant 'side of a square' because you actually want to draw a square?</p> <p>You need to calculate the 4 coordinates of your square's corners. </p> <pre><code>- (void) addSquareToMap:(CLLocation*)center withRadius:(float)radius{ // Create a C array of size 4 CLLocationCoordinate2D points[4]; //Fill the array with the four corners (center - radius in each of four directions) points[0] = CLLocationCoordinate2DMake(center.coordinate.latitude-radius, coord.longitude-radius); points[1] = CLLocationCoordinate2DMake(center.coordinate.latitude+radius, coord.longitude-radius); points[2] = CLLocationCoordinate2DMake(center.coordinate.latitude+radius, coord.longitude+radius); points[3] = CLLocationCoordinate2DMake(center.coordinate.latitude-radius, coord.longitude+radius); //Create the polygon MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4]; poly.title = @"Square"; // Add it to the map [map addOverlay:poly]; } - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay{ if ([overlay isKindOfClass:[MKPolygon class]]){ MKPolygonView* aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease]; aView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.15]; aView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7]; aView.lineWidth = 3; return aView; } return nil; </code></pre> <p>}</p> <p>EDIT: Sorry I misread the "meters" portion of your question, so here is a more simple answer.</p> <pre><code>- (void) addSquareToMap:(CLLocation*)center withRadius:(float)radius{ MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center.coordinate, radius*2, radius*2); CLLocationCoordinate2D points[4]; //Fill the array with the four corners (center - span/2 in each of four directions) points[0] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2); points[1] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2); points[2] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2); points[3] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2); MKPolygon *polygon = [MKPolygon polygonWithCoordinates:points count:4]; polygon.title = @"Square"; // Add it to the map [self.mapView addOverlay:polygon]; } - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay{ if ([overlay isKindOfClass:[MKPolygon class]]){ MKPolygonView* aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease]; aView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.15]; aView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7]; aView.lineWidth = 3; return aView; } return nil; } </code></pre> <p>You can easily test it with the MapCallouts example by replacing the allAction method with:</p> <pre><code>- (IBAction)allAction:(id)sender{ //CLLocationCoordinate2D SF = CLLocationCoordinate2DMake(37.786996, -122.440100) ; CLLocation *center = [[CLLocation alloc] initWithLatitude:37.786996 longitude:-122.440100]; [self addSquareToMap:center withRadius:1000]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload