我正在使用Mapkit,我必须在地图中显示注释,但我无法显示注释。这是我的代码:
@interface MyMapView : UIViewController <MKAnnotation,MKMapViewDelegate>{
MKMapView *Obj_Map_View;
MKPlacemark *pmark;
MKReverseGeocoder *geocoder1;
}
@end
#import "MyMapView.h"
@implementation MyMapView
- (id)init {
if (self = [super init]) {
}
return self;
}
- (void)loadView {
[super loadView];
Obj_Map_View = [[MKMapView alloc]initWithFrame:self.view.bounds];
Obj_Map_View.showsUserLocation =YES;
Obj_Map_View.mapType=MKMapTypeStandard;
[self.view addSubview:Obj_Map_View];
Obj_Map_View.delegate = self;
CLLocationCoordinate2D cord = {latitude: 19.120000, longitude: 73.020000};
MKCoordinateSpan span = {latitudeDelta:0.3, longitudeDelta:0.3};
MKCoordinateRegion reg= {cord,span};
[Obj_Map_View setRegion:reg animated:YES];
//[Obj_Map_View release];
}
- (NSString *)subtitle{
return @"Sub Title";
}
- (NSString *)title{
return @"Title";
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annov = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Current location"];
annov.animatesDrop = TRUE;
[annotation title]==@"Current location";
annov.canShowCallout = YES;
[annov setPinColor:MKPinAnnotationColorGreen];
return annov;
}
上面的代码工作正常并显示地图但不显示注释。
答案 0 :(得分:2)
通常,符合MKAnnotation
协议的类不是视图控制器,而是数据类。
您需要创建另一个类,我将其称为“MyLandmarks”。
@interface MyLandmarks : NSObject <MKAnnotation>
// Normally, there'd be some variables that contain the name and location.
// And maybe some means to populate them from a URL or a database.
// This example hard codes everything.
@end
@implementation MyLandmarks
-(NSString*)title {
return @"'ere I am, J.H.";
}
-(NSString*)subtitle {
return @"The ghost in the machine.";
}
-(CLLocationCoordinate2D) coordinate {
CLLocationCoordinate2D coord = {latitude: 19.120000, longitude: 73.020000};
return coord;
}
@end
然后,在MyMapView
班级的适当位置添加:
MyLandmark *landmark = [[[MyLandmark alloc]init]autorelease];
[Obj_Map_View addAnnotation:landmark];
其他与您合作的Objective-C开发人员将会欣赏以下几点:
MyMapView
,请不要调用类UIViewController
。改为称呼MyMapViewController
。Obj_Map_View
应为objMapView
。答案 1 :(得分:0)
要添加注释,请使用:addAnnotation:
了解它here