在地图套件中显示注释
我正在使用 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;
}
上面的代码工作正常并显示地图,但不带注释。
I am working with Mapkit and I have to show annotations in the map but I'm not able to display the annotation. Here's my code:
@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;
}
The above code works fine and displays a map but not with annotation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,符合 MKAnnotation 协议的类不是视图控制器,而是数据类。
您需要创建另一个类,在本例中我将其称为“MyLandmarks”。
然后,在您的
MyMapView
类中适当的位置添加: 与您一起工作的其他 Objective-C 开发人员将会欣赏的其他一些内容:
MyMapView
如果它继承自UIViewController
。而是将其命名为MyMapViewController
。Obj_Map_View
应该是objMapView
。Typically, the class that conforms to the
MKAnnotation
protocol isn't the view controller, it's a data class.You'll need to create another class, which I'll call "MyLandmarks" for the example.
Then, somewhere appropriate in your
MyMapView
class add:A couple other bits that other Objective-C developers working with you will appreciate:
MyMapView
if it descends from aUIViewController
. Call itMyMapViewController
, instead.Obj_Map_View
should beobjMapView
.要添加注释,请使用:
addAnnotation:
阅读相关内容 此处
To add annotation use :
addAnnotation:
read about it here