没有获得当前用户位置

时间:2012-07-13 07:01:24

标签: iphone

<。>文件中的

#import <MapKit/MapKit.h>
@interface FirstViewController : UIViewController <MKMapViewDelegate,MKReverseGeocoderDelegate,CLLocationManagerDelegate>
{

}
<。>文件中的

-(void)viewDidLoad
{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    [super viewDidLoad];
}


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

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

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

     NSString *kABPersonAddressCityKey;
     NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];

     lblAddress.text = city;

     NSLog(@"city detail is:--> %@",city);
}

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

这是我为获取用户和当前位置所做的代码。将其打印在标签上。

CLLocationManager委托方法(如上所述)未被调用&amp;我无法获得当前地址。

请帮帮我。

我在哪里做错了??指导我。

感谢。

2 个答案:

答案 0 :(得分:1)

在您的方法viewDidLoad中:

-(void)viewDidLoad
{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    myMapView.showsUserLocation = YES;     // add this line

    [super viewDidLoad];
}

你完成了!!!

答案 1 :(得分:1)

不是自动释放CLLocationManager实例,而是将其分配给班级中的ivar。然后像往常一样在-dealloc中释放它(或者如果你不再需要它,则在其中一个委托方法中)。

我怀疑你的位置管理器在下一轮运行循环中被取消分配,然后才有机会启动它的委托方法。