Ios 区域内的地理点
我正在使用iOS应用程序,在某个点上,我想获得用户的位置,并在地图上显示她的所有兴趣点,这些兴趣点位于圆形区域内,该区域的中心是用户的当前位置,半径是恒定的。兴趣点及其坐标(纬度、经度)存储在数据库中 我已经设法获得了用户的位置。现在我想知道如何计算某个坐标是否在那个区域 我在想,我可以用这个等式计算某点到中心的距离:Ios 区域内的地理点,ios,database,geolocation,Ios,Database,Geolocation,我正在使用iOS应用程序,在某个点上,我想获得用户的位置,并在地图上显示她的所有兴趣点,这些兴趣点位于圆形区域内,该区域的中心是用户的当前位置,半径是恒定的。兴趣点及其坐标(纬度、经度)存储在数据库中 我已经设法获得了用户的位置。现在我想知道如何计算某个坐标是否在那个区域 我在想,我可以用这个等式计算某点到中心的距离: d=sqrt((中心纬度-点纬度)^2+(中心经度-点经度)^2) 其中d是该点到圆心的距离。然后我可以简单地将d与半径进行比较 我不确定这是否正确,是否也是有效的方法。我可以想
d=sqrt((中心纬度-点纬度)^2+(中心经度-点经度)^2)
其中d
是该点到圆心的距离。然后我可以简单地将d
与半径进行比较
我不确定这是否正确,是否也是有效的方法。我可以想象,如果我有数千个点,这会非常慢(查询每个点的数据库,然后进行计算)。您可以使用
我已经用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;
}
您可以尝试以下方法:
-(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];
}
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
float lon=//要与当前位置进行比较的经度
float lat=//要与当前位置进行比较的纬度
float rad=//半径,如果你给出100米,那么它会检查给定的点是否在距离你100米的范围内
CLLocation*centerLocation=[[CLLocation alloc]INITWITHLATIONE:lat
经度:lon];
CLLocation*lastLocation=[locations lastObject];
//在文本字段中显示当前纬度和经度
currentLat.text=[NSString stringWithFormat:@“%f”,lastLocation.coordinate.latitude];
currentLon.text=[NSString stringWithFormat:@“%f”,lastLocation.coordinate.longitude];
CLLocationDistance=[lastLocation distanceFromLocation:centerLocation];
如果(距离)