2012-07-13 178 views
0

在.h文件中没有获得当前用户位置

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

} 

在.m文件

-(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委托方法(这是写在上面)不叫&我无法获得当前地址。

请帮我一把。

我在哪里犯错...?引导我。

谢谢。

回答

1

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

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

+0

什么是错误? (顺便说一句,我想你是在泄漏你的反向地理编码器。) – 2012-07-13 07:15:28

+0

reverseGeocoder: didFailWithError:错误域= PBRequesterErrorDomain代码= 6001“操作无法完成(PBRequesterErrorDomain错误6001.)” – 2012-07-13 07:19:33

+0

似乎很好对我来说 - 你看到http://stackoverflow.com/questions/3031038/reversegeocoder-fail-error-in-mapkit? – 2012-07-13 07:23:35

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]; 
} 

,就大功告成了!

+0

您绝对不会*需要在地图上显示位置以获取位置;你根本不需要一个'MKMapView'实例!无论如何,该属性是'showUserLocation'而不是'showUserLocation'。 – 2012-07-13 07:08:02

+0

我还没有Mapview控制器... – 2012-07-13 07:11:57