how to change pin color in mapkit under the same annotationview (swift3)
Subclass MKPointAnnotation
to add any custom property that you want, such as a pinTintColor
:
class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor?
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var jmap: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
jmap.delegate = self
let hello = MyPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40, longitude: -73)
hello.pinTintColor = .red
let hellox = MyPointAnnotation()
hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72)
hellox.pinTintColor = .blue
jmap.addAnnotation(hello)
jmap.addAnnotation(hellox)
}
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
}
}
Changing pin color MKMapView
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
annView.pinColor = MKPinAnnotationColorGreen;
return annView;
}
How to change selected pin image in Map Kit in Swift 3?
"selected" means "tapped"? If so, Try the following code:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
view.image = UIImage(named: "marker_gray")
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.image = UIImage(named: "marker_red")
}
Related Topics
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture
Programmatically Obtaining the Imei or Udid of an iOS Device
How to Convert Base64 into Nsdata in Swift
iOS How to Restart App for Changing Language Swift 4
Swiftui Animation and Subsequent Reverse Animation to Original State
Multiple Image Pickers in One Controller
Why Do I Get an Mkerrordomain Error When Doing a Local Search
How to Create an Alert in a Swift File Model That Works for Various View Controller
Calculate New Coordinates with Starting Position and Distance
My Reachability Notifier Is Only Able to Be Called Once
Scene Kit Memory Management Using Swift
Swift: Terminating with Uncaught Exception of Type Nsexception