【问题标题】:Map not showing annotation地图未显示注释
【发布时间】:2014-03-14 19:06:32
【问题描述】:

我在 mapView 中添加了一个注释,但它没有显示出来。

地图出现并以预期位置为中心。地图中心坐标与注解坐标相同。

欢迎任何帮助检测问题出在哪里。

#import "mapKitViewController.h"
#import "Annotation.h"

@interface mapKitViewController ()

@end

//AYUNTAMIENTO DE LAS PALMAS DE GC
#define LP_LATITUDE 28.1249625;
#define LP_LONGITUDE -15.4286931;
//span
#define THE_SPAN 0.01f;



@implementation mapKitViewController
@synthesize myMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Localízame";    


    //DATOS DEL MAPA
    //region
    MKCoordinateRegion myRegion;

    //centro del mapa
    CLLocationCoordinate2D center;
    center.latitude = LP_LATITUDE;
    center.longitude = LP_LONGITUDE;

    //span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

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

    [myMapView setRegion:myRegion animated:YES];

    //annotation
    CLLocationCoordinate2D ayuntamiento;
    ayuntamiento.latitude = LP_LATITUDE;
    ayuntamiento.longitude = LP_LONGITUDE;

    Annotation *aytoPin;
    aytoPin.coordinate = ayuntamiento;
    aytoPin.title = @"Ayuntamiento";
    aytoPin.subtitle = @"Las Palmas de Gran Canaria";
    [myMapView addAnnotation:aytoPin];
}

【问题讨论】:

  • 除了下面马特的回答,请确保您正在设置地图视图的委托并实现委托方法mapView:viewForAnnotation:
  • @David,显示默认注释(红色引脚),无需设置委托或实现 viewForAnnotation。
  • @Anna 但如果他使用默认注释,他不会使用 MKPointAnnotation 吗?大概这是他自己的一些特殊课程。
  • @matt、MKPointAnnotation 和任何实现 MKAnnotation 协议的类只是您告诉地图添加的 model 对象。他们的 view 是别的东西(MKAnnotationView 或其子类)。
  • 不确定 Anna,但无论如何,主要问题是 Matt 的回答。

标签: ios mapkit mapkitannotation


【解决方案1】:

我不知道 Annotation 是什么(而不是内置的 MKPointAnnotation 或类似的),但我认为它知道如何正确绘制自己。

那么问题来了:

Annotation *aytoPin;
aytoPin.coordinate = ayuntamiento;

您没有创建注释(allocinit)。所以aytoPin 为零,什么也没有发生。这里没有对象。

(很容易被这种错误抓住,因为你后续的消息是指向 nil 的消息,这在 Objective-C 中不是错误。所以没有任何事情提醒你这个问题。)

【讨论】:

  • 谢谢马特,这是初学者的错误。我已按如下方式更新了该行。注解 *aytoPin = [注解分配];现在它显示出来了....不需要初始化?? Annotation 是 NSObject 的子类。
  • 是的,您应该使用[[Annotation alloc] init][Annotation new] 这是同义词。
  • 永远不要说alloc而不说init,也永远不要说init,除非说alloc(或实现init)。 :) 在这里查看我的讨论:apeth.com/iOSBook/ch05.html#_instantiation_from_scratch 注意:我不知道您的 Annotation 类的初始化程序;您可能需要提供更多信息。
  • 我的问题与我正在关注的教程有关,程序员在没有 init 的情况下使用 alloc ...但我想这并不完全正确...
  • 哇,如果我是你,我不会再看那个教程了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多