通过距离位置计算MAX和MIN纬度和经度 - 目标C. [英] Calculating MAX and MIN Latitude and Longitude with distance from Location - Objective C

查看:108
本文介绍了通过距离位置计算MAX和MIN纬度和经度 - 目标C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从具有一定距离的位置计算MAX和MIN纬度和经度值。

I need to compute MAX and MIN Latitude and Longitude values from a location with certain distance.

我有数千个位置存储在CoreData中,我想显示只有距用户位置5公里范围内的那些。

I have thousands of locations stored in CoreData, and I want to show only the ones within 5km from users location.

我该如何处理这个问题?

How can I approach this problem?

推荐答案

这是一个可能的解决方案:

Here's a possible solution:


  1. 将度数转换为Radians的宏

  1. macros to convert Degrees to Radians

#define deg2rad(degrees) ((degrees) / 180.0 M_PI)


  • 宏来保持我的搜索距离

  • macros to hold my searching distance

    #define searchDistance 5.00 //float value in KM
    


  • 设置最小和最大纬度,经度值

  • set the minimum and maximum Latitude, Longitude values

    float minLat = userLocation.coordinate.latitude - (searchDistance / 69);
    float maxLat = userLocation.coordinate.latitude + (searchDistance / 69);
    float minLon = userLocation.coordinate.latitude - searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69);
    float maxLon = userLocation.coordinate.longitude + searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69);
    


  • 按如下方式创建谓词

  • create predicate as follows

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude <= %f AND latitude >= %f AND longitude <= %f AND longitude >= %f", maxLat, minLat, maxLon, minLon];
    


  • 这将在<$ c周围创建一个正方形$ c> userLocation 并检查给定位置是否落入其坐标。

    This will create a square around userLocation and check if a given location falls into its coordinates.


    更新:Swift 2. *实现

    Update: Swift 2.* implementation

    首先创建一个计算度数到弧度的函数

    First create a function to compute degrees to radians

    func deg2rad(degrees:Double) -> Double{
        return degrees * M_PI / 180
    }
    

    计算并创建最小值和最大纬度经度

    Compute and create minimum and maximum Latitude and Longitude values

    let searchDistance:Double =  5.00 //float value in KM    
    
    let minLat = userLocation.coordinate.latitude - (searchDistance / 69)
    let maxLat = userLocation.coordinate.latitude + (searchDistance / 69)
    
    let minLon = userLocation.coordinate.longitude - searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69)
    let maxLon = userLocation.coordinate.longitude + searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69)
    

    上次创建 NSPredicate ,查询位置的 CoreData 。在我的情况下,我正在查询值纬度经度但是你应该更改它以匹配你的 CoreData object

    Last create NSPredicate to query CoreData for locations. In my case I am querying for values latitude and longitude but you should change this to match your CoreData object

    let predicate = NSPredicate(format: "latitude <= \(maxLat) AND latitude >= \(minLat) AND longitude <= \(maxLon) AND longitude >= \(minLon)")
    

    这篇关于通过距离位置计算MAX和MIN纬度和经度 - 目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆