我正在iOS应用程序上工作,在某个时候,我想获取用户的位置,并在地图上呈现她所有感兴趣的点,这个区域的中心是用户当前的位置,半径是恒定的。兴趣点以其坐标(纬度、经度)存储在数据库中。
我已经设法得到用户的位置。现在,我想弄清楚,如果某个坐标在那个区域,该如何计算。
我在想,我可以用这个方程计算出某一点离中心的距离:
d = sqrt((centre_latitude - point_latitude)^2 + (centre_longitude - point_longitude)^2)
其中d是那个点与圆心的距离。然后,我可以简单地比较d和radius。
我不知道这是否正确和有效的办法。我可以想象,如果我有数千点,这将是非常慢(查询数据库中的每一点,然后做数学)。
发布于 2015-09-01 17:04:13
你可以试试这个:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
float lon= // longitude you want to compare to your current postion
float lat=//your latitude you want to compare to your current position
float rad=//radius , if you give this 100m, then it checks the given points are within the 100m from you are not
CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:lat
longitude:lon];
CLLocation *lastLocation=[locations lastObject];
//display current lat and lon in text fields
currentLat.text=[NSString stringWithFormat:@"%f",lastLocation.coordinate.latitude];
currentLon.text=[NSString stringWithFormat:@"%f",lastLocation.coordinate.longitude];
CLLocationDistance distance = [lastLocation distanceFromLocation:centerLocation];
if (distance<=rad) {
// you are within the radius
}
CLLocationAccuracy accuracy = [lastLocation horizontalAccuracy];
if(accuracy <=10) { //accuracy in metres
[manager stopUpdatingLocation];
}
}发布于 2015-09-01 16:43:52
您可以使用Haversine公式。
我已经用Java为Android实现了它,也许它对你有帮助。
private static double calculateDistanceInMiles(Location StartP, Location EndP) {
//Haversine formula
//double Radius=6371; // to get distance in kms
double Radius=3963.1676; //to get distance in miles
double lat1 = StartP.getLatitude();
double lat2 = EndP.getLatitude();
double lon1 = StartP.getLongitude();
double lon2 = EndP.getLongitude();
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
} https://stackoverflow.com/questions/32336437
复制相似问题