在单例类(ARC)中使用MKReverseGeocoder

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

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

在应用程序委托中,在启动时,我创build了我的天气单件的共享实例。 除非有UIView需要报告天气结果,否则这个实例什么也不做。 当加载报告天气的视图时,如果该位置尚未在首选项中静态设置,则该应用会尝试拉取当前位置。

这工作,我可以logging设备的当前位置的坐标。 在完成之后,我立即进入试图扭转这些坐标的地理定位。 方法MKReverseGeocoder开始得到执行,我可以logging该实例的isQuerying属性是真实的,所以我知道它正试图地理定位的coords。 (是的,我将代理设置为我的共享实例,实例的types为MKReverseGeocoderDelegate)。

现在这是奇怪的部分。 如果我第一次启动应用程序,并且第一次向屏幕添加天气UIView,则MKReverseGeocoder将启动,但不会调用委托方法。 如果我然后closures应用程序并再次打开它(第二次),当前的位置获取的查找,MKReverseGeocoder调用委托方法和一切工作。 它只是不想在第一次启动时工作,无论我多次调用它(我有一个可以启动查找的button)。

这完全是莫名其妙的 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]); } } }]; } } 

另外,我将反向地理编码器设置为(非primefaces,强大)属性并将其合成。 请记住,第二次干净启动后(当UIView已经加载的时候)这个工作正常。 对日志的调用甚至没有被打(这就是为什么我假设委托方法没有被击中)。

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

我终于搞清楚了…好吧。 而不是使用MKReverseGeocoder,我只是做一个谷歌地图ala的静态查找如何处理在iOS 4.3 MKReverseGeocoder / PBHTTPStatusCode = 503错误?

完美的作品。

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