casillas
casillas

Reputation: 16813

Draw Rectangle on the MapView

My application gets data in the following coverage area. I would like to use these boundary values to draw rectangle on the map. Do you have any idea? Thanks in advance

Left Upper Boundary: 30.439217,-95.899668;

Right Upper Boundary: 30.443953,-94.685679;

Left Buttom boundary: 28.930662,-95.908595;

Right Buttom Boundary: 28.930061,-94.690486;

Upvotes: 2

Views: 3709

Answers (3)

lionserdar
lionserdar

Reputation: 2032

if you wanna draw something basic with lines u can use this MKPolyline

Try the code below, Make sure your delegate is connected with your mapview object

- (void) drawRect
{

    CLLocation *coordinates1 =  [[CLLocation alloc] initWithLatitude:30.439217 longitude:-95.899668];

    CLLocation *coordinates2 =  [[CLLocation alloc] initWithLatitude:30.443953 longitude:-94.685679];

    CLLocation *coordinates3 =  [[CLLocation alloc] initWithLatitude:28.930061 longitude:-94.690486];

    CLLocation *coordinates4 =  [[CLLocation alloc] initWithLatitude:28.930662 longitude:-95.908595];


    NSMutableArray *locationCoordinates = [[NSMutableArray alloc] initWithObjects:coordinates1,coordinates2,coordinates3,coordinates4,coordinates1, nil];

    int numberOfCoordinates = [locationCoordinates count];

    CLLocationCoordinate2D coordinates[numberOfCoordinates];


    for (NSInteger i = 0; i < [locationCoordinates count]; i++) {

        CLLocation *location = [locationCoordinates objectAtIndex:i];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[i] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
    [self.myMapView addOverlay:polyLine];


}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 1.0;

    return polylineView;
}

UPDATE: Here's the swift version of the drawRect function

func drawRect() {
    let coordinates1 = CLLocation(latitude: 30.439217, longitude: -95.899668)
    let coordinates2 = CLLocation(latitude: 30.443953, longitude: -94.685679)
    let coordinates3 = CLLocation(latitude: 28.930061, longitude: -94.690486)
    let coordinates4 = CLLocation(latitude: 28.930662, longitude: -95.908595)
    let locationCoordinates = [coordinates1,coordinates2,coordinates3,coordinates4,coordinates1]

    let coordinates = locationCoordinates.map { $0.coordinate }

    let polyLine = MKPolyline(coordinates: coordinates, count: coordinates.count)
    mapView.addOver(polyLine)
}

replace the

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay (deprecated)

with

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer

Upvotes: 5

Ulaş Sancak
Ulaş Sancak

Reputation: 907

First:

#import <QuartzCore/QuartzCore.h>

Then do this where you want:

CLLocationCoordinate2D koor = CLLocationCoordinate2DMake(30.439217,-95.899668);
CLLocationCoordinate2D koor2 = CLLocationCoordinate2DMake(30.443953,-94.685679);
CLLocationCoordinate2D koor3 = CLLocationCoordinate2DMake(28.930662,-95.908595);
CGPoint point1 = [mapView convertCoordinate:koor toPointToView:mapView];
CGPoint point2 = [mapView convertCoordinate:koor2 toPointToView:mapView];
CGPoint point3 = [mapView convertCoordinate:koor3 toPointToView:mapView];
UIView *someView = [[UIView alloc]initWithFrame:CGRectMake(point1.x, point1.y, point2.x-point1.x, point3.y-point1.y)];
[self viewBicimle:someView];
[someView setBackgroundColor:[UIColor clearColor]];
[someView.layer setBorderColor:[[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]];
[someView.layer setBorderWidth:1.0];
[mapView addSubview:someView];
[someView release];

But this will only work for an immovable some MapView. When you move the map it will slide...

Upvotes: 0

Abizern
Abizern

Reputation: 150685

Since you've asked a general question I can only give you a general hint at the answer.

MKMapView has a method convertCoordinate:toPointToView: which turns a coordinate to a point in the view. You can use this to draw on an overlay.

Upvotes: 1

Related Questions