ios - 为 MKAnnotation 添加引脚颜色

标签 ios objective-c mkannotation

我在 map 上有一个按钮,它重定向到“我的位置”,它是核心经纬度。我想改变别针颜色。尝试了一些但没有成功。这是代码。

- (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的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 - 为 MKAnnotation 添加引脚颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545422/

相关文章:

ios - 我有一个 TableView 和五个数据源

objective-c - 作为 C 程序员使用闭包(或 block )进行设计的方法

objective-c - iOS 中的键盘快捷键?

ios - 注释、注释、注释

iphone - NSData/UIImage 到字符串

ios - 后退后 UISearchBar 错误

iphone - Facebook 中的智能应用横幅

ios - Azure 通知中心不适用于 ios

iphone - 找不到协议(protocol)声明 MKAnnotation

objective-c - 如何在 MKMapView 中的 MKOverlay 上方显示 MKAnnotation