Find nearest by lat/long with Core Data

前端 未结 3 1328
鱼传尺愫
鱼传尺愫 2021-02-10 02:23

I have an iPhone app with a Core Data database containing a list of locations, each with lat/long coordinates. How can I search for say the nearest 10 to my current location?

3条回答
  •  盖世英雄少女心
    2021-02-10 02:52

    You need the Haversine formula for distance between lat / long pairs. You can have a calculated field for each object which returns it's distance from a set point, then order by this distance to get the nearest -> farthest.

    See this article on Haversine

    And here is an Objective-c version...

    Objective-c Haversine

提交回复
热议问题