代码之家  ›  专栏  ›  技术社区  ›  zebra

cllocation和mkreversegeocoder

  •  3
  • zebra  · 技术社区  · 14 年前

    我试图用cllocation和mkreversegeocoder检索城市名。 在我的viewdidload方法中,i istance cllocationmanager:

    self.locManager = [[CLLocationManager alloc] init];
    locManager.delegate = self;
    locManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locManager startUpdatingLocation];
    

    之后:

    - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation 
    *)newLocation
    fromLocation:(CLLocation *)oldLocation {
    //some code to retrieve my information
    
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]     
    initWithCoordinate:newLocation.coordinate ];
    geoCoder.delegate = self;
    [geoCoder start];
    }
    
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark  
    *)placemark
    {
    MKPlacemark *myPlacemark = placemark;
    citta.text = [placemark locality];
    }
    
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    citta.text =@ "Unknow";
    

    应用程序只在我第一次得到我的价值时工作。在第二次应用程序崩溃时。 我想是因为地理编码器启动了,我想我必须有一个而且只有一个距离运行。(但我真的不是舒尔…)我可以用哪种方式控制地理编码器的运行?

    我看到我可以在视图中使用cllocationcoordinate2d加载mkreversegeocoder,但是…用哪种方法可以检索newlocation.coordinate?

    我已经部分解决了将geocoder声明为类变量并检查

    if (self.geocoder != nil) {
       [geocoder release];
    }
    

    但是…如果在我

    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark  
    *)placemark
    

    或者在我的

    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(@NSError 
    *)error
    

    我释放还是取消我的对象? 我觉得自己很蠢:D

    1 回复  |  直到 14 年前
        1
  •  7
  •   user121301    14 年前

    不要将反向地理编码器创建为局部变量。

    查看中的mkreversegeocoder示例 CurrentAddress 苹果提供的示例应用程序。请参见mapviewcontroller.h和mapviewcontroller.m。

    在代码中遵循相同的模式。