无法将MapKit用户坐标转换为屏幕坐标 [英] Trouble converting MapKit user coordinates to screen coordinates

查看:121
本文介绍了无法将MapKit用户坐标转换为屏幕坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

OK this is actually three distinct questions in one thread:

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在用户坐标上放置图像,但遗憾的是图像是没有被放置在正确的位置:

to place an image using GCRectMake over the user's coordinates, but sadly the image is not being placed in the right spot:

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

And if i move the map, the picture will always be placed with the exact same offset regarding the user's location. What am i missing? Shouldn't it have been placed directly above the user? I'm guessing the offset (-100, -100) I gave the mapView in the first place is what's causing this (i've got a toolbar below, hence the offset).

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

2) My iOS 5 simulator is acting crazy, placing my location in Glendale, Phoenix (shouldn't it have been in Cupertino??) for some reason and acting as i were on the move due east; everything works fine in my iPhone 4 though (apart from the incorrect placement of the picture). Any ideas?

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

3) I've been thinking about using Annotations instead, but i've heard they're static (and i really need them dynamic). Is this true?

谢谢!

推荐答案

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

When adding a subview, its frame origin is relative to that of its superview. In this case you derived an origin from the map view and then added the image to the view controller's view. You would need to convert your image's origin to the self.view's coordinate system like this:

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的坐标空间:

NB: this is untested code, but the principal is that you need to convert between the mapView's coordinate space (which is what you got from [self.mapView convertCoordinate:coord toPointToView:self.mapView]) to self.view's coordinate space. This example is a little contrived to be illustrative since an even simpler approach would be to use self.view's coordinate space in the first place:

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

这篇关于无法将MapKit用户坐标转换为屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆