【问题标题】:Calculating Distance between two coordinates using CLLocation使用 CLLocation 计算两个坐标之间的距离
【发布时间】:2014-05-12 18:26:49
【问题描述】:

我正在使用CLLocationDistance 获取两点之间的距离,但在传递我当前的位置时出现错误。

CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude];
CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lat"] doubleValue] longitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lon"] doubleValue]];

//Here the current location gives me an error "Initializing cllocation with an expression incompatible format"
CLLocationDistance *itemDist = [itemLoc distanceFromLocation:current];
NSLog(@"Distance: %@", itemDist);

【问题讨论】:

    标签: ios cocoa-touch ios7 cllocation


    【解决方案1】:

    你得到的错误实际上是:

    使用不兼容类型“CLLocationDistance”(又名“double”)的表达式初始化“CLLocationDistance *”(又名“double *”)

    它的意思是您正在将 itemDist(您已声明为 CLLocationDistance *)初始化为返回 CLLocationDistance(注意没有星号)的东西。

    CLLocationDistance 不是对象。
    它只是一个原始类型(特别是double -- 参见Core Location Data Types Reference)。


    因此,不要将itemDist 声明为指向CLLocationDistance指针,只需将其声明为CLLocationDistance(无星号):

    CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];
    

    您还需要更新 NSLog 以期望 double 而不是 object 否则它将在运行时崩溃:

    NSLog(@"Distance: %f", itemDist);
    

    【讨论】:

      【解决方案2】:

      是swift 2.0是这样的:

      let userLocation:CLLocation = CLLocation(latitude: 11.11, longitude: 22.22)
      let priceLocation:CLLocation = CLLocation(latitude: 33.33, longitude: 44.44)
      let meters:CLLocationDistance = userLocation.distanceFromLocation(priceLocation)
      

      【讨论】:

        【解决方案3】:

        以下代码将为您工作 在视图中加载

        //************************** GEtting USer's latitude/ Longitude ********************
        
        
        
        
          self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.delegate = self;
                [self.locationManager startUpdatingLocation];
        //************************** GEtting USer's latitude/ Longitude ********************
        

        编译指示标记 - CLLocationManagerDelegate

        - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
        {
            NSLog(@"didFailWithError: %@", error);
            UIAlertView *errorAlert = [[UIAlertView alloc]
                                       initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [errorAlert show];
        }
        - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
        {
            NSLog(@"didUpdateToLocation: %@", newLocation);
            CLLocation *currentLocation = newLocation;
            if (currentLocation != nil) {        
                CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.longitude longitude:currentLocation.coordinate.latitude];
                CLLocation *locB = [[CLLocation alloc] initWithLatitude:11.111111111111111 longitude:11.1111111111111111]; 
                User_Vanue_Distance = [locA distanceFromLocation:locB];
                NSLog(@"THIS IS THE DISTANCE BT TWO POINTS +++++++++++++++++++++%f",User_Vanue_Distance);  
            }
        }
        

        您需要将以下键放在 info.plist 中,在 ios 8 中要访问位置,我们需要将以下键放在 info.plist 中 1.NSLocationWhenInUseUsageDescription : Location required 2.NSLocationAlwaysUsageDescription Needed : Location Needed

        这对您有用,但您需要添加的一件事是添加对 ios8 的检查。否则这将在 ios 7 上崩溃

        // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
        
        
          if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
        

        希望这对你有用。 :)

        【讨论】:

          【解决方案4】:

          从两个不同的纬度和经度中找到以 KM 为单位的距离

          let userLocation:CLLocation = CLLocation(latitude: 23.0320, longitude: 72.5250)
          let priceLocation:CLLocation = CLLocation(latitude: 23.0283, longitude: 72.5067)
          let distance = String(format: "%.2f km", userLocation.distanceFromLocation(priceLocation)/1000)
          print("Distance is KM is:: \(distance)")
          

          【讨论】:

            猜你喜欢
            • 2016-07-19
            • 1970-01-01
            • 2020-02-12
            • 2018-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多