0

ユーザーの出身地を知る必要があるため、座標を取得し、mapkit を使用して必要な国コードを取得する小さなシングルトン オブジェクトを作成しました。

これが私のヘッダーファイルです:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

#define TW_GEO_CODER_CHANGED_STATE  @"TW_GEO_CODER_CHANGED_STATE"

@interface TWGeoCoder : NSObject <CLLocationManagerDelegate, MKReverseGeocoderDelegate>
{
    CLLocationManager *locationManager;
    MKReverseGeocoder *geoCoder;

    NSString *currentCountryIsoCode;
}

+ (TWGeoCoder*) sharedTWGeoCoder;

-(void) startGeoCoder;
-(void) stopGeoCoder;


@property (nonatomic, retain) NSString *currentCountryIsoCode;

@end

そして実装:

#import "TWGeoCoder.h"

@implementation TWGeoCoder


static TWGeoCoder* _singleton;

+ (TWGeoCoder*) sharedTWGeoCoder
{
    @synchronized([TWGeoCoder class])
    {
        if (_singleton == nil)
        {
            _singleton = [[TWGeoCoder alloc] init];
        }
    }
    return _singleton;
}


- (void)startGeoCoder
{  
    if (locationManager == nil) 
    {
        locationManager = [[CLLocationManager alloc] init];
    }
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.purpose = NSLocalizedString(@"#LocalizationPurpose",nil);
    [locationManager startUpdatingLocation];
}

- (void) stopGeoCoder 
{            
    if (geoCoder != nil)
    {
        [geoCoder cancel];
        [geoCoder release];
        geoCoder = nil;
    }

    if (locationManager != nil)
    {
        [locationManager stopUpdatingLocation];
        [locationManager release];
        locationManager = nil;
    }
}


#pragma mark -
#pragma mark locationManager Delegate 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    if (geoCoder == nil)
    {
        geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    }

    geoCoder.delegate = self;
    [geoCoder start];       
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
    [self stopGeoCoder];
}



#pragma mark -
#pragma mark reverseGeocoder Delegate 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{

    self.currentCountryIsoCode = placemark.countryCode;

    [[NSNotificationCenter defaultCenter] postNotificationName:TW_GEO_CODER_CHANGED_STATE 
                                                        object:self];
}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
    [self stopGeoCoder];    
}


#pragma mark -
#pragma mark Synthesizes

@synthesize currentCountryIsoCode;

@end

stopGeoCoder メソッドを呼び出すと、performSelectorOnMainThread 経由で呼び出しても、アプリがクラッシュします...

問題は次の行にあります。

if (geoCoder != nil)
    {
        [geoCoder cancel];
        [geoCoder release];
        geoCoder = nil;
    }

MKReverseGeocoder をリリースしようとすると、すごく怒っているようです。「didFail」メソッドでのみクラッシュします。実際、目印が見つかると、別のクラスが通知を受け取り、何かを実行して stopGeocoder を呼び出しますが、クラッシュしません! なんてこと?

4

1 に答える 1

0

解決策については、スタック オーバーフローの別の投稿を参照する必要があります。

cllocation と mkreversegeocoder

于 2011-07-22T07:56:21.383 に答える