【问题标题】:MKAnnotationView update title and subtitle with distance when event is receivedMKAnnotationView 在收到事件时更新标题和副标题与距离
【发布时间】:2011-03-16 02:27:58
【问题描述】:

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

我在地图上放了很多 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 的 customAnnotationView。

在 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 委托方法):

- (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;
}

在地图上加载 mkannotation 后,所有这些字幕都没有更新...... 怎么了?

感谢您的帮助...

【问题讨论】:

    标签: objective-c iphone mapkit mkpinannotationview mkannotationview


    【解决方案1】:

    这取决于您创建 MKAnnotations 的方式。

    你应该有一个像“Place”这样的对象,由 Place.h 和 Place.m 表示,它符合 MKAnnotation 协议...

    Place 会有一个类似的属性

    float distance;
    

    然后你的字幕方法(MKAnnotation 的一部分)会做这样的事情

    - (NSString *)subtitle
    {
        return [NSString stringWithFormat:@"%0.2f Miles Away", distance];
    }
    

    那个 subtitle 方法被 mapview 不断调用(实际上它被调用的频率几乎是荒谬的),所以一旦你操纵了距离的值,它就会反映在地图上(可能早在下一个您点击图钉的时间)。

    【讨论】:

    • 感谢您的解决方案。这似乎是一个不错的解决方案。在此之前,我希望在收到 CLLocationManager 的位置更新时尝试读取我的 mapviewcontroller 中的注释数组。但是标题是不可取的。关于刷新时间,我打算将 CLLocationManager 添加到 appdelegate 中,这将更改它的属性,然后使用观察者监视此属性,以在每次收到更新时更改位置。我会看看它是否有效。如果是这种情况,我会在这里发布解决方案...
    • 我认为还缺少其他东西。是的,这将在每个注释的副标题中返回距离,但您必须在其他位置设置距离。
    【解决方案2】:

    当您更新标题时,您应该通知 MKAnnotationView 以最适合您需要的 KVO 方式更新标注视图,例如:

    1. 为title和使用合成或实现setter

      self.title = @"new title";
      
    2. 使用明确的 KVO 通知

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

    字幕同上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多