2016-04-14 30 views
0

Haritadaki bir noktanın koordinatlarını touchesBegan işlevini ve tapGesture'ı kullanarak bulmaya çalışıyorum ancak aldığım koordinatlar, haritada seçili olandan farklı bir noktayı temsil ediyor. Herhangi bir yardım lütfen?Bir harita üzerinde bir konumun seçilmesi yanlış koordinatlar veriyor

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) 

    } 

cevap

0

bununla kodunuzu değiştirin:

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)") 
} 

senin viewDidLoad yılında, yani gibi, MapView bir tapGesture ekleyin:

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

Değerli khuong291, çok teşekkür ederim. İşe yaradı. – Simon

+0

İyi çalışıyor mu? – Khuong

+0

İyi çalıştı. Teşekkür ederim. – Simon

İlgili konular