Bevor alles, was ich bin keine Standards zu erhalten und ich habe nur eine der Lösung für Ihr Problem und ich lerne swift und MapKit durch andere Probleme lösen.
Hier habe ich einige Beispiel-Code gegeben, und ich habe mapView und UITapGestureRecognizer durch die Objekt-Bibliothek hinzugefügt.
Für Annotation Hinzufügen wie von oben fallen
pinView!.animatesDrop = true
im viewForAnnotation Delegation method.And AddAnnotation Methode aufrufen, wird die Anmerkung, wenn Sie tippen auf der Karte hinzuzufügen.
Für
ich umgesetzt haben regionDidChangeAnimated Methode delegieren und entfernen Sie einfach und fügen Sie wieder alle Anmerkungen alle Annotation Animieren.
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
self.mapView.delegate = self
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
let locationData = [
//Walker Art Gallery
["name": "Walker Art Gallery",
"latitude": 12,
"longitude": 79],
//Liver Buildings
["name": "Liver Buildings",
"latitude": 13,
"longitude": 77],
//St George's Hall
["name": "St George's Hall",
"latitude": 14,
"longitude": 77]
]
var annotations = [MKPointAnnotation]()
for each in locationData {
let latitude = CLLocationDegrees(each["latitude"] as! Double)
let longitude = CLLocationDegrees(each["longitude"] as! Double)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let name = each["name"] as! String
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(name)"
annotations.append(annotation)
}
mapView.addAnnotations(annotations)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForAnnotation \(annotation.title)")
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
return pinView
}
@IBAction func addAnnotation(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
let location = sender.locationInView(mapView)
let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("regionDidChangeAnimated")
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
}
EDIT
Für Ihren Fall in der viewDidLoad Methode, um diesen
self.mapView.delegate = self
fügen Sie die MKAnnotation Für Anmerkungen versehen Sie die unten stehende Delegatmethode
//Annotation view
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
return pinView
}
implementieren Nach zur Kommentierung der Annotation n Sie müssen die Delegate-Methode implementieren.
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
Und ich gehe davon aus Sie wissen, wie Anmerkung auf der Karte tippen hinzuzufügen ...
EDIT2
Hier ist, was du bist mein Freund brauchen.
In der ViewDidLoad fügen Sie eine Benachrichtigung Beobachter wie folgt hinzu.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
Und schließlich ändern, dass regionDidChangeAnimated zu didBecomeActive wie unten
func didBecomeActive() {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
Sie müssen genauer sein in dem, was Sie für Fragen und stellen Sie sicher, dass Ihr Code richtig formatiert ist - anders es wird schwierig sein, dir zu helfen – AlBlue
@AIBlue Wo bist du mein Freund? –