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

Posted

技术标签:

【中文标题】来自点 + 半径/距离的 MKPolygon (iPhone SDK)【英文标题】:MKPolygon from Point + Radius/Distance (iPhone SDK) 【发布时间】:2012-03-23 16:16:50 【问题描述】:

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

我想在给定半径的地图上显示一个矩形(蓝线)

谢谢

【问题讨论】:

【参考方案1】:

我改编了@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;

【讨论】:

我会说你这样做的方式是多虑了。我知道问题是从米到度。更简单的 MKCoordinateRegionMakeWithDistance 应该可以帮助您拥有更健壮和易于理解的代码。【参考方案2】:

通常矩形没有半径,圆有。所以我猜你的意思是“正方形的边”是因为你真的想画一个正方形?

您需要计算正方形角的 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];

【讨论】:

我添加了一张图片来描述问题。根据您的计算,矩形太大了。 您的解决方案中的半径是多少?如果半径是 CLLocationDistance(米),则点是错误的。 我没有注意到您使用 CLLocationDistance 作为半径。对我来说似乎相当反直觉。对此感到抱歉。

以上是关于来自点 + 半径/距离的 MKPolygon (iPhone SDK)的主要内容,如果未能解决你的问题,请参考以下文章

R 中的 X 和 Y 坐标。是不是有一种明显的方法可以删除距离给定点一定半径的数据?

R:当点重叠/在距离内时追加数据;将缓冲区矩形添加到 set1,将半径添加到 set2

使用由两个远点定义的半径绘制的 MKCircle 渲染不正确

MySQL计算坐标之间距离

Python 计算三维空间某点距离原点的欧式距离

1)定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法; 2)定义一个圆形类,其属性包括圆心和半径; 3)创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果