代码之家  ›  专栏  ›  技术社区  ›  AtomRiot

从iPhone上的mkmapview获取点的坐标

  •  15
  • AtomRiot  · 技术社区  · 14 年前

    我想知道如何根据用户接触的位置在地图上放置注释。

    我试过将 MKMapView 并寻找 touchesBegan 但事实证明, MKMMAVIEW 不使用标准 触摸 方法。

    我也试过把A分班 UIView 添加一个 MKMMAVIEW 当我还是个孩子的时候,然后听hittest和 触摸开始 . 这有点管用。 如果我有地图的话 UIVIEW ,然后吃点像这样的东西

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

    那就行了,我的 触摸开始 可以用

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

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

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

    4 回复  |  直到 8 年前
        1
  •  42
  •   Jesse    11 年前

    你可以试试这个代码

    - (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
  •  8
  •   AtomRiot    14 年前

    所以我终于找到了一个办法。如果我创建一个视图并添加一个具有相同帧的地图对象。然后,在该视图上监听点击测试,我可以调用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
  •  3
  •   Erken    12 年前

    有点死气沉沉,但由于这是谷歌最好的结果,它可能值得:

    您可以使用轻敲和按住手势识别器来获取坐标并在地图上放置一个销。一切都解释在 freshmob

        4
  •  1
  •   Ramis    8 年前

    斯威夫特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
    }