2016-04-14 7 views
0

touchesBegan関数とtapGestureを使用してマップ上の点の座標を取得しようとしていますが、取得した座標はマップ上の選択した点とは異なる点を表します。助けてください?地図上の場所を選択すると間違った座標が返されます

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     let tapGesture = UITapGestureRecognizer(target: self, action: nil) 
     let touchPoint = tapGesture.locationInView(self.myMap) 
     let location = self.myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap) 

     let mySpan:MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5) 
     let region:MKCoordinateRegion = MKCoordinateRegionMake(location, mySpan) 
     self.myMap.setRegion(region, animated: true) 

     lblLat.text = String(location.latitude) 
     lblLng.text = String(location.longitude) 

    } 

答えて

0

これであなたのコードを置き換えます、あなたのviewDidLoad

func tapGestureOnMap(gestureRecognizer: UITapGestureRecognizer) { 
     let touchLocation = gestureRecognizer.locationInView(mapView) 
     let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView) 
     print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)") 
} 

をそのように、あなたのMapViewにtapGestureを追加します。

let tapGestureOnMap = UITapGestureRecognizer(target: self, action: "tapGestureOnMap:") 
mapView.addGestureRecognizer(tapGestureOnMap) 
+0

親愛なるkhuong291、どうもありがとうございました。出来た。 – Simon

+0

正常に動作しますか? – Khuong

+0

それはうまくいった。ありがとうございました。 – Simon

関連する問題