gpt4 book ai didi

iphone - 无法将 MapKit 用户坐标转换为屏幕坐标

转载 作者:可可西里 更新时间:2023-11-01 06:22:30 26 4
gpt4 key购买 nike

好的,这实际上是一个线程中的三个不同问题:

1) 我正在使用:

- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setFrame :CGRectMake(-100,-100,520,520)];
[mapView setAutoresizesSubviews:true];
mapView.showsUserLocation = YES;

locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
[locManager startUpdatingLocation];
[locManager startUpdatingHeading];

[bt_toolbar setEnabled:YES];
}


- (IBAction) CreatePicture
{
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);

CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.mapView];
mapPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]];
mapPic.frame = CGRectMake(annPoint.x, annPoint.y, 32, 32);
[self.view addSubview:macPic];
}

使用 GCRectMake 将图像放置在用户的坐标上,但遗憾的是图像没有放置在正确的位置:

Coordinate conversion gone wrong

如果我移动 map ,图片将始终以与用户位置完全相同的偏移量放置。我错过了什么?它不应该直接放在用户上方吗?我猜我首先给 mapView 的偏移量 (-100, -100) 是造成这种情况的原因(我在下面有一个工具栏,因此有偏移量)。

2) 我的 iOS 5 模拟器表现得很疯狂,出于某种原因把我的位置放在凤凰城的格伦代尔(它不应该在库比蒂诺吗??)并且表现得像我在向东移动一样;尽管在我的 iPhone 4 中一切正常(除了图片位置不正确)。有什么想法吗?

3) 我一直在考虑改用注释,但我听说它们是静态的(我真的需要它们是动态的)。这是真的吗?

谢谢!

最佳答案

添加 subview 时,其框架原点与其父 View 的框架原点相关。在这种情况下,您从 map View 中导出原点,然后将图像添加到 View Controller 的 View 中。您需要像这样将图像的原点转换为 self.view 的坐标系:

CGPoint imageOrigin = [self.view convertPoint:annPoint fromView:self.mapView];
mapPic.frame.origin = imageOrigin;
[self.view addSubview:macPic];

注意:这是未经测试的代码,但主要是您需要在 mapView 的坐标空间(这是您从 [self.mapView convertCoordinate:coord toPointToView:self.mapView] 获得的)到 self.view 的坐标之间进行转换空间。这个例子有点做作来说明,因为更简单的方法是首先使用 self.view 的坐标空间:

CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.view];    

关于iphone - 无法将 MapKit 用户坐标转换为屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463896/

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