gpt4 book ai didi

ios - 如何将 CGRect 转换为 MapRect

转载 作者:可可西里 更新时间:2023-11-01 06:16:52 27 4
gpt4 key购买 nike

我创建了如下方法将 CGRect 转换为 MapRect,如下所示

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

我做了一个小测试。

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

它在 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

但是,在 iOS 4.3 上的结果有点奇怪。

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

最佳答案

像这样的东西(Swift 代码)似乎工作正常。在这种情况下,mapView类型为 GMSMapView! , 所以 mapView.frame类型为 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)

关于ios - 如何将 CGRect 转换为 MapRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249890/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com