How to calculate distance from different markers in a map and then pick up the least one

后端 未结 9 1020
野趣味
野趣味 2020-12-05 08:53

I have to get distance from different markers on the map to the current location of the device and the pick up the shortest one. I have the lat and long for the markers and

9条回答
  •  一向
    一向 (楼主)
    2020-12-05 09:16

    An efficient way to search for the smallest distance between a single point (that may change frequently), and a large set of points, in two dimensions is to use a QuadTree. There is a cost to initially build the QuadTree (i.e., add your marker locations to the data structure), so you only want to do this once (or as infrequently as possible). But, once constructed, searches for the closest point will typically be faster than a brute force comparison against all points in the large set.

    BBN's OpenMap project has an open-source QuadTree Java implementation that I believe should work on Android that has a get(float lat, float lon) method to return the closest point.

    Google's android-maps-utils library also has an open-source implementation of a QuadTree intended to run on Android, but as it is currently written it only supports a search(Bounds bounds) operation to return a set of points in a given bounding box, and not the point closest to an input point. But, it could be modified to perform the closest point search.

    If you have a relatively small number of points (70-80 may be sufficiently small), then in real-world performance a brute-force comparison may execute in a similar amount of time to the QuadTree solution. But, it also depends on how frequently you intended on re-calculating the closest point - if frequent, a QuadTree may be a better choice.

提交回复
热议问题