【问题标题】:How to check if current coordinates are within a radius of other coordinates如何检查当前坐标是否在其他坐标的半径范围内
【发布时间】:2012-02-16 16:01:28
【问题描述】:

我有一个固定位置的经纬度。我想检查另一个位置(纬度和经度)是否足够接近(50-100 米)到固定位置。我使用 iPhone 获取当前位置。

【问题讨论】:

    标签: iphone objective-c ios google-maps google-maps-api-3


    【解决方案1】:

    以下是在 Swift 中使用 CoreLocation 的方法。您只需在 CLLocation 类型的位置上使用 .distance(from: ) 方法比较 2 个不同的位置。确保两个位置的类型均为CLLocation

    import CoreLocation
    
    let someOtherLocation: CLLocation = CLLocation(latitude: someOtherLat,
                                                   longitude: someOtherLon)
    
    guard let usersCurrentLocation: CLLocation = locationManager.location else { return }
    
                                                           // **the distance(from: ) is right here **
    let distanceInMeters: CLLocationDistance = usersCurrentLocation.distance(from: someOtherLocation)
    
    if distanceInMeters < 100 {
    
        // this user is pretty much in the same area as the otherLocation
    } else {
    
       // this user is at least over 100 meters outside the otherLocation
    }
    

    这不是必需的,但也许您需要保存 lats 和 lons 以供以后进行另一个比较。我知道我需要他们

    let usersCurrentLat: CLLocationDegrees = currentLocation.coordinate.latitude // not neccessary but this is how you get the lat
    let usersCurrentLon: CLLocationDegrees = currentLocation.coordinate.longitude // not neccessary but this is how you get the lon
    
    let someOtherLocationLat: CLLocationDegrees = someOtherLocationLocation.coordinate.latitude // not neccessary but this is how you get the lat
    let someOtherLocationLon: CLLocationDegrees = someOtherLocationtLocation.coordinate.longitude // not neccessary but this is how you get the lon
    
    
    let usersLocation: CLLocation = CLLocation(latitude: usersCurrentLat,
                                               longitude: usersCurrentLon)
    
    let otherLocation: CLLocation = CLLocation(latitude: someOtherLocationLat,
                                               longitude: someOtherLocationLon)
    

    【讨论】:

      【解决方案2】:
      distanceFromCurrentLocation = [userLocation distanceFromLocation:destinationlocation]/convertToKiloMeter;
      if(distanceFromCurrentLocation < 100 && distanceFromLocation > .500)
      {
          NSLog(@"Yeah, this place is inside my circle");
      }
      else
      {
          NSLog(@"Oops!! its too far");
      }
      

      这会找到空中距离,或者我们可以说,只是直线距离。
      希望您不是在寻找道路距离。

      【讨论】:

        【解决方案3】:
        func distance(from location: CLLocation) -> CLLocationDistance
        

        Documentation

        【讨论】:

          【解决方案4】:

          CLLocation的方法– distanceFromLocation:正是你所需要的。

          【讨论】:

            【解决方案5】:

            添加到 Deepukjayan 答案,在使用他的答案之前使用 CLLocation 定义参考:

            CLLocation *montreal = [[CLLocation alloc] initWithLatitude:45.521731 longitude:-73.628679];
            

            【讨论】:

              【解决方案6】:

              虽然我赞成空堆栈答案.. 如果您需要更多帮助,这里是代码..

              成为 CLLocationManagerDelegate,然后在您的实现类中。

               - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
              
              
                          int distance = [newLocation distanceFromLocation:oldLocation];
                          if(distance >50 && distance <100)
                          {
              
                          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Distance"
                                                                          message:[NSString stringWithFormat:@"%i meters",distance]
                                                                         delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                                otherButtonTitles:nil];
                          [alert show];
                          [alert release];
                          }
                      }
                  }
              

              【讨论】:

                【解决方案7】:
                CLLocation *location;// = init...;
                double distance = [location distanceFromLocation:otherLoc]; //in meters
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-16
                  • 1970-01-01
                  • 2013-10-02
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-07-15
                  相关资源
                  最近更新 更多