将地址编码到iphone中的坐标中

时间:2012-05-24 10:48:54

标签: iphone ios ios5 geocoding

我正在尝试使用以下代码将地址编码为坐标:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"6138 Bollinger Road, San Jose, United States" completionHandler:^(NSArray* placemarks, NSError* error){
                     for (CLPlacemark* aPlacemark in placemarks)
                     {
                         // Process the placemark.
                         NSString *latDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
                         NSString *lngDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
                         lblDestinationLat.text = latDest1;
                         lblDestinationLng.text = lngDest1;
                     }
                 }];

我已经尝试了很多次但调试器从未进入块状态,我无法获得该位置。有人可以帮帮我吗?

问候
的Pankaj

3 个答案:

答案 0 :(得分:10)

好吧我发现了我的错误。 代码是正确的,并且完美无缺。我一直在研究它是通过调试器,并试图弄清楚为什么调试器没有进入块。但是现在我发现调试器在那一刻没有进入块。获取位置值几乎不需要。它是异步完成的,所以我无法找到它,因为崩溃后没有值,我崩溃了。我已将代码发布块移动到块内部,现在一切正常。

答案 1 :(得分:0)

我刚刚运行了那个确切的代码,它按预期工作。确保您有一个有效的互联网连接。

尝试在字符串上添加NSLog,看看它是否被调用。

NSLog(@"lat: %@, lng: %@", latDest1, lngDest1);

您是在模拟器或设备中运行它吗?

答案 2 :(得分:0)

块是iOS4.0以后Objective C的新功能。您可以将一个块想象为在同一功能块中工作的委托方法。对于任何委托方法,在完成地理编码工作时,取决于条件,块执行其中的代码的方式相同。您可以在Apple文档中阅读有关Block的更多信息,或阅读http://www.raywenderlich.com/9438/how-to-use-blocks-in-ios-5-tutorial-part-2

您还可以在GITHUB https://github.com/Mangesh20/geocoding

上查看我的存储库