1

我正在尝试为天气功能(全部包含)创建一个单例类,以便我可以在单个分配的对象中更改/更新/调用整个应用程序中的天气数据。

除了一个奇怪的小错误外,我让它正常工作。我在运行 iOS < 5 的设备上使用 MKReverseGeocoder。CLGeocoder 在 iOS 5+ 上运行良好。基本上,这就是应用程序中发生的情况:

在应用程序委托中,在启动时,我创建了我的天气单例的共享实例。除非有一个 UIView 期望报告天气结果,否则这个实例不会做任何事情。加载报告天气的视图时,如果该位置尚未在首选项中静态设置,则应用程序会尝试提取您的当前位置。

这有效,我可以记录设备当前位置的坐标。完成后,我立即开始尝试反向定位这些坐标。方法 MKReverseGeocoder start 确实被执行,我可以记录实例的 isQuerying 属性为真,所以我知道它正在尝试对坐标进行地理定位。(是的,我将委托设置为我的共享实例,并且该实例的类型为 MKReverseGeocoderDelegate)。

现在这是奇怪的部分。如果我是第一次启动应用程序,并且第一次将天气 UIView 添加到屏幕上,MKReverseGeocoder 会启动但从不调用委托方法。如果我然后关闭应用程序并再次打开它(第二次),则会查找当前位置,并且 MKReverseGeocoder 确实调用了委托方法并且一切正常。它只是不想在第一次启动时工作,无论我调用它多少次(我有一个可以启动查找的按钮)。

这完全令人费解。iOS 5 CLGeocoder 在首次启动和每次后续启动时都能正常工作。MKReverseGeocoder 在初始启动时不起作用(因为它不调用委托方法),但在后续启动时起作用。

下面是相关代码:

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

    NSLog(@"error getting location:%@", error);
    self.reverseGeocoder = nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"errorGettingLocation" object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]];

}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)pm
{
    //update placemark and get weather from correct placemark
    placemark = pm;
    NSLog(@"singleton placemark: %@",placemark);
    [self getLocationFromPlacemark];
    self.reverseGeocoder = nil;
}


- (void) getReverseGeoCode:(CLLocation *)newLocation
{
    NSLog(@"reversGeocode starting for location: %@", newLocation);
    NSString *ver = [[UIDevice currentDevice] systemVersion];
    float ver_float = [ver floatValue];
    if (ver_float < 5.0) {
        //reverseGeocoder = nil;
        self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
        self.reverseGeocoder.delegate = self;
        [self.reverseGeocoder start];
        if(self.reverseGeocoder.isQuerying){
            NSLog(@"self.reverseGeocoder querying");
            NSLog(@"self.reverseGeocoder delegate %@", self.reverseGeocoder.delegate);
            NSLog(@"self %@", self);
        }else {
            NSLog(@"geocoder not querying");
        }
    }
    else {
        [reverseGeocoder5 reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error){


            if([placemarks count]>0){
                placemark = [placemarks objectAtIndex:0];
                [self getLocationFromPlacemark];
            }
            else{
                if (error) {
                    NSLog(@"error reverseGeocode: %@",[error localizedDescription]);

                }
            }
        }];

    }
}

另外,我将 reverserGeocoder 设置为(非原子,强)属性并对其进行合成。请记住,这在第二次干净启动后工作正常(当已经加载了天气 UIView 时)。对日志的调用甚至没有被命中(这就是我假设委托方法没有被命中的原因)。

任何投入将不胜感激!谢谢!

4

2 回答 2

1

我最终弄清楚了……嗯,有点。而不是使用 MKReverseGeocoder,我只是对谷歌地图进行静态查找 ala如何处理 iOS 4.3 中的 MKReverseGeocoder / PBHTTPStatusCode=503 错误?

完美运行。

于 2012-05-01T12:17:52.857 回答
0

MKReverseGeocoder 不再存在......在 iOS 5 中您必须导入 CoreLocation 并使用 CLGeocoder。下面只是一些示例示例代码:

                  CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:37.33188 longitude:-122.029497];

        [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) {
            CLPlacemark *place = [placemark objectAtIndex:0];
            if (error) {
                NSLog(@"fail %@", error);

            } else {
                NSLog(@"return %@", place.addressDictionary);
            }

        }];
于 2012-05-01T12:21:20.620 回答