2016-11-24 115 views
1

我在故事板上有一個MapView,並且選中了User Location選項。我還寫了一些代碼在地圖上繪製另一個註釋。此註釋的座標(tappedCoordinates)源自已註冊的手勢。如何在當前位置,MapKit,Swift,iOS10上繪製引腳註釋

//add new annotation 
let annotation = MKPointAnnotation() 
annotation.coordinate = tappedCoordinates 
mapView.addAnnotation(annotation) 

//add circle radius 
let circle = MKCircle(center: tappedCoordinates, radius: 20) 
mapView.setRegion(MKCoordinateRegion(center: tappedCoordinates, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)), animated: true) 
mapView.add(circle) 

該代碼允許用戶在地圖上繪製註釋(pin)。它工作正常,除非用戶試圖在當前位置上繪製註釋。相反,MapView認爲您選擇了當前位置註釋,並顯示「當前位置」,而不是允許用戶繪製自定義註釋。

如何阻止用戶位置註釋可點擊,並允許將我的自定義註釋放在同一區域?我不想刪除當前位置註釋。

+0

你可以大概集userInteractionEnabled爲false,以防止觸摸在上面 – CZ54

回答

0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let locObject = locations.last 
    let annotation = MKPointAnnotation() 
    let centerCoordinate = locObject?.coordinate 
    annotation.coordinate = centerCoordinate! 
    annotation.title = "Title" 
    mapView.addAnnotation(annotation) 

}

0
class ViewController: UIViewController, MKMapViewDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Set map view delegate with controller 
    self.mapView.delegate = self 
} 
} 

覆蓋其委託方法:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
if (annotation is MKUserLocation) { 
    return nil 
} 

if (annotation.isKindOfClass(CustomAnnotation)) { 
    let customAnnotation = annotation as? CustomAnnotation 
    mapView.translatesAutoresizingMaskIntoConstraints = false 
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as MKAnnotationView! 

    if (annotationView == nil) { 
     annotationView = customAnnotation?.annotationView() 
    } else { 
     annotationView.annotation = annotation; 
    } 

    self.addBounceAnimationToView(annotationView) 
    return annotationView 
} else { 
    return nil 
} 
} 

//添加PIN(MKPointAnnotation)

override func viewDidLoad() { 
super.viewDidLoad() 

// Set map view delegate with controller 
self.mapView.delegate = self 

let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066) 
// Drop a pin 
let dropPin = MKPointAnnotation() 
dropPin.coordinate = newYorkLocation 
dropPin.title = "USA" 
mapView.addAnnotation(dropPin) 
}