我想知道如何根據用戶觸摸的位置在地圖上添加註釋。從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]);
}
}
獲得分數,但隨後的地圖有一些瘋狂的行爲,就像它不會滾動一樣,除非雙擊,否則它不會放大,但可以縮小。並且只有當我將地圖作爲視圖返回時纔有效。如果我沒有命中測試方法,地圖工作正常,但顯然沒有得到任何數據。
我是不是要弄錯座標?請告訴我有更好的方法。我知道如何添加註釋就好,我找不到任何添加註釋的例子,當用戶觸摸地圖的時候和地點。
斯威夫特版本:http://stackoverflow.com/a/37860496/1151916 – Ramis 2016-06-16 13:19:46