我正在设备上使用
我做错了什么吗? 谢谢。
CLLocationManager
,但委托方法没有被调用。以下是我的代码(我可以看到该类也没有被释放):#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CurrentLocation : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
#import "CurrentLocation.h"
#import "SBJson.h"
@implementation CurrentLocation
@synthesize locationManager;
- (id)init
{
self = [super init];
if (self) {
NSLog(@"Initialized");
locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
BOOL enable = [CLLocationManager locationServicesEnabled];
NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"A");
NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSLog(@"%@", stringURL);
NSURL *url = [NSURL URLWithString:stringURL];
NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithData:data];
NSLog(@"%@", dict);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"B");
}
- (void)dealloc {
NSLog(@"Dealloc called");
[super dealloc];
[locationManager release];
}
@end
日志中显示:
2011-10-02 19:49:04.095 DixieMatTracker[1404:707] Initialized
2011-10-02 19:49:04.109 DixieMatTracker[1404:707] Enabled
我做错了什么吗? 谢谢。
locationManager
属性是如何定义的? - user94896assign
还是retain
呢?试试使用self.locationManager
。@Daryl Teo 我同意,这很可能是问题所在,因为locationManager
将被自动释放。 - user94896dealloc
实现中,应该在最后调用[super dealloc]
,而不是首先调用。 - Leslie Godwin