代码之家  ›  专栏  ›  技术社区  ›  Bjinse

什么是最快的方法来排序很多地点的距离?

  •  5
  • Bjinse  · 技术社区  · 16 年前

    我要根据与当前位置的距离对许多位置(航路点)进行排序。 当然,当前位置是一个移动目标,因此对于每个位置更新,需要为每个位置重新计算距离。但只对附近的位置进行重新计算就足够了。

    我目前使用核心数据,并将到当前位置的距离存储为表中的一个属性(但只有当更新被更改时),该属性来自configureCell:atindexpath:方法。 这是可行的,但当核心数据自动更新所有距离时,应用程序没有响应。这适用于250个地点,但适用于5000个地点。 我需要它在10000个地点工作,尽管我可能只需要1000个最近的地点。

    我还没有尝试的想法: 将所有的距离存储在一个单独的内存数组中,除了记录ID和距离之外什么都没有。然后按距离对数组进行排序。问题是我不能使用FetchedResultsController,因为数据库中没有排序字段。

    使用谓词根据纬度和经度筛选位置。然后只显示过滤后的位置。

    在单独的线程中重新计算距离。

    这些想法似乎都不容易尝试。

    有没有人对我的想法有建议,不同的想法,不同的想法?

    4 回复  |  直到 9 年前
        1
  •  2
  •   Bjinse    14 年前

    我的最终解决方案如下: 我选择1度经纬度范围内的所有航路点(通常约1000个航路点),然后计算并存储到表格中当前位置的距离。然后我就可以根据距离分类了。最慢的事情是保存核心数据。但是在排序(和提取)之后,我只需取消更改。储蓄占了全部储蓄的90%以上,所以效果相当好。

        2
  •  1
  •   Community Mohan Dere    9 年前

    听起来你需要把你的位置转换成希尔伯特曲线上的位置-然后点“接近”你是一个简单的减法?

    Mapping N-dimensional value to a point on Hilbert curve

    我不能说我对这项技术了如指掌,但那正是我要找的地方

        3
  •  1
  •   outis    16 年前

    如果重要的是顺序(而不是精确的距离),您可以在移动窗口中对路径点序列切片进行排序(即对项目进行排序) 我 到 I+N 在哪里 我 变化)。从航路点序列开始。排序 n 项目(n=10是开始的好地方)。如果有任何项目改变了位置,将窗口向前移动 N/2 (用不同的偏移量或算法来选择偏移量)并重复。根据路标在当前位置附近的密集程度,我预计这将仅在几次排序后停止。

    请注意,我没有考虑这么长时间来说明这是否真的有效。

    在您提到的三个选项中,我最喜欢使用线程。这是处理非响应用户界面的经典方法,当它被大量计算阻塞时。

        4
  •  0
  •   Chris Garrett    9 年前

    我将位置存储在数据模型中作为纬度/经度坐标。然后我编写了一些助手扩展来查找lat/lon坐标的半矩形区域,并以此进行查询。这是我使用的代码。我知道这个问题是针对客观C的,但是这个问题已经过时了,而且现在大多数人都在寻找一个快速的答案。

    斯威夫特3

       extension CLLocationDistance {
            var feet: Double {
                return self * 3.28084
            }
            var miles: Double {
                return self.feet / 5280.0
            }
        }
    
        extension CLLocationDegrees {
            static var north: CLLocationDegrees {
                return 90.0
            }
            static var south: CLLocationDegrees {
                return -90.0
            }
            static var east: CLLocationDegrees {
                return 180.0
            }
            static var west: CLLocationDegrees {
                return -180.0
            }
    
            var radians: Double {
                return Double.pi * self / 180.0
            }
        }
    
        extension CLLocationCoordinate2D {
            static var origin: CLLocationCoordinate2D {
                return CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
            }
    
            static var northPole: CLLocationCoordinate2D {
                return CLLocationCoordinate2D(latitude: 90.0, longitude: 0.0)
            }
    
            static var southPole: CLLocationCoordinate2D {
                return CLLocationCoordinate2D(latitude: 90.0, longitude: 0.0)
            }
    
            var metersPerDegreeLatitude: CLLocationDistance {
                return 111319.4907932736
            }
            var metersPerDegreeLongitude: CLLocationDistance {
                return max(0.0, cos(self.latitude.radians) * self.metersPerDegreeLatitude)
            }
        }
    
        extension CLCircularRegion {
            var northernmostLatitude: CLLocationDegrees {
                let longitude = self.center.latitude + self.radius / self.center.metersPerDegreeLatitude
                return min(longitude, .north)
            }
    
            var southernmostLatitude: CLLocationDegrees {
                let longitude = self.center.latitude - self.radius / self.center.metersPerDegreeLatitude
                return max(longitude, .south)
            }
    
            var easternmostLongitude: CLLocationDegrees {
                guard self.northernmostLatitude <= .north else {
                    return .east
                }
                guard self.southernmostLatitude >= .south else {
                    return .east
                }
                return min(.east, self.center.longitude + self.radius / (self.center.metersPerDegreeLongitude + 0.0001))
            }
    
            var westernmostLongitude: CLLocationDegrees {
                guard self.northernmostLatitude <= .north else {
                    return .west
                }
                guard self.southernmostLatitude >= .south else {
                    return .west
                }
                return max(.west, self.center.longitude - self.radius / (self.center.metersPerDegreeLongitude + 0.0001))
            }
    
            func buildPredicate(latitudeName: String = "latitude", longitudeName: String = "longitude") -> NSPredicate {
                let args = [self.southernmostLatitude, self.northernmostLatitude, self.westernmostLongitude, self.easternmostLongitude]
                return NSPredicate(format: "\(latitudeName) >= %@ && \(latitudeName) <= %@ && \(longitudeName) >= %@ && \(longitudeName) <= %@", argumentArray: args)
            }
        }