【问题标题】:Getting Latitude and Longitude Values Using Geocoder使用地理编码器获取纬度和经度值
【发布时间】:2014-03-20 04:55:43
【问题描述】:

我编写以下代码从地址获取纬度和经度值。我有 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 方法之后调用显示方法,我想知道为什么它第一次得到全零。

【问题讨论】:

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


    【解决方案1】:

    当您致电[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];
    }
    

    【讨论】:

    • 感谢您的回答。我使用“Dispatch_Time”解决了我的问题
    【解决方案2】:

    我这样解决了我的问题,效果很好

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

    【讨论】:

    • 这在某些情况下可能会产生同样的问题。如果您无法在 1.5 秒内获得值怎么办?或者如果你得到 0.5 秒的值,你就是在浪费你的 1 秒;)
    • @RahulV.Mane 的主要目的是我想获取那些经纬度值并在以后使用它们。你能不能再给我一个建议。
    • 如果是这种情况,那么您不必担心打印值。您可以在接收值的 GCD 中写入 NSLog。我可以看到fromLat, fromLng, toLat, toLng 是全局变量。所以如果关注的是“何时打印值?”那么它应该在收到时打印。 IE。在if/else 块内:)
    • @RahulV.Mane 不,如果我在使用时编写 NSLog,则它不起作用,它首先显示全零,第二次显示 lat long 值。
    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多