如何检查当前坐标是否在其他坐标半径范围内

6

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

7个回答

14

7
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

以下是使用CoreLocation在Swift中执行此操作的方法。只需在类型为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
}

这并不是必需的,但也许您需要将经纬度保存起来,以便稍后进行其他比较。我知道我需要它们。

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)

1

在Deepukjayan的回答基础上,使用CLLocation来定义一个参考点,然后再使用他的方法:

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

1
CLLocation *location;// = init...;
double distance = [location distanceFromLocation:otherLoc]; //in meters

1

虽然我已经为Empty Stack的答案点了赞,但如果你需要更多帮助,这里是代码:

成为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];
            }
        }
    }

0
func distance(from location: CLLocation) -> CLLocationDistance

文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接