gpt4 book ai didi

iphone - 从 iPhone 上的 mkmapview 获取点的坐标

转载 作者:行者123 更新时间:2023-12-03 18:21:10 25 4
gpt4 key购买 nike

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

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

我还尝试对 UIView 进行子类化,添加 MKMapView 作为子项,然后监听 HitTest 和 touchesBegan。这有点作用。如果我的 map 是 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]);
}
}

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

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

最佳答案

您可以尝试此代码

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

祝一切顺利。

关于iphone - 从 iPhone 上的 mkmapview 获取点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3080198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com