从 iPhone 上的 mkmapview 获取点的坐标

发布于 2024-09-06 04:49:33 字数 1023 浏览 1 评论 0原文

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

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

我还尝试对 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]);
 }
}

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

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

I'm trying to figure out how to put an annotation on a map based on where the user touches.

I have tried sub-classing the MKMapView and looked for the touchesBegan to fire but as it turns out, MKMapView does not use the standard touches methods.

I also have tried sub-classing a UIView, adding an MKMapView as a child and then listening for HitTest and touchesBegan. This works somewhat.
if i have my map the full size of the UIView, then have something like this

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

and that works, my touchesBegan will be able to get the point using

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

but then the map has some crazy behavior like it will not scroll, and it will not zoom in unless double tapped but you can zoom out. and it only works if I return the map as the view. If I do not have the hit test method, the map works fine but obviously doesn't get any data.

Am I going about getting the coordinate wrong? Please tell me there is a better way. I know how to add annotations just fine, I just cannot find any examples of adding an annotation where and when a user touches the map.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往事风中埋 2024-09-13 04:49:33

你可以试试这个代码

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

一切顺利。

You can try this code

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

All the best.

江挽川 2024-09-13 04:49:33

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

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

so i found a way to do it, finally. If i create a view and add a map object to it with the same frame. then listen for hit test on that view, i can call convertPoint:toCoordinateFromView: on the touch point sent, and give it the map like so:

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

this is pretty rough as is and as you scroll the map it still constantly calls hit test so you will need to handle that, but its a start at getting the gps coordinates from touching a map.

是伱的 2024-09-13 04:49:33

有点死线程挖掘,但因为这是谷歌的最高结果,所以可能是值得的:

您可以使用点击并按住手势识别器来获取坐标并在地图上放置图钉。一切都在 进行了解释新鲜生物

A bit of dead thread digging but as this is the top result in Google it may be worth it:

You can use the tap and hold gesture recognizer in order to get the coordinates and drop a pin on the map. Everything is explained at freshmob

稳稳的幸福 2024-09-13 04:49:33

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

Swift 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)")
}}
何时共饮酒 2024-09-13 04:49:33

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

Swift 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文