如何在地图视图中将标注添加到单个注释中
Posted
技术标签:
【中文标题】如何在地图视图中将标注添加到单个注释中【英文标题】:How to add callout into individual annotation in map view 【发布时间】:2014-02-04 02:35:37 【问题描述】:我正在尝试为每个注释设置不同的标注详细信息。
目前,当我单击任何注释位置时,它会显示所有标注信息。
#import "AnnotationViewController.h"
#import "Annotation.h"
@implementation AnnotationViewController
@synthesize mapView;
-(void)viewDidLoad
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion TT = 0.0, 0.0 , 0.0, 0.0 ;
TT.center.latitude = 43.65343;
TT.center.longitude = -79.396311;
TT.span.longitudeDelta = 0.02f;
TT.span.latitudeDelta = 0.02f;
[mapView setRegion:TT animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = @"Annotation 01";
ann1.subtitle = @"Message 01";
ann1.coordinate = TT.center;
[mapView addAnnotation:ann1];
MKCoordinateRegion TTY = 0.0, 0.0 , 0.0, 0.0 ;
TTY.center.latitude = 43.76919;
TTY.center.longitude = -79.41245;
TTY.span.longitudeDelta = 0.02f;
TTY.span.latitudeDelta = 0.02f;
[mapView setRegion:TTY animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = @"Annotation 02";
ann2.subtitle = @"Message 02";
ann2.coordinate = TTY.center;
[mapView addAnnotation:ann2];
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:id<MKAnnotation>)annotation
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
view.pinColor = MKPinAnnotationColorPurple;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GPSicon.png"]];
view.leftCalloutAccessoryView = imageView;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
NSString *msg = [@"Location 01" stringByAppendingFormat:@"Opening 01"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Plaza 01" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
NSString *msg02 = [@"Location 02" stringByAppendingFormat:@"Opening 02"];
UIAlertView *alert02 = [[UIAlertView alloc] initWithTitle:@"Plaza 02" message:msg02 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert02 show];
-(void)button:(id)sender
NSLog(@"Button action");
- (void)dealloc
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
*/
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
end
所以这就是发生的事情:当我点击“Annotation 01”时,会出现气泡。但是当我点击标注“详细图标”时,它会弹出位置 01 和位置 2 标题。
感谢安娜的帮助。但是当我检查一个注释时,我仍然得到 2 个标注。这是现在的代码..
#import "AnnotationViewController.h"
#import "Annotation.h"
@implementation AnnotationViewController
@synthesize mapView;
-(void)viewDidLoad
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion TT = 0.0, 0.0 , 0.0, 0.0 ;
TT.center.latitude = 43.65343;
TT.center.longitude = -79.396311;
TT.span.longitudeDelta = 0.02f;
TT.span.latitudeDelta = 0.02f;
[mapView setRegion:TT animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = @"Annotation 01";
ann1.subtitle = @"Message 01";
ann1.coordinate = TT.center;
[mapView addAnnotation:ann1];
MKCoordinateRegion TTY = 0.0, 0.0 , 0.0, 0.0 ;
TTY.center.latitude = 43.76919;
TTY.center.longitude = -79.41245;
TTY.span.longitudeDelta = 0.02f;
TTY.span.latitudeDelta = 0.02f;
[mapView setRegion:TTY animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = @"Annotation 02";
ann2.subtitle = @"Message 02";
ann2.coordinate = TTY.center;
[mapView addAnnotation:ann2];
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:id<MKAnnotation>)annotation
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
view.pinColor = MKPinAnnotationColorPurple;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GPSicon.png"]];
view.leftCalloutAccessoryView = imageView;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
//cast the object to our custom class...
Annotation *ann1 = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:ann1.title
message:ann1.subtitle
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
//cast the object to our custom class...
Annotation *ann2 = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:ann2.title
message:ann2.subtitle
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
-(void)button:(id)sender
NSLog(@"Button action");
- (void)dealloc
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
*/
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
end
我的 Annotation.m 文件代码
#import "Annotation.h"
@implementation Annotation
@synthesize coordinate, title, subtitle;
-(void)dealloc
@end
我的 Annotation.h 文件代码
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface Annotation : NSObject <MKAnnotation>
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
我是否必须为每个注释制作大量 h/m 文件才能使标注不同?我只是在这一点上跺脚。任何猜测都会很棒,谢谢!
【问题讨论】:
请不要显示两个警报视图。就像我的例子一样显示一个。 对不起,我没有说清楚。我试图让它显示不同的标注信息然后气泡信息。每个注释。 【参考方案1】:在calloutAccessoryControlTapped
中,代码显示两个警报视图,其中一个接一个地显示标题和消息的硬编码文本。显然,这就是你得到的。
在calloutAccessoryControlTapped
方法中,可以通过view.annotation
获得点击了标注按钮的注释对象。
view
参数是MKAnnotationView
,它有一个annotation
属性指向其相关的注解。
因此更改代码以仅显示一个警报视图,并根据注释对象的属性显示标题和消息。
检查注释的类以确保它是您期望的注释类型也是一个好主意。
例如:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
//cast the object to our custom class...
Annotation *ann = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:ann.title
message:ann.subtitle
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
【讨论】:
对不起,我没有说清楚。我试图让它显示不同的标注信息然后气泡信息。每个注释 将要显示的信息添加到 Annotation 类。在创建每个注释时设置该信息。然后在 calloutAccessoryControlTapped 中访问该信息,就像示例访问标题和副标题一样。 你能给我看一个示例脚本吗?我对 xcode 本身有点陌生。非常感谢你帮助我。 看到这个问题:***.com/questions/5939223/store-data-in-mkannotation 它仍然在一个注释中显示所有标注。安娜 如果我可以付钱/雇用你来帮助我,有可能吗?我的时间有点短,我需要完成这个项目。请告诉我。你可以联系我“assamiea@gmail.com”以上是关于如何在地图视图中将标注添加到单个注释中的主要内容,如果未能解决你的问题,请参考以下文章