首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >区域地质点

区域地质点
EN

Stack Overflow用户
提问于 2015-09-01 16:17:54
回答 2查看 77关注 0票数 0

我正在iOS应用程序上工作,在某个时候,我想获取用户的位置,并在地图上呈现她所有感兴趣的点,这个区域的中心是用户当前的位置,半径是恒定的。兴趣点以其坐标(纬度、经度)存储在数据库中。

我已经设法得到用户的位置。现在,我想弄清楚,如果某个坐标在那个区域,该如何计算。

我在想,我可以用这个方程计算出某一点离中心的距离:

d = sqrt((centre_latitude - point_latitude)^2 + (centre_longitude - point_longitude)^2)

其中d是那个点与圆心的距离。然后,我可以简单地比较d和radius。

我不知道这是否正确和有效的办法。我可以想象,如果我有数千点,这将是非常慢(查询数据库中的每一点,然后做数学)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-01 17:04:13

你可以试试这个:

代码语言:javascript
复制
-(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];
}
}
票数 0
EN

Stack Overflow用户

发布于 2015-09-01 16:43:52

您可以使用Haversine公式

我已经用Java为Android实现了它,也许它对你有帮助。

代码语言:javascript
复制
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;  
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32336437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档