【问题标题】:Geo points in area区域内的地理点
【发布时间】:2015-09-01 16:17:54
【问题描述】:

我正在开发 iOS 应用程序,有时我想获取用户的位置并在地图上显示她的所有兴趣点,这些兴趣点位于圆形区域内,该区域的中心是用户的当前位置,半径是恒定的。兴趣点及其坐标(纬度、经度)存储在数据库中。

我已经设法获取用户的位置。现在我想弄清楚如何计算某些坐标是否在该区域内。

我在想我可以使用这个等式计算某个点到中心的距离:

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

d 是该点到圆心的距离。然后我可以简单地将d 与半径进行比较。

我不确定这是否正确且有效的方法。我可以想象,如果我有数千个点,这会非常慢(为每个点查询数据库,然后进行数学运算)。

【问题讨论】:

    标签: ios database geolocation


    【解决方案1】:

    你可以试试这个:

    -(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];
    }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用Haversine formula

      我已经用 Java for 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;  
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2018-10-27
        • 2013-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多