如何将标题和自定义图像添加到 MKMapView 中的特定位置图钉?

How to add title and custom image to a specific location pin in MKMapView?

我正在为从 MKMapView 中的数据库获取的位置添加注释图钉。在所有位置中,其中一个位置是中心 location.A 图钉,指示中心位置将与其余位置图钉具有不同的图像。此外,中心位置大头针应在用户点击时显示标题。这是我正在尝试的代码:

  -(void)addPinWithTitle:(NSString *)lat longitude:(NSString*)longi
  {
  //get centerLocation
  NSString* centerLat=[GroupLocation getGroupLat];
  NSString* centerLon=[GroupLocation getGroupLon];

  MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
  double latitude =[lat doubleValue];
  double longitude =[longi doubleValue];
  CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  mapPin.coordinate = coordinate;

  if([lat isEqualToString:centerlat] && [longi isEqualToString:groupLon]){
  map.title=@"demoTitle";
  }
  [myMapView addAnnotation:mapPin];
  }

  - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
  {
  if ([annotation isKindOfClass:[MKUserLocation class]])
  return nil;
  static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
  MKAnnotationView *annotationView = [myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
  if(annotationView)
  return annotationView;
  else
  {
  MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
  reuseIdentifier:AnnotationIdentifier];
  annotationView.canShowCallout = YES;

  annotationView.image = [UIImage imageNamed:@"red_dotFromPaint.png"];

  UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  // [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
  [rightButton setTitle:annotation.title forState:UIControlStateNormal];
  annotationView.rightCalloutAccessoryView = rightButton;

  annotationView.draggable = YES;
  return annotationView;
  }
  return nil;
  }

使用上述方法,我已经在 mkmapview 中显示了从数据库中获取的所有位置,但现在我不知道如何在用户点击中心图钉时为特定位置图钉和标题显示不同的图像。 有人,请告诉如何实现这一目标。 先感谢您!

您需要更改纬度、经度、标题和副标题的示例代码。

ViewContrller.m

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "V3AnnotationView.h"

@interface ViewController () {
    IBOutlet MKMapView *myMapView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableArray *arrAnnotation = [[NSMutableArray alloc] init];

    V3AnnotationView *annotation = [[V3AnnotationView alloc] init];
    annotation.title = @"Ahmedabad";
    annotation.image = [UIImage imageNamed:@"icon1.png"];
    annotation.subtitle = @"City";
    annotation.coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714);
    [arrAnnotation addObject:annotation];

    V3AnnotationView *annotationAirport = [[V3AnnotationView alloc] init];
    annotationAirport.title = @"Airport";
    annotationAirport.image = [UIImage imageNamed:@"airport.png"];
    annotationAirport.subtitle = @"Airport";
    annotationAirport.coordinate = CLLocationCoordinate2DMake(23.0734, 72.6266);
    [arrAnnotation addObject:annotationAirport];

    V3AnnotationView *annotationCarParking = [[V3AnnotationView alloc] init];
    annotationCarParking.title = @"Car";
    annotationCarParking.image = [UIImage imageNamed:@"carparking.png"];
    annotationCarParking.subtitle = @"Free parking";
    annotationCarParking.coordinate = CLLocationCoordinate2DMake(23.0225, 72.6714);
    [arrAnnotation addObject:annotationCarParking];

    V3AnnotationView *annotationWifi = [[V3AnnotationView alloc] init];
    annotationWifi.title = @"Wifi";
    annotationWifi.image = [UIImage imageNamed:@"wifi-512.png"];
    annotationWifi.subtitle = @"Free wifi";
    annotationWifi.coordinate = CLLocationCoordinate2DMake(23.0225, 72.7714);
    [arrAnnotation addObject:annotationWifi];

    [myMapView addAnnotations:arrAnnotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[V3AnnotationView class]])
    {
        V3AnnotationView *myAnnotation = (V3AnnotationView *)annotation;
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            //pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.calloutOffset = CGPointMake(0, 4);

        } else {
            pinView.annotation = annotation;
        }
        pinView.image = myAnnotation.image;
        return pinView;
    }
    return nil;
}

您需要创建 class MKAnnotationView 并将以下代码复制到您的 class 文件中

V3AnnotationView.h

#import <MapKit/MapKit.h>

@interface V3AnnotationView : MKAnnotationView
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,retain) UIImage *image;
@end

V3AnnotationView.m

#import "V3AnnotationView.h"

@implementation V3AnnotationView
@synthesize title,subtitle,coordinate,image;