我在“地图”上有一个按钮,可将其重定向到“我的位置”其核心位置。我想更改图钉颜色。尝试了一些但没有成功。这是代码。

- (IBAction)btnMylLocation:(UIButton *)sender {

CLLocationCoordinate2D coord = {.latitude =  18.520430, .longitude =  73.856744};
MKCoordinateSpan span = {.latitudeDelta =  0.2, .longitudeDelta =  0.2};
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 18.520430;
annotationCoord.longitude = 73.856744;
self.lblLongitude.text = [NSString stringWithFormat:@"%f ", annotationCoord.latitude];
self.lblLatitude.text = [NSString stringWithFormat:@" %f", annotationCoord.longitude];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = @"Mindbowser";
annotationPoint.subtitle = @"Pune Headquater's";
[_mapView addAnnotation:annotationPoint];
}

最佳答案

您需要实现map's delegate方法并设置delegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If it's the user location, just return nil.
  if ([annotation isKindOfClass:[MKUserLocation class]])
  return nil;

  // Handle any custom annotations.
  if ([annotation isKindOfClass:[MKPointAnnotation class]])
  {

  MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
  if (!pinView)
  {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
    pinView.canShowCallout = YES;
    pinView.pinColor = MKPinAnnotationColorGreen;
  }
  else {
    pinView.annotation = annotation;
  }

  return pinView;

  }

  return nil;
}


在您的viewDidLoad()中,编写以下代码

_mapView.delegate = self;

关于ios - 将图钉颜色添加到MKA注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34545422/

10-13 01:56