2012-03-23 86 views

回答

0

我適於從@Cyril戈德弗魯瓦答案和從How to make the union between two MKCoordinateRegion

- (MKMapRect) mapRectForCoordinateRegion:(MKCoordinateRegion)coordinateRegion { 
    CLLocationCoordinate2D topLeftCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude + (coordinateRegion.span.latitudeDelta/2.0), coordinateRegion.center.longitude - (coordinateRegion.span.longitudeDelta/2.0)); 

    MKMapPoint topLeftMapPoint = MKMapPointForCoordinate(topLeftCoordinate); 

    CLLocationCoordinate2D bottomRightCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude - (coordinateRegion.span.latitudeDelta/2.0), coordinateRegion.center.longitude + (coordinateRegion.span.longitudeDelta/2.0)); 

    MKMapPoint bottomRightMapPoint = MKMapPointForCoordinate(bottomRightCoordinate); 

    MKMapRect mapRect = MKMapRectMake(topLeftMapPoint.x, topLeftMapPoint.y, fabs(bottomRightMapPoint.x - topLeftMapPoint.x), fabs(bottomRightMapPoint.y - topLeftMapPoint.y)); 

    return mapRect; 
} 

- (void) addSquareToMap:(CLLocation*)center withRadius:(float)radius{ 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, radius*2, radius*2); 
    MKMapRect rect = [self mapRectForCoordinateRegion:region]; 

    MKMapPoint leftBottom = MKMapPointMake(MKMapRectGetMinX(rect), MKMapRectGetMinY(rect)); 
    MKMapPoint leftTop = MKMapPointMake(MKMapRectGetMinX(rect), MKMapRectGetMaxY(rect)); 
    MKMapPoint rightTop = MKMapPointMake(MKMapRectGetMaxX(rect), MKMapRectGetMaxY(rect)); 
    MKMapPoint rightBottom = MKMapPointMake(MKMapRectGetMaxX(rect), MKMapRectGetMinY(rect)); 

    MKMapPoint rect[4] = {leftBottom, leftTop, rightTop, rightBottom}; 

    MKPolygon *polygon = [MKPolygon polygonWithPoints:rect count:4]; 
    poly.title = @"Square"; 

    // Add it to the map 
    [map addOverlay:poly]; 
} 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)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; 
} 
+0

我想說,你這樣做是過度的。我知道問題是從米到度。更簡單的MKCoordinateRegionMakeWithDistance應該可以幫助您獲得更強大,更易於理解的代碼。 – 2012-03-26 16:03:58

0

通常矩形沒有一個半徑,圓了。所以我想你的意思是「方形的一面」,因爲你實際上想畫一個正方形?

您需要計算您的方形角落的4個座標。

- (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 <MKOverlay>)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; 

}

編輯:對不起,我誤解你的問題的「米」部分,所以這裏是一個更簡單的答案。

- (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 <MKOverlay>)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; 
} 

可以很容易地通過用替換allAction方法與MapCallouts示例測試:

- (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]; 
} 
+0

添加的轉換我添加了一張圖片來描述問題。在你的計算中,矩形太大了。 – x2on 2012-03-25 09:53:56

+0

解決方案中的半徑是多少?如果半徑是CLLocationDistance(米),則分數是錯誤的。 – x2on 2012-03-26 08:24:20

+0

我沒有注意到你使用半徑的CLLocationDistance。似乎相反,我直覺。對於那個很抱歉。 – 2012-03-26 15:30:32

相關問題