无法使用UILongTouchGestureRecognizer从MKMapView获取坐标 [英] Can't Get Coordinates From MKMapView Using UILongTouchGestureRecognizer
问题描述
我正在使用iOS 6,我已经阅读了很多方法来实现这一目标(包括许多来自堆栈溢出)但没有成功。
I'm using iOS 6, and I've read dozens of ways to accomplish this (including many from stack overflow) without success.
这就是我的意思尝试过,分成阶段:
Here's what I've tried, separated into "stages":
-
创建
UILongTouchGestureRecognizer
接收MKMapView
的长触摸。
- 我试过添加
UILongTouchGestureRecognizer
通过我的故事板,并通过Connections Inspector连接出口,代表等。 - 我试过创建
UILongTouchGestureRecognizer
以编程方式,使用mapView
作为目标初始化它,self
as目标,以及self.view
作为目标。
- I've tried adding a
UILongTouchGestureRecognizer
via my Storyboard, and hooking up the outlets, delegates, etc. through the Connections Inspector. - I've tried creating the
UILongTouchGestureRecognizer
programmatically, initializing it usingmapView
as target,self
as target, andself.view
as target.
收到使用 UILongTouchGestureRecognizer
中的选择器方法触摸手势,获取 CGPoint
,并将其转换为 CLLocationCoordinate2D
object。
Receive the touch gesture using the selector method from the UILongTouchGestureRecognizer
, get the CGPoint
, and convert it to a CLLocationCoordinate2D
object.
- 我试过:
- 使用[mapView convertPoint:(CGPoint)toCoordinateFromView:self.mapView];
- 首先使用
MKMapPoint aPoint = MKMapPointMake(aCGPoint.x,aCGPoint.y);
,然后使用MKCoordinateForMapPoint(aMapPoint)
获取CLLocationCoordinate2D
。 - 两者都直接访问UILongPressGestureRecognizer并使用
(UILongPressGestureRecognizer *)发件人来自方法调用的
获取CGRect的x,y。
- I've tried:
- Using [mapView convertPoint:(CGPoint) toCoordinateFromView:self.mapView];
- Using
MKMapPoint aPoint = MKMapPointMake(aCGPoint.x, aCGPoint.y);
first, and then usingMKCoordinateForMapPoint(aMapPoint)
to get theCLLocationCoordinate2D
. - Both Directly accessing the UILongPressGestureRecognizer and using the
(UILongPressGestureRecognizer *)sender
from the method call to get the x,y for the CGRect.
结果
- 简而言之,这是我在使用<$检查值时得到的结果c $ c> NSLog ,无论使用何种方法。
- 对于从UILongPressGestureRecognizer
给出的X,Y,x似乎波动到200和420之间的某个范围.Y范围从~400 - 700 。
- 长时间触摸和触发的纬度和经度。奇怪的是,打印到日志中的小数点仅在85.051XX(纬度)和-179.9997XX(经度)之后的第3到第6个十进制值有所不同。
- In a nutshell here is what I've been getting when checking the values using
NSLog
, irregardless of the methods used. - For the X,Y given from theUILongPressGestureRecognizer
, x seems to fluctuate to somewhere in the range of 200, and 420. Y ranges from ~400 - 700. - The latitudes and longitudes triggered by a long touch & printed to the log, strangely, only vary in the 3rd - 6th decimal values after the decimal point of 85.051XX (latitude) and -179.9997XX (longitude).
以下是我试过的一些代码示例
Here's examples of some of the code I've tried
- (void)viewDidLoad { NSLog(@"View did load"); [super viewDidLoad]; UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:mapView action:@selector(handleLongPress:)]; mapView = [[MKMapView alloc] init]; } - (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender { NSLog(@"CGPoint point: x - %f y - %f", point.x, point.y); CGPoint point = [sender locationInView:self.mapView]; CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; NSLog(@"Coords from \"locCoord\": lat - %f lng - %f", locCoord.latitude, locCoord.lon); MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init]; [addAnnotation setCoordinate: [self.mapView addAnnotation:addAnnotation]; }
希望有人可以帮助我,因为我完全被这个问题困住了。
Hope someone can help me, because I'm completely stumped on this one.
重申一下,最终目标是得到一个坐标,用户长按
a mapView,然后(别的我不能开始工作)在那个位置放一个别针。Just to reiterate, the end goal is to get a coordinate of where the user "long pressed a mapView", and then (something else I can't get to work) drop a pin at that location.
推荐答案
回顾我在iOS 6中工作的旧项目似乎有几乎相同的代码(我猜相同的源教程)。区别在于我正在检查
状态
并且我的mapView变量没有在viewDidLoad中重置。您似乎在手势识别器之后分配了一个新的,并且您没有显示任何将其添加到viewController的代码。我猜你已经设法将屏幕上的那个与代码中的一个分开。如果您的MKMapview要在viewcontroller的生命周期中显示在屏幕上,请让IB管理分配和分配。Looking back at an old project of mine that works in iOS 6 we seem to have almost identical code (same source tutorial I guess). The difference being that I am checking the
state
and that my mapView variable is not being reset in the viewDidLoad. You seem to be allocating a new one after the gesture recogniser and you haven't shown any code for adding it to your viewController. I'd guess that you've managed to separate the one on screen from the one in code. If your MKMapview is meant to be on screen through the life of the viewcontroller, let IB manage the allocation and assignment.- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { CGPoint point = [recognizer locationInView:mapView]; CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; [self addPinAtLocation:locCoord]; } }
这篇关于无法使用UILongTouchGestureRecognizer从MKMapView获取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!