How to convert CGRect to MapRect
Asked Answered
B

1

4

I created a method as below to convert a CGRect to MapRect as below

- (MKMapRect)mapRectForRect:(CGRect)rect
{
    CLLocationCoordinate2D topleft = [mapView convertPoint:CGPointMake(rect.origin.x, rect.origin.y) toCoordinateFromView:canvasView];
    CLLocationCoordinate2D bottomeright = [mapView convertPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)) toCoordinateFromView:canvasView];
    MKMapPoint topleftpoint = MKMapPointForCoordinate(topleft);
    MKMapPoint bottomrightpoint = MKMapPointForCoordinate(bottomeright);

    return MKMapRectMake(topleftpoint.x, topleftpoint.y, bottomrightpoint.x - topleftpoint.x, bottomrightpoint.y - topleftpoint.y);
}

I did a little test.

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"heart.png"] imageByScalingProportionallyToSize:CGSizeMake(100, 100)]];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    imageView.center = [mapView convertCoordinate:mapView.centerCoordinate toPointToView:canvasView];
    [canvasView addSubview:imageView];

    CGRect rect = imageView.frame;
    for (int i = 0; i < 5; i ++)
    {
        NSLog(@"%f, %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        maprect = [self mapRectForRect:rect];
        NSLog(@"%f, %f, %f, %f", maprect.origin.x, maprect.origin.y, maprect.size.width, maprect.size.height);

        region = MKCoordinateRegionForMapRect(maprect);
        rect = [mapView convertRegion:region toRectToView:canvasView];
    }

It works pretty well on iOS 5:

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

But, the result on iOS 4.3 is a little weird.

110.000000, 180.000000, 100.000000, 100.000000

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

110.000000, 174.263596, 100.000000, 101.746048

78118912.000000, 91803986.406252, 52428800.000000, 53344231.999997

110.000000, 166.962997, 100.000000, 104.330460

78118912.000000, 87976370.406253, 52428800.000000, 54699207.999997

110.000000, 157.400009, 100.000000, 108.272507

78118912.000000, 82962610.406253, 52428800.000000, 56765975.999997

110.000000, 144.297470, 100.000000, 114.581375

78118912.000000, 76093106.406254, 52428800.000000, 60073639.999996

Babbler answered 12/2, 2012 at 15:17 Comment(3)
developer.apple.com/library/ios/#documentation/MapKit/Reference/… - (MKMapRect)mapRectForRect:(CGRect)rectTiberias
i checked it. it's not what i want. i want to convert a rect in other views to mapview. method on #6776218 does not work correctly either.Babbler
anybody has the same problem?Babbler
J
0

Something like this (Swift code) seems to work fine. In this case, mapView is of type GMSMapView!, so mapView.frame is of type CGRect.

let mkMapSize = MKMapSize(width: Double(self.mapView.frame.width), height: Double(self.mapView.frame.height))
let mkMapPoint = MKMapPoint(x: Double(self.mapView.frame.origin.x), y: Double(self.mapView.frame.origin.y))
let mkMapRect = MKMapRect(origin: mkMapPoint, size: mkMapSize)
Johannessen answered 8/10, 2015 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.