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(上に書かれている) デリゲート メソッドは呼び出されず、現在のアドレスを取得できません。

私を助けてください。

私はどこで間違っていますか...?教えて。

ありがとう。

4

2 に答える 2

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

そして、あなたは完了です!!!

于 2012-07-13T07:05:43.190 に答える
1

インスタンスを自動解放する代わりにCLLocationManager、クラスの ivar に割り当てます。-dealloc次に、通常どおり (または、不要になった場合はデリゲート メソッドのいずれかで)解放します。

デリゲート メソッドを起動する前に、実行ループの次のターンでロケーション マネージャーの割り当てが解除されていると思われます。

于 2012-07-13T07:06:19.840 に答える