【问题标题】:Get the coordinates of a point from mkmapview on iphone从 iPhone 上的 mkmapview 获取点的坐标
【发布时间】:2010-06-20 17:23:57
【问题描述】:

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

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

我还尝试对UIView 进行子类化,将MKMapView 添加为子类,然后收听HitTest 和touchesBegan。这有点工作。 如果我的地图是UIView 的全尺寸,那么就有这样的东西

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

这行得通,我的touchesBegan 将能够使用

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

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

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

【问题讨论】:

    标签: iphone mkmapview touchesbegan


    【解决方案1】:

    你可以试试这个代码

    - (void)viewDidLoad
    {
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
    
        tapRecognizer.numberOfTapsRequired = 1;
    
        tapRecognizer.numberOfTouchesRequired = 1;
    
        [self.myMapView addGestureRecognizer:tapRecognizer];
    }
    
    
    -(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
    {
        CGPoint point = [recognizer locationInView:self.myMapView];  
    
        CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
    
        MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
    
        point1.coordinate = tapPoint;
    
        [self.myMapView addAnnotation:point1];
    }
    

    一切顺利。

    【讨论】:

      【解决方案2】:

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

      - (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 坐标的开始。

      【讨论】:

        【解决方案3】:

        有点死线挖掘,但由于这是谷歌的顶级结果,它可能是值得的:

        您可以使用长按手势识别器来获取坐标并在地图上放置图钉。一切都在freshmob进行解释

        【讨论】:

          【解决方案4】:

          斯威夫特 4.2

          func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          
          for touch in touches {
              let touchPoint = touch.location(in: mapView)
              let location = mapView.convert(touchPoint, toCoordinateFrom: mapView)
              print ("\(location.latitude), \(location.longitude)")
          }}
          

          【讨论】:

            【解决方案5】:

            斯威夫特 2.2

            func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
                let point = gestureRecognizer.locationInView(mapView)
                let tapPoint = mapView.convertPoint(point, toCoordinateFromView: view)
                coordinateLabel.text = "\(tapPoint.latitude),\(tapPoint.longitude)"
            
                return true
            }
            

            【讨论】:

              猜你喜欢
              • 2011-04-26
              • 2012-10-31
              • 1970-01-01
              • 1970-01-01
              • 2011-02-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多