2012-07-13 175 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