从iphone上的mkmapview获取一个点的坐标 [英] Get the coordinates of a point from mkmapview on iphone

查看:136
本文介绍了从iphone上的mkmapview获取一个点的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何根据用户触摸的位置在地图上添加注释。

I'm trying to figure out how to put an annotation on a map based on where the user touches.

我尝试对 MKMapView 进行子类化,并查找 touchesBegan 开火但事实证明, MKMapView 不使用标准的 touches 方法。

I have tried sub-classing the MKMapView and looked for the touchesBegan to fire but as it turns out, MKMapView does not use the standard touches methods.

我也尝试过分类 UIView ,在孩提时加上 MKMapView 然后听HitTest和 touchesBegan 。这有点工作。
如果我的地图是 UIView 的完整尺寸,那么就有这样的东西

I also have tried sub-classing a UIView, adding an MKMapView as a child and then listening for HitTest and touchesBegan. This works somewhat. if i have my map the full size of the UIView, then have something like this

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return map;
}

这是有效的,我的 touchesBegan 将能够获得点数

and that works, my touchesBegan will be able to get the point using

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  for (UITouch *touch in touches){
  CGPoint pt = [touch  locationInView:map];
  CLLocationCoordinate2D coord= [map convertPoint:pt toCoordinateFromView:map];
  NSLog([NSString stringWithFormat:@"x=%f y=%f - lat=%f long = %f",pt.x,pt.y,coord.latitude,coord.longitude]);
 }
}

但是地图有一些疯狂的行为,就像它会不滚动,除非双击,否则它不会放大,但你可以缩小。它只有在我将地图作为视图返回时才有效。如果我没有命中测试方法,地图工作正常,但显然没有得到任何数据。

but then the map has some crazy behavior like it will not scroll, and it will not zoom in unless double tapped but you can zoom out. and it only works if I return the map as the view. If I do not have the hit test method, the map works fine but obviously doesn't get any data.

我是否会让坐标错误?请告诉我有更好的方法。我知道如何添加注释就好了,我找不到任何在用户触摸地图时添加注释的示例。

Am I going about getting the coordinate wrong? Please tell me there is a better way. I know how to add annotations just fine, I just cannot find any examples of adding an annotation where and when a user touches the map.

推荐答案

所以我找到了一种方法,最后。如果我创建一个视图并使用相同的帧添加一个地图对象。然后在该视图上监听命中测试,我可以在发送的触摸点上调用convertPoint:toCoordinateFromView:并给它像这样的地图:

so i found a way to do it, finally. If i create a view and add a map object to it with the same frame. then listen for hit test on that view, i can call convertPoint:toCoordinateFromView: on the touch point sent, and give it the map like so:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    CLLocationCoordinate2D coord= [map convertPoint:point toCoordinateFromView:map];
    NSLog(@"lat  %f",coord.latitude);
    NSLog(@"long %f",coord.longitude);

    ... add annotation ...

    return [super hitTest:point withEvent:event];
}

这是非常粗糙的当你滚动地图它仍然不断调用命中测试所以你需要处理它,但它开始从触摸地图获取gps坐标。

this is pretty rough as is and as you scroll the map it still constantly calls hit test so you will need to handle that, but its a start at getting the gps coordinates from touching a map.

这篇关于从iphone上的mkmapview获取一个点的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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