无法在地图中显示注释点的颜色,如何在这里解决我的代码。我在MKPointAnnotation中声明了颜色的功能,即使我无法改变颜色,是否有我错过了任何密码?
class NearbyViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet weak var mapview: MKMapView!
@IBOutlet weak var name: UILabel!
var locationManager : CLLocationManager!
@IBOutlet weak var menu: UIBarButtonItem!
var schoolMap : [schools] = []
var collegeMap : [Colleges] = []
var universityMap : [University] = []
class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor?
}
override func viewDidLoad() {
super.viewDidLoad()
getschoolMapJson()
getcollegeMapJson()
getuniversityMapJson()
menu.target = self.revealViewController()
menu.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 1.00
locationManager.startUpdatingLocation()
}
override func viewWillAppear(_ animated: Bool) {
mapview.reloadInputViews()
}
override func viewDidAppear(_ animated: Bool) {
mapview.reloadInputViews()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span = MKCoordinateSpanMake(0.05, 0.05)
let mylocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region = MKCoordinateRegionMake(mylocation, span)
mapview.setRegion(region, animated: true)
self.mapview.showsUserLocation = true
}
func getschoolMapJson(){
if (schoolMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/schools")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
/*for i in 0..<self.myresponse.count{
let schools_data = schools(schoolJson:self.myresponse[i])
self.schooldata.append(schools_data)
}*/
for school in myresponse.array!{
let schoolsObj = schools(schoolJson: school)
//Add Pin
let location = CLLocationCoordinate2DMake((schoolsObj.latitude) , (schoolsObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = schoolsObj.name
let pincolor = MyPointAnnotation()
pincolor.pinTintColor = .blue
// MKPinAnnotationColor.green
// let pinC = MKPinAnnotationView.greenPinColor()
// self.mapview.addAnnotation(pinC as! MKAnnotation)
self.mapview.addAnnotation(annotation)
self.schoolMap.append(schoolsObj)
}
// self.mapview.reloadInputViews()
case.failure(let error):
print("Not Success",error)
}
}
}
func getcollegeMapJson(){
if (collegeMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/colleges")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
/*for i in 0..<self.myresponse.count{
let schools_data = schools(schoolJson:self.myresponse[i])
self.schooldata.append(schools_data)
}*/
for college in myresponse.array!{
let collegeObj = Colleges(collegeJson: college)
//Add Pin
let location = CLLocationCoordinate2DMake((collegeObj.latitude) , (collegeObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = collegeObj.name
self.mapview.addAnnotation(annotation)
self.collegeMap.append(collegeObj)
}
case.failure(let error):
print("Not Success",error)
}
}
}
func getuniversityMapJson(){
if (universityMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/universities")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
for university in myresponse.array!{
let universityObj = University(universityJson: university)
//Add Pin
let location = CLLocationCoordinate2DMake((universityObj.latitude) , (universityObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = universityObj.name
self.mapview.addAnnotation(annotation)
self.universityMap.append(universityObj)
}
case.failure(let error):
print("Not Success",error)
}
}
}
}
我很困惑地显示我在很多方面尝试过的注释点颜色 但是不能这样做。
答案 0 :(得分:1)
您必须添加以下功能。我希望这能解决你的问题。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")
} else {
annotationView?.annotation = annotation
}
if let annotation = annotation as? MyPointAnnotation {
annotationView?.pinTintColor = annotation.pinTintColor
}
return annotationView
}