MKPolygon из точки + радиус/расстояние (iPhone SDK)

Как я могу рассчитать MKPolygon из CLLocationCoordinate2D (центральная точка) и CLLocationDistance (радиус — зеленая линия)?

Я хочу показать Rect на карте с заданным радиусом (синяя линия)

Прямая с радиусом

Спасибо


person x2on    schedule 23.03.2012    source источник


Ответы (2)


Я адаптировал ответ @Cyril Godefroy и добавил преобразование из Как сделать объединение двух 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;
}
person x2on    schedule 26.03.2012
comment
Я бы сказал, что то, как вы это делаете, слишком много обдумывает. Я понимаю, что проблема в том, чтобы перейти от метров к градусам. Более простой MKCoordinateRegionMakeWithDistance должен помочь вам получить более надежный и понятный код. - person Cyril Godefroy; 26.03.2012

Обычно у прямоугольника нет радиуса, а у круга есть. Итак, я думаю, вы имели в виду «сторону квадрата», потому что вы действительно хотите нарисовать квадрат?

Вам нужно рассчитать 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];
}
person Cyril Godefroy    schedule 23.03.2012
comment
Я добавил изображение, чтобы описать проблему. С вашим расчетом прямоугольник слишком велик. - person x2on; 25.03.2012
comment
Что такое радиус в вашем решении? Если радиус равен CLLocationDistance (в метрах), точки неверны. - person x2on; 26.03.2012
comment
Я не заметил, что вы использовали CLLocationDistance для радиуса. Мне это кажется довольно нелогичным. Извини за это. - person Cyril Godefroy; 26.03.2012