使用地理编码器获取纬度和经度值

时间:2014-03-20 04:55:43

标签: ios iphone google-maps ios7 google-geocoder

我编写了以下代码,用于从地址获取纬度和经度值。我有2个地址字段From和To。

-(void)getLatLong:(NSString *)string
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
    if([placemarks count]) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
        if([string isEqualToString:self.from.text])
        {
            fromLat = coordinate.latitude;
            fromLng = coordinate.longitude;

        }
        else if([string  isEqualToString:self.to.text])
        {
            toLat = coordinate.latitude;
            toLng = coordinate.longitude;
        }
    }
}];
}  
- (void)display
{
    NSLog(@"%f",fromLat);
    NSLog(@"%f",fromLng);
    NSLog(@"%f",toLat);
    NSLog(@"%f",toLng);
}
- (IBAction)getLatLongs:(id)sender
{
    [self getLatLong:self.from.text];
    [self getLatLong:self.to.text];
    [self display];
}

首次单击“提交”按钮后,输出为:

 2014-03-20 10:15:28.178 Application[746:70b] 0.000000  
 2014-03-20 10:15:28.179 Application[746:70b] 0.000000  
 2014-03-20 10:15:28.180 Application[746:70b] 0.000000  
 2014-03-20 10:15:28.181 Application[746:70b] 0.000000   
 2014-03-20 10:15:29.019 Application[746:70b] coordinate = (17.387337, 78.480835)  
 2014-03-20 10:15:29.134 Application[746:70b] coordinate = (16.516667, 80.616667) 

在第二次单击“提交”按钮后,输出为:

 2014-03-20 10:15:28.178 Application[746:70b] 17.387337  
 2014-03-20 10:15:28.179 Application[746:70b] 78.480835 
 2014-03-20 10:15:28.180 Application[746:70b] 16.516667  
 2014-03-20 10:15:28.181 Application[746:70b] 80.616667  
 2014-03-20 10:15:29.019 Application[746:70b] coordinate = (17.387337, 78.480835)  
 2014-03-20 10:15:29.134 Application[746:70b] coordinate = (16.516667, 80.616667) 

我想知道为什么如果我在获取LaltLong方法之后调用显示方法,它是第一次全部归零。

2 个答案:

答案 0 :(得分:4)

致电[self getLatLong:self.from.text];

它调用[geocoder geocodeAddressString:string completionHandler:^{}];即GCD。哪个运行在不同的线程上。

然后你立即调用[self display];,直到GCD没有收到值。这就是为什么你第一次得到 0 。 下次调用[self display];时,值已经存在,因此按原样打印。

-(void)display和GCD内部使用断点以获得更多想法。

尝试在GCD中调用[self display];

-(void)getLatLong:(NSString *)string
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
    if([placemarks count]) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
        if([string isEqualToString:self.from.text])
        {
            fromLat = coordinate.latitude;
            fromLng = coordinate.longitude;
            [self display];
        }
        else if([string  isEqualToString:self.to.text])
        {
            toLat = coordinate.latitude;
            toLng = coordinate.longitude;
            [self display];
        }
    }
    }];
} 

- (void)display
{
   NSLog(@"%f",fromLat);
   NSLog(@"%f",fromLng);
   NSLog(@"%f",toLat);
   NSLog(@"%f",toLng);
}

- (IBAction)getLatLongs:(id)sender
{
   [self getLatLong:self.from.text];
   [self getLatLong:self.to.text];
}

答案 1 :(得分:0)

我解决了我的问题就像这样工作正常

-(void)getLatLong:(NSString *)string
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
    CLPlacemark *placemark = [placemarks objectAtIndex:0];
    CLLocation *location = placemark.location;
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
    if([string isEqualToString:self.from.text])
    {
        fromLat = coordinate.latitude;
        fromLng = coordinate.longitude;

    }
    else if([string  isEqualToString:self.to.text])
    {
        toLat = coordinate.latitude;
        toLng = coordinate.longitude;
    }
}
}];
}  
- (void)display
{
 NSLog(@"%f",fromLat);
 NSLog(@"%f",fromLng);
 NSLog(@"%f",toLat);
 NSLog(@"%f",toLng);
}
- (IBAction)getLatLongs:(id)sender
{
 [self getLatLong:self.from.text];
 [self getLatLong:self.to.text];
  int64_t delayInSeconds = 1.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

       [self display];
    });
}

}