【发布时间】:2013-11-22 13:11:54
【问题描述】:
我正在制作一个简单的地图应用程序 - 我正在 pubnub 频道上发送和接收位置和文本。当聊天消息进来时,我想使用一个简单的 MKAnnotation 来绘制聊天(我知道这是 UX 的可怕行为,我不在乎)。
当我的应用程序委托在 pubnub 频道上收到一条消息时,它会调用主视图控制器中的一个方法来在地图上绘制文本消息。该方法应使用最新的用户位置作为图钉的坐标。
我不知道为什么,但我无法在我的方法中显示注释。我已经尝试从方法中构建注释并显示它。我也尝试过制作一个自定义注释类并从我的方法中调用它。当我使用相同的注释代码但在我的 viewDidLoad 中对其进行硬编码时,它会显示得很好。任何见解将不胜感激。
我的应用代表:
#import "MyLocationAppDelegate.h"
#import "MyLocationViewController.h"
@implementation MyLocationAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[PubNub setDelegate: self];
return YES;
}
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message
{
NSString* text = message.message;
//Call drawChat method of MyLocationViewController
MyLocationViewController *MyLocViewController = [[MyLocationViewController alloc] init];
[MyLocViewController drawChat:text];
}
我的视图控制器:
#import "MyLocationViewController.h"
#import "MyLocationAppDelegate.h"
#import "MyLocationAnnotation.h"
CLLocation *userLocation;
@implementation MyLocationViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Delegate map view
self.mapView.delegate = self;
[self SetUpChat];
[self configurePubNub];
//Instantiate location manager
if (nil == locationManager) {
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"Application: viewDidLoad");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
userLocation = [locations lastObject];
}
- (void)drawChat:(NSString *)message
{
//Create new annotation object
CLLocationCoordinate2D location;
location.latitude = userLocation.coordinate.latitude;
location.longitude = userLocation.coordinate.longitude;
MyLocationAnnotation *chat = [[MyLocationAnnotation alloc] initWithLocation:location andTitle:message];
[self.mapView addAnnotation:chat];
}
还有 MyLocationAnnotation.m
#import "MyLocationAnnotation.h"
@implementation MyLocationAnnotation
- (id)initWithLocation:(CLLocationCoordinate2D)coord andTitle:(NSString *)ttl {
self = [super init];
if (self) {
_coordinate = coord;
_title = ttl;
}
return self;
}
@end
【问题讨论】:
-
在调用 drawChat 方法之前是否调用了 didUpdateLocation。首先检查 userLocation 是否有任何内容。
-
感谢 Bhumeshwer - 我已经从 drawChat 方法中检查了 userLocation,它正确地填充了纬度和经度。
-
在 didReceiveMessage 中,您正在创建 MyLocationViewController 的新实例,并且该实例从未出现(显示)。原始的 MyLocationViewController 在哪里创建和呈现?
-
啊 - 这很有意义 - 感谢您的输入。我认为这是一种让 appDelegate 访问 MyLocationViewController 中的方法的方法。除了 MyLocationViewController.m 文件之外,我没有在其他任何地方显式创建它 - MyLocationViewController 内部只有一个 MKMapView。
-
万岁 - 感谢您朝正确的方向轻推 Anna - 我能够解决这个问题:我没有在应用程序委托中重新声明我的主视图控制器,而是这样做:MyLocationViewController *mainController = (MyLocationViewController * ) self.window.rootViewController; [主控制器drawChat:文本];然后我可以使用来自应用程序委托的数据很好地调用该方法。
标签: ios annotations mapkit