我正在製作一個在地圖上添加一個圖釘的應用程序,到目前爲止,它已經在用戶進行長按操作的位置添加了圖釘,但是我希望將其更改並添加到用戶位置,我不知道要做什麼改變,你能糾正我的代碼,使其工作? 謝謝你,我加了我下面的代碼:如何使用UILongPressGestureRecognizer在用戶位置添加PIN?
var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
uilpgr.minimumPressDuration = 2.0
Map.addGestureRecognizer(uilpgr)
}
func action(gestureRecognizer:UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.Began {
var touchPoint = gestureRecognizer.locationInView(self.Map)
var newCoordinate = self.Map.convertPoint(touchPoint, toCoordinateFromView: self.Map)
var location = CLLocation(latitude: newCoordinate.latitude , longitude: newCoordinate.longitude)
這是我使用的用戶位置代碼:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var coordinate = CLLocationCoordinate2DMake(latitude, longitude)
var latDelta:CLLocationDegrees = 0.01
var lonDelta:CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
self.Map.setRegion(region, animated: true)
}
而不是在您的action()函數中添加觸摸的位置,只需更改它添加用戶位置的位置,如果它可通過self.Map.userLocation –
感謝您的回答@JoshHamet您的意思是這樣嗎? var touchPoint = gestureRecognizer.self.Map.userLocation,如果是這樣,它說UILongPressGestureRecognizer沒有一個名爲「Map」的成員 –