2

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 がマップにロードされた後、このサブタイトルは更新されません。何が問題なのですか?

ご協力いただきありがとうございます...

4

2 に答える 2

6

タイトルを更新するときは、MKAnnotationView にコールアウト ビューを更新するよう通知する必要があります。これには、ニーズに最適な KVO の方法が使用されます。次に例を示します。

  1. タイトルのセッターを合成または実装して使用する

    self.title = @"new title";
    
  2. 明示的な KVO 通知を使用する

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

サブタイトルも同上。

于 2011-01-29T04:29:57.297 に答える
2

MKAnnotations の作成方法によって異なります。

MKAnnotation プロトコルに準拠する Place.h および Place.m で表される「Place」のようなオブジェクトが必要になる可能性があります...

場所には、次のようなプロパティがあります

float distance;

次に、字幕メソッド(MKAnnotation の一部)は次のようになります

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

サブタイトル メソッドは、常にマップビューによって呼び出されます (実際、呼び出される頻度はほとんどばかげています)。そのため、距離の値を操作するとすぐに、マップに反映されます (おそらく、次にタップしたとき)。ピン上)。

于 2010-07-14T21:26:19.343 に答える