objective-c - MKAnnotationView 在收到事件时更新标题和副标题的距离

标签 objective-c iphone mapkit mkpinannotationview mkannotationview

我正在使用 mapkit 和 CLLocationManager 开发一个 iPhone 应用程序。

我在 map 上放置了很多 MKPinAnnotationView(大约 100 个),并且我想在收到它时更新所有带有用户距离的标注的字幕。

怎么做?

谢谢

<小时/>

我尝试用新位置更新字幕标注,但效果不佳。

在我的 MyAppDelegate.h

extern NSString * const GMAP_USERLOCATION_CHANGED; 

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    CLLocationManager *locationManager;
    CLLocation *userLocation;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *userLocation;
@end

在我的 MyAppDelegate.m

@implementation MyAppDelegate

NSString * const GMAP_USERLOCATION_CHANGED = @"gMapUserLocationChanged"; 
@synthesize locationManager, userLocation;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{    
    userLocation = nil;
    [[self locationManager] startUpdatingLocation];

    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
}

#pragma mark -
#pragma mark Core Location delegate
- (CLLocationManager *)locationManager 
{
    if (locationManager != nil) 
 {
        return locationManager;
    }

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    locationManager.delegate = self;

    return locationManager; 
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateToLocation:(CLLocation *)newLocation
                   fromLocation:(CLLocation *)oldLocation 
{
 self.userLocation = newLocation;
 // send notification to defaulcenter
 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
 [nc postNotificationName:GMAP_USERLOCATION_CHANGED object:self.userLocation];
}


- (void)dealloc {
    [window release];
    [locationManager release];
    [userLocation release];

    [super dealloc];
}

@end

我制作了一个名为 MyAnnotation 的自定义AnnotationView。

在 MyAnnotation.h 中:

@interface MyAnnotation : MKPinAnnotationView<MKAnnotation>
{
 double longitude;
 double latitude;
 NSString *title;
 NSString *subtitle;

}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property double longitude;
@property double latitude;
@end

在 MyAnnotation.m 中:

#import "MyAnnotation.h"
#import "MyAppDelegate.h"

@implementation MyAnnotation

@synthesize title, subtitle;
@synthesize longitude;
@synthesize latitude;

-(id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
 self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
 if (self != nil) 
 {
  NSLog(@"add observer");
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc addObserver:self selector:@selector(receivedNewUserLocation:) name:GMAP_USERLOCATION_CHANGED object:nil];
 }
 return self;
}

- (void)setTitleAndSubtitle 
{
 [self setTitleAndSubtitle:nil];
}

- (id)setTitleAndSubtitle:(CLLocation*)userLocation
{
 CLLocationDistance dist = -1;

 if(userLocation)
 {
  CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
  dist = [userLocation distanceFromLocation:poiLoc] / 1000;
  NSLog(@"distance is now %.f", dist);
 }

 title = @"the Title of the poi!";

 subtitle = [NSString stringWithFormat:@"Distance: %@", 
    dist > -1 ? [NSString stringWithFormat:@"%.2f km", dist] : @"-"
    ];

 return self;
}

- (void)receivedNewUserLocation:(NSNotification *)userLocationNotification
{
 CLLocation *userlocation = (CLLocation*)[userLocationNotification object];
 [self setTitleAndSubtitle:userlocation];
}

- (CLLocationCoordinate2D)coordinate;
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = latitude;
    theCoordinate.longitude = longitude;
    return theCoordinate; 
}

- (NSString *)title
{
    return title;
}

- (NSString *)subtitle
{
    return subtitle;
}

- (void)dealloc
{ 
    [title release];
    [subtitle release];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self];

    [super dealloc];
}
@end

最后我像在 MapViewController 中那样使用它(我在这里只放置了 viewForAnnotation 委托(delegate)方法):

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MyAnnotation *annotationView = nil;
    MyAnnotation* myAnnotation = (MyAnnotation *)annotation;
    // try to dequeue an existing pin view first
    NSString* identifier = @"CustomMapAnnotation";
    MyAnnotation *customPinView = (MyAnnotation *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];


    if(nil == customPinView) 
    {
 // if an existing pin view was not available, create one
 customPinView = [[[MyAnnotation alloc]
    initWithAnnotation:myAnnotation 
    reuseIdentifier:identifier] 
    autorelease];

 customPinView.animatesDrop = YES;
 customPinView.canShowCallout = YES;
    }

    annotationView = customPinView;
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];

    return annotationView;
}

毕竟,在 map 上加载 mkannotation 后,所有这些字幕都不会更新...... 怎么了?

感谢您的帮助...

最佳答案

当你更新标题时,你应该通知 MKAnnotationView 更新标注 View ,通过最适合你需要的 KVO 方式,例如:

  1. 合成或实现标题和使用的 setter

    self.title = @"new title";
    
  2. 使用显式 KVO 通知

    [self willChangeValueForKey:@"title"];
    [title release];
    title = [newTitle copy];
    [self didChangeValueForKey:@"title"];
    

副标题同上。

关于objective-c - MKAnnotationView 在收到事件时更新标题和副标题的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250602/

相关文章:

objective-c - 驯服 XCode 的自动完成选项

ios - 在 IOS 中创建事件时,日历事件添加了多次

ios - 如何在没有动画的情况下弹出到 View Controller

swift - 当 View ( map )未显示时停止更新位置

ios - 需要有关 shouldChangeCharactersInRange 的帮助

objective-c - 如何在 XCode 文件中查找符号?

iphone - 我怎样才能捕捉到 MapView 上的点击,然后将它传递给默认的手势识别器?

iphone - 使用 Javascript 关闭 iPhone 视频播放器

ios - 如何在不打开注释 View 的情况下放大注释单击?

ios - 在 map Swift 上选择注释时坐标为零