使用纬度和经度按人搜索附近 IOS

Search near by people by using Latitude & longitude IOS

我正在 iOS 开发一个聊天应用程序。用户可以在其中搜索半径为 1 公里的附近的人。 在注册时我存储了每个用户的纬度和经度。 现在告诉我如何在 1 公里半径内搜索用户附近的人??

非常感谢

您可以使用此方法比较两个位置

CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];

或者从后端(PHP 开发人员)计算以找到距离,因为基本上他们只存储所有经纬度。(我认为)

我的解决方案是直接使用 Radios.Make 以下步骤无法搜索用户。

  1. 使用网络服务将纬度、经度和无线电信息发送到您的后端服务器。

  2. 为您的 Web 服务开发人员查询并让附近的用户使用您的信息。

请参考以下link:

Geo Distance Search with MySQL

您可以使用Haversine公式计算当前纬度、经度与目标纬度、经度之间的合适距离。

define DEG2RAD(degrees) (degrees * 0.01745327)

double currentLatitude = 40.727181; // 登录用户的当前纬度
double currentLongitude = -73.986786; // 登录用户当前经度
double destinationLatitude = 42.896578; //目标用户纬度
double destinationLongitude = -75.784537; // 目标用户的经度

//将得到的经纬度转换为RADS。
double currentLatitudeRad = DEG2RAD(currentLatitude);
double currentLongitudeRad = DEG2RAD(currentLongitude);
double destinationLatitudeRad = DEG2RAD(destinationLatitude);
double destinationLongitudeRad = DEG2RAD(destinationLongitude);

//Haversine 公式.
distance = acos(sin(currentLatitudeRad) * sin(destinationLatitudeRad) + cos(currentLatitudeRad) * cos(destinationLatitudeRad) * cos(currentLongitudeRad - destinationLongitudeRad)) * 6880.1295896;

此处获取的距离以公里为单位。