【问题标题】:Can't Get Coordinates From MKMapView Using UILongTouchGestureRecognizer无法使用 UILongTouchGestureRecognizer 从 MKMapView 获取坐标
【发布时间】:2012-10-31 11:02:03
【问题描述】:

我使用的是 iOS 6,并且我已经阅读了许多方法来完成此操作(包括许多来自堆栈溢出的方法),但均未成功。

这是我尝试过的,分为“阶段”:

  1. 创建一个UILongTouchGestureRecognizer 以接收对MKMapView 的长时间触摸。

    • 我尝试通过我的 Storyboard 添加 UILongTouchGestureRecognizer,并通过 Connections Inspector 连接出口、代表等。
    • 我尝试以编程方式创建UILongTouchGestureRecognizer,使用mapView 作为目标、self 作为目标和self.view 作为目标对其进行初始化。
  2. 使用选择器方法从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。
  3. 结果

    • 简而言之,这是我在使用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


    【解决方案1】:

    回顾我在 iOS 6 中运行的一个旧项目,我们似乎拥有几乎相同的代码(我猜是相同的源教程)。不同之处在于我正在检查state 并且我的mapView 变量没有在viewDidLoad 中重置。您似乎在手势识别器之后分配了一个新的,并且您没有显示任何将其添加到 viewController 的代码。我猜您已经设法将屏幕上的内容与代码中的内容分开。如果您的 MKMapview 打算在视图控制器的整个生命周期中显示在屏幕上,则让 IB 管理分配和分配。

    - (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];       
        }
    
    }
    

    【讨论】:

    • 介意发布其余代码吗?现在我刚刚得到 0.000000 的纬度和经度。感谢您的帮助!
    • 没有其他代码。我在 IB 中附加了手势识别器。
    • 在创建坐标后使用此代码会得到什么?:NSLog(@"Lat: %f Lng: %f", locCoord.latitude, locCoord.longitude); 另外,您能否验证您的连接是如何在 IB 中设置的?我的 UILongPressGestureRecognizer 设置如下:Delegate->mapView Sent Actions:handleLongPress: Referencing Outlets:gestureRecognizers->mapView。你的也一样吗?
    • 我犯了愚蠢的错误。使用@property MKMapView *mapView 并且没有使用 IBOutlet 从 IB 连接我的 mapView。一旦我这样做了,一切都很好。 ...最简单的答案通常是最好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多