Changing pin color MKMapView

与世无争的帅哥 提交于 2019-12-01 03:51:56
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
  {
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annView.pinColor = MKPinAnnotationColorGreen;
    return annView;
  }

The pinColor property is defined in the MKPinAnnotationView class (not the MKAnnotation protocol).

You create a MKPinAnnotationView in the viewForAnnotation delegate method. If you haven't implemented that delegate, you get standard red pins by default.

In that delegate method, you create an instance of MKPinAnnotationView and you can set its pinColor to green.

Swift3 is in this way:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

    if annotation.title! == "My Place" {

        annotationView.pinTintColor = UIColor.green

    } else {

        annotationView.pinTintColor = UIColor.red
    }


    return annotationView
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!