Created
January 2, 2017 18:32
-
-
Save robertmryan/5f55d06adf34d850cdab757a6ea1ff85 to your computer and use it in GitHub Desktop.
Related to http://stackoverflow.com/q/41430255/1271826
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomAnnotation: MKPointAnnotation { | |
| var color = UIColor.red | |
| var size = CGSize(width: 20, height: 20) | |
| } | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var mapView: MKMapView! | |
| @IBAction func didTapButton(_ sender: UIButton) { | |
| let annotation = CustomAnnotation() | |
| annotation.coordinate = mapView.centerCoordinate | |
| switch arc4random_uniform(14) { | |
| case 0: annotation.color = .black | |
| case 1: annotation.color = .blue | |
| case 2: annotation.color = .brown | |
| case 3: annotation.color = .cyan | |
| case 4: annotation.color = .darkGray | |
| case 5: annotation.color = .gray | |
| case 6: annotation.color = .green | |
| case 7: annotation.color = .lightGray | |
| case 8: annotation.color = .magenta | |
| case 9: annotation.color = .orange | |
| case 10: annotation.color = .purple | |
| case 11: annotation.color = .red | |
| case 12: annotation.color = .white | |
| case 13: annotation.color = .yellow | |
| default: annotation.color = .black | |
| } | |
| let side = Int(arc4random_uniform(80) + 40) | |
| annotation.size = CGSize(width: side, height: side) | |
| mapView.addAnnotation(annotation) | |
| } | |
| } | |
| extension ViewController: MKMapViewDelegate { | |
| func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
| if annotation is MKUserLocation { return nil } | |
| guard let customAnnotation = annotation as? CustomAnnotation else { fatalError("should have been CustomAnnotation") } | |
| let identifier = "myid" | |
| var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) | |
| if annotationView == nil { | |
| annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) | |
| } else { | |
| annotationView?.annotation = annotation | |
| } | |
| annotationView?.image = customImage(customAnnotation.color, size: customAnnotation.size) | |
| return annotationView | |
| } | |
| func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { | |
| print("didSelect") | |
| DispatchQueue.main.async { | |
| mapView.deselectAnnotation(view.annotation, animated: false) | |
| } | |
| } | |
| func customImage(_ color: UIColor, size: CGSize, scale: CGFloat = 0) -> UIImage { | |
| UIGraphicsBeginImageContextWithOptions(size, false, scale) | |
| color.setFill() | |
| UIBezierPath(ovalIn: CGRect(origin: .zero, size: size)).fill() | |
| let image = UIGraphicsGetImageFromCurrentImageContext()! | |
| UIGraphicsEndImageContext() | |
| return image | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment