在我的注释左侧添加图像 [英] Add image to the left of my annotations

查看:20
本文介绍了在我的注释左侧添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了在我的地图上完美显示的注释,但我不确定如何实现 leftCalloutAccessoryView 来显示图像.

I have created annotations that show up perfectly on my map but I am not sure how to implement the leftCalloutAccessoryView to show images.

MapViewAnnotation.h

MapViewAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

MapViewAnnotation.m

MapViewAnnotation.m

#import "MapViewAnnotation.h"

@implementation MapViewAnnotation
@synthesize coordinate, title, subtitle;

@end

GroundsViewController.h

GroundsViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>


@interface GroundsViewController : UIViewController <MKMapViewDelegate>
{
    MKMapView *mapView;
}

@property (strong, nonatomic) IBOutlet UISegmentedControl *segment;

- (IBAction)changeSeg:(id)sender;
@end

GroundsViewController.m

GroundsViewController.m

#import "GroundsViewController.h"
#import "MapViewAnnotation.h"

@interface GroundsViewController ()


@end

// Centre in on Northern Ireland

#define Northern_Ireland_Latitude 54.629338;
#define Northern_Ireland_Longitude -6.668701;

//Span

#define The_Span 2.00f;


// Premiership

#define Ballymena_Latitude 54.870105;
#define Ballymena_Longitude -6.265076;


// Championship 1

#define Ards_Latitude 54.651629;
#define Ards_Longitude -5.684478;

// Championship 2

#define Annagh_Latitude 54.411372;
#define Annagh_Longitude -6.440355;


@implementation GroundsViewController

@synthesize segment;

- (IBAction)changeSeg:(id)sender {
    if (segment.selectedSegmentIndex == 0) {
        mapView.mapType = MKMapTypeStandard;
    }

    if (segment.selectedSegmentIndex == 1) {
        mapView.mapType = MKMapTypeSatellite;
    }

    if (segment.selectedSegmentIndex == 2) {
        mapView.mapType = MKMapTypeHybrid;
    }

}

// When the view loads
- (void)viewDidLoad
{
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:mapView atIndex:0];

    // Create the region

    MKCoordinateRegion myRegion;

    // Center

    CLLocationCoordinate2D center;
    center.latitude = Northern_Ireland_Latitude;
    center.longitude = Northern_Ireland_Longitude;

    // Create the Span

    MKCoordinateSpan span;
    span.latitudeDelta = The_Span;
    span.longitudeDelta = The_Span;

    myRegion.center = center;
    myRegion.span = span;

    // Set our map view

    [mapView setRegion:myRegion animated:YES];


    NSMutableArray *locations = [[NSMutableArray alloc] init];
    CLLocationCoordinate2D location;
    MapViewAnnotation *myAnn;

    //Premiership

    // Pin to show Ballymena United F.C.
    myAnn = [[MapViewAnnotation alloc] init];
    location.latitude = Ballymena_Latitude;
    location.longitude = Ballymena_Longitude;
    myAnn.coordinate = location;
    myAnn.title = @"Ballymena United F.C.";
    myAnn.subtitle = @"The Showgrounds";
    [locations addObject:myAnn];


    // Championship 1

    // Pin to show Ards F.C.
    myAnn = [[MapViewAnnotation alloc] init];
    location.latitude = Ards_Latitude;
    location.longitude = Ards_Longitude;
    myAnn.coordinate = location;
    myAnn.title = @"Ards F.C.";
    myAnn.subtitle = @"Clandeboye Park";
    [locations addObject:myAnn];


    // Championship 2

    // Pin to show Annagh United F.C.
    myAnn = [[MapViewAnnotation alloc] init];
    location.latitude = Annagh_Latitude;
    location.longitude = Annagh_Longitude;
    myAnn.coordinate = location;
    myAnn.title = @"Annagh United F.C.";
    myAnn.subtitle = @"Tandragee Road";
    [locations addObject:myAnn];


    [self->mapView addAnnotations:locations];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

推荐答案

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

static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[PlaceMark class]]) {

    MKPinAnnotationView *annotationView = 
    (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] 
                          initWithAnnotation:annotation 
                          reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;


UIImageView *imvLeft = [[UIImageView alloc] init];
//Set Image
    [annotationView setLeftCalloutAccessoryView:imvLeft];

    return annotationView;
}

return nil; 

}

这篇关于在我的注释左侧添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆