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

时间:2017-01-10 17:16:35

标签: ios objective-c mkmapview mapkit mkannotation

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

  -(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中显示了从数据库中提取的所有位置,但现在我没有得到当用户点击该中心引脚时如何为特定位置引脚和标题显示不同的图像。 有人,请告诉我们如何实现这一目标。 先感谢您!

1 个答案:

答案 0 :(得分:1)

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

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;
}

您需要创建类MKAnnotationView并将下面的代码复制到您的类文件

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;