3

这就是我现在还没有工作的东西。注意颜色的注释行。我正在尝试使 Olso 引脚成为标准红色。但我想改变伦敦别针的颜色,使它不是红色。所以 2 个不同的引脚会有不同的颜色。

视图控制器

 import MapKit
 import UIKit

class ViewController: UIViewController, MKMapViewDelegate {
  @IBOutlet weak var mapView: MKMapView!
 //color
let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor())

 override func viewDidLoad() {
super.viewDidLoad()

let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")



 mapView.addAnnotations([london, oslo])

 }


  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
   // 1
  let identifier = "Capital"

// 2
if annotation is Capital {


    // 3

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
        //4
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
        //color
          let colorPointAnnotation = annotation as! ColorPointAnnotation
        annotationView?.pinTintColor = colorPointAnnotation.pinColor

        // 5
        let btn = UIButton(type: .detailDisclosure)
        annotationView!.rightCalloutAccessoryView = btn
    } else {
        // 6
        annotationView!.annotation = annotation

    }

    return annotationView
}

// 7
return nil

    }

   func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let capital = view.annotation as! Capital
let placeName = capital.title
let placeInfo = capital.info


let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
 }
 }

首都

import MapKit
import UIKit

class Capital: NSObject, MKAnnotation {
var title: String?
var coordinate: CLLocationCoordinate2D
var info: String

init(title: String, coordinate: CLLocationCoordinate2D, info: String)           {
    self.title = title
    self.coordinate = coordinate
    self.info = info



  }
  }
4

0 回答 0