点+半径/距离的MKPolygon(iPhone SDK)

时间:2012-03-23 16:16:50

标签: iphone ios4 mapkit

如何从CLLocationCoordinate2D(中心点)和CLLocationDistance(半径 - 绿线)计算MKPolygon?

我想在给定半径(蓝线)

的地图上显示一个Rect

Rect with Radius

由于

2 个答案:

答案 0 :(得分: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;
}

您可以使用MapCallouts示例轻松测试它,方法是将allAction方法替换为:

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

答案 1 :(得分:0)

我调整了@Cyril Godefroy的答案并添加了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;
}