我构建了一个具有拆分视图的应用程序:一张桌子和一张地图。我想根据动物的类型更改标记图像。我在下面添加了代码,但标记未更改。
LocationListController.swift
override func viewDidLoad() {
super.viewDidLoad()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Animal")
do{
animalList = try managedObjectContext.fetch(fetchRequest) as! [Animal]
}
catch {
fatalError("Failded to fetch teams: \(error)")
}
let location: FencedAnnotation = FencedAnnotation (newTitle: "*****", newSubtitle: "*****", lat: -33, long: 145.045374, newType: "Start", newPhoto: "none")
filteredList = animalList
for animal in filteredList{
let annotation = FencedAnnotation(newTitle: animal.name!, newSubtitle: animal.animaldescription!, lat: animal.latitude, long: animal.longtitude, newType: animal.type!, newPhoto: animal.photo!)
self.locationList.add(annotation)
self.mapViewController?.addAnnotation(annotation: annotation)
}
geoLocation = CLCircularRegion(center: location.coordinate, radius: 500, identifier: location.title!)
geoLocation!.notifyOnExit = true
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.stopMonitoring(for: geoLocation!)
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Animal"
navigationItem.searchController = searchController
tableView.reloadData()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
mapView.swift
func addAnnotation(annotation: FencedAnnotation) {
self.mapView.addAnnotation(annotation)
}
func removeAnnotation(annotation: FencedAnnotation) {
self.mapView.removeAnnotation(annotation)
}
func focusOn(annotation: MKAnnotation) {
self.mapView.centerCoordinate = annotation.coordinate
self.mapView.selectAnnotation(annotation, animated:true)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "pin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
let customPointAnnotation = annotation as! FencedAnnotation
if customPointAnnotation.type == "cat"{
annotationView?.image = #imageLiteral(resourceName: "cat")
}
if customPointAnnotation.type == "dog"{
annotationView?.image = #imageLiteral(resourceName: "dog")
}
if customPointAnnotation.type == "fish"{
annotationView?.image = #imageLiteral(resourceName: "fish")
}
if customPointAnnotation.type == "monkey"{
annotationView?.image = #imageLiteral(resourceName: "monkey")
}
if customPointAnnotation.type == "fish"{
annotationView?.image = #imageLiteral(resourceName: "bird")
}
if customPointAnnotation.type == "fish"{
annotationView?.image = #imageLiteral(resourceName: "mouse")
}
else{
annotationView?.image = #imageLiteral(resourceName: "placeholder")
}
annotationView?.canShowCallout = true
return annotationView
}
我从核心数据中获取列表,并使用客户的MKAnnotation添加注释。