2012-11-03 194 views
0

我的應用程序獲取以下覆蓋區域中的數據。我想用這些邊界值在地圖上繪製矩形。你有什麼主意嗎?在此先感謝在地圖上繪製矩形

左上邊界:30.439217,-95.899668;

右上邊界:30.443953,-94.685679;

Left Buttom邊界:28.930662,-95.908595;

Right Buttom邊界:28.930061,-94.690486;

回答

4

,如果你想畫的東西基本與線u可以使用這個MKPolyline

嘗試下面的代碼,請確保您的代理與您的MapView對象

連接
- (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; 
} 
1

既然您已經提出了一般性問題,我只能給您一個關於答案的一般提示。

MKMapView有一個方法convertCoordinate:toPointToView:它將座標轉換到視圖中的一個點。您可以使用它來繪製疊加層。

0

首先:您想要

#import <QuartzCore/QuartzCore.h> 

那麼做到這一點:

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]; 

但這隻會對一個固定的一些MapView的工作。當您移動地圖將幻燈片...

+0

添加的子視圖不會隨地圖平鋪 – adauguet