iOS 正向地理编码块未执行
为什么这段代码没有被执行?我从我的另一个项目中复制并粘贴了它,它运行得很好。我还在我的其他应用程序中尝试过使用我在此处插入的相同 addressString
,并且效果非常好。
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
NSLog(@"Address string: %@",addressString);
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"%@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
任何地标和错误消息都不会记录到控制台。
这是我的整个 AppDelegate.m:
@implementation AppDelegate
@synthesize window = _window;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *error = nil;
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString *JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Streets" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSDictionary *dict = [parser objectWithString:JSONString error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSArray *addresses = [[dict objectForKey:@"results"] retain];
NSDictionary *boroughs = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Bronx",@"Brooklyn",@"New York", @"Queens",@"Staten Island",nil] forKeys:[NSArray arrayWithObjects:@"B",@"K",@"M",@"Q",@"S", nil]];
int i = 1;
for(NSDictionary *file in addresses)
{
NSString *borough = [file objectForKey:@"Borough"];
NSString *ID = [file objectForKey:@"ID"];
NSString *leftBound = [file objectForKey:@"LeftBound"];
NSString *rightBound = [file objectForKey:@"RightBound"];
NSString *sideOfStreet = [file objectForKey:@"SideOfStreet"];
NSString *street = [file objectForKey:@"Street"];
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
// NSLog(@"Address string: %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
{
NSLog(@"AAAAAAAAAAAAAAAAAAAAAAAA");
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"Placemark: %@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
[geocoder release];
NSLog(@"%d",i++);
}
[parser release];
[addresses release];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"geocoder"])
{
NSLog(@"AAAAAAA");
}
}
@end
Why this block of code isn't being executed? I copied and pasted it from another project of mine, where it works just fine. I also tried it in my other app with the same addressString
I'm plugging in here, and it worked perfectly.
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
NSLog(@"Address string: %@",addressString);
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"%@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
Neither any placemarks nor an error message is logged to the console.
Here is my entire AppDelegate.m:
@implementation AppDelegate
@synthesize window = _window;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *error = nil;
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString *JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Streets" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSDictionary *dict = [parser objectWithString:JSONString error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSArray *addresses = [[dict objectForKey:@"results"] retain];
NSDictionary *boroughs = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Bronx",@"Brooklyn",@"New York", @"Queens",@"Staten Island",nil] forKeys:[NSArray arrayWithObjects:@"B",@"K",@"M",@"Q",@"S", nil]];
int i = 1;
for(NSDictionary *file in addresses)
{
NSString *borough = [file objectForKey:@"Borough"];
NSString *ID = [file objectForKey:@"ID"];
NSString *leftBound = [file objectForKey:@"LeftBound"];
NSString *rightBound = [file objectForKey:@"RightBound"];
NSString *sideOfStreet = [file objectForKey:@"SideOfStreet"];
NSString *street = [file objectForKey:@"Street"];
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
// NSLog(@"Address string: %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
{
NSLog(@"AAAAAAAAAAAAAAAAAAAAAAAA");
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"Placemark: %@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
[geocoder release];
NSLog(@"%d",i++);
}
[parser release];
[addresses release];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"geocoder"])
{
NSLog(@"AAAAAAA");
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定您的“geocoder”实例不为零吗?
如果您向“nil”对象发送消息,什么都不会发生...:)
Have you made sure your "geocoder" instance is not nil?
Nothing will happen if you send a message to a "nil" object... :)
我还是不确定发生了什么事。我在模拟器上运行了以下代码(原始代码减去 JSON 操作),它运行得很好。也许您需要全新安装 Xcode & iOS 模拟器?
输出是:
我唯一能想到的是你可能太早发布你的地理编码器了!也许尝试将版本移到块中?这样,您就会知道地理编码器仅在完成地理编码操作后才被释放一次。
另外,您在块内的错误处理中犯了一个错误。
它应该是
NSLog(@"Error: %@",[anError description]);
而不是
NSLog(@"Error: %@",[错误描述]);
。另外,请确保您没有使用 ARC...
I'm still not sure what's going on. I ran the following code (original code minus the JSON operations) on my simulator and it worked perfectly. Maybe you need a fresh install of Xcode & iOS Simulator?
Output was:
The only thing I can think of is that you may be releasing your geocoder too early! Maybe try moving the release into the block? This way, you'll know the geocoder is only being released once after it has finished the geocode operation.
Also, you made a mistake with your error handling inside the block.
It should be
NSLog(@"Error: %@",[anError description]);
instead of
NSLog(@"Error: %@",[error description]);
.Also, make sure you're not using ARC...
我知道这是一个老问题,但它不起作用的原因是你不能同时发出多个转发请求。在发送另一个之前,您必须检查 isGeocoding 属性。
I know this is an old question but the reason this doesn't work is you cannot make multiple forward requests simultaneously. You must check the property isGeocoding before sending another one.