首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布MKReverseGeocoder会让我的应用崩溃!

发布MKReverseGeocoder会让我的应用崩溃!
EN

Stack Overflow用户
提问于 2011-04-18 23:32:45
回答 1查看 585关注 0票数 0

我需要知道我的用户来自哪里,所以我做了一个小的单例对象,它获取坐标并使用mapkit来获取我需要的国家代码。

下面是我的头文件:

代码语言:javascript
复制
#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

以及实现方法:

代码语言:javascript
复制
#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调用它也是如此…

问题出在以下几行中:

代码语言:javascript
复制
if (geoCoder != nil)
    {
        [geoCoder cancel];
        [geoCoder release];
        geoCoder = nil;
    }

当我试图发布它的时候,MKReverseGeocoder似乎变得非常生气!我只在"didFail“方法上得到崩溃。实际上,当它找到placemark时,另一个类将收到通知,做一些事情并调用stopGeocoder和...它不会崩溃!见鬼?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-22 15:56:21

你应该参考堆栈溢出中的另一个帖子来解决这个问题:

cllocation and mkreversegeocoder

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5705282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档