在地图套件中显示注释

发布于 2024-09-16 16:25:38 字数 1449 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半仙 2024-09-23 16:25:38

通常,符合 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

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.

@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

Then, somewhere appropriate in your MyMapView class add:

MyLandmark *landmark = [[[MyLandmark alloc]init]autorelease];
[Obj_Map_View addAnnotation:landmark];

A couple other bits that other Objective-C developers working with you will appreciate:

  • To avoid confusion, don't call the class MyMapView if it descends from a UIViewController. Call it MyMapViewController, instead.
  • Classes start with a capital letter, variables start lowercase. Both are CamelCased. Obj_Map_View should be objMapView.
海拔太高太耀眼 2024-09-23 16:25:38

要添加注释,请使用: addAnnotation:

阅读相关内容 此处

To add annotation use : addAnnotation:

read about it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文