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

Joã*_*ira 3 iphone mapkit ios ios-simulator ios5

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

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];
}
Run Code Online (Sandbox Code Playgroud)

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

协调转换出错了

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

2)我的iOS 5模拟器表现得很疯狂,把我的位置放在凤凰城的格伦代尔(不应该是在Cupertino吗?)出于某种原因,并且因为我正在向东移动; 我的iPhone 4一切正常(除了不正确的图片放置).有任何想法吗?

3)我一直在考虑使用Annotations,但我听说它们是静态的(我真的需要它们是动态的).这是真的?

谢谢!

kal*_*rin 6

添加子视图时,其帧原点相对于其超视图的原点.在这种情况下,您从地图视图派生了一个原点,然后将该图像添加到视图控制器的视图中.您需要将图像的原点转换为self.view的坐标系,如下所示:

CGPoint imageOrigin = [self.view convertPoint:annPoint fromView:self.mapView];
mapPic.frame.origin = imageOrigin;
[self.view addSubview:macPic];
Run Code Online (Sandbox Code Playgroud)

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

CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.view];    
Run Code Online (Sandbox Code Playgroud)