【发布时间】:2012-10-31 11:02:03
【问题描述】:
我使用的是 iOS 6,并且我已经阅读了许多方法来完成此操作(包括许多来自堆栈溢出的方法),但均未成功。
这是我尝试过的,分为“阶段”:
-
创建一个
UILongTouchGestureRecognizer以接收对MKMapView的长时间触摸。- 我尝试通过我的 Storyboard 添加
UILongTouchGestureRecognizer,并通过 Connections Inspector 连接出口、代表等。 - 我尝试以编程方式创建
UILongTouchGestureRecognizer,使用mapView作为目标、self作为目标和self.view作为目标对其进行初始化。
- 我尝试通过我的 Storyboard 添加
-
使用选择器方法从
UILongTouchGestureRecognizer接收触摸手势,获取CGPoint,并将其转换为CLLocationCoordinate2D对象。- 我试过了:
- 使用 [mapView convertPoint:(CGPoint) toCoordinateFromView:self.mapView];
- 先使用
MKMapPoint aPoint = MKMapPointMake(aCGPoint.x, aCGPoint.y);,然后使用MKCoordinateForMapPoint(aMapPoint)获取CLLocationCoordinate2D。 - 直接访问 UILongPressGestureRecognizer 和使用方法调用中的
(UILongPressGestureRecognizer *)sender来获取 CGRect 的 x,y。
- 我试过了:
-
结果
- 简而言之,这是我在使用
NSLog检查值时得到的结果,无论使用何种方法。 - 对于UILongPressGestureRecognizer给出的 X、Y,x 似乎在 200 和 420 的范围内波动。Y 的范围从 ~400 - 700。 - 长按触发并打印到日志中的纬度和经度,奇怪的是,仅在 85.051XX(纬度)和 -179.9997XX(经度)的小数点后的第 3-6 位小数中变化。
- 简而言之,这是我在使用
这是我尝试过的一些代码示例
- (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];
}
希望有人可以帮助我,因为我完全被这个难住了。
重申一下,最终目标是获取用户“长按”位置的坐标 一个 mapView”,然后(我无法工作的其他东西)在该位置放置一个图钉。
【问题讨论】:
标签: iphone ios ios6 mkmapview uigesturerecognizer