从 iPhone 上的 mkmapview 获取点的坐标
我试图弄清楚如何根据用户触摸的位置在地图上添加注释。
我尝试对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以试试这个代码
一切顺利。
You can try this code
All the best.
所以我终于找到了一种方法。如果我创建一个视图并使用同一帧向其中添加一个地图对象。然后监听该视图上的命中测试,我可以在发送的触摸点上调用convertPoint:toCooperativeFromView:,并像这样给它地图:
这非常粗糙,当你滚动地图时,它仍然不断地调用命中测试,所以你需要处理这个问题,但它是从触摸地图获取 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:
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.
有点死线程挖掘,但因为这是谷歌的最高结果,所以可能是值得的:
您可以使用点击并按住手势识别器来获取坐标并在地图上放置图钉。一切都在 进行了解释新鲜生物
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
斯威夫特 4.2
Swift 4.2
斯威夫特2.2
Swift 2.2