简体   繁体   English

计算MAP中两点之间的距离(以Km为单位)

[英]calculating the distance (in Km) between two points in the MAP

i arrive to the most critic step in my application, my data base have a list of service stations (with their longitude and latitude coordinates) and i have to send the longitude and the latitude of the user to a webservice which will try to find which stations are around the user with a radius of 5 KM. 我到达应用程序中最批评的一步,我的数据库有一个服务站列表(带有经度和纬度坐标) ,我必须将用户的经度和纬度发送到一个web服务,它将尝试找到哪个站点位于用户周围,半径为5公里。 Is there any pre-built algorithm that may help me, any suggestions, links, or whatever you think can help me is welcome, thx in advance :) 是否有任何预先建立的算法可以帮助我,任何建议,链接,或任何你认为可以帮助我的东西是受欢迎的,thx提前:)

You may use the CoreLocation framework to do so : Initializes one CLLocation object for each of your service stations with latitude and longitude 您可以使用CoreLocation框架执行此操作:使用纬度和经度为每个服务站初始化一个CLLocation对象

CLLocation *loc = [[CLLocation alloc] initWithLatitude:serviceStation.latitude longitude:serviceStation.longitude];

Once you did that you can use the distanceFromLocation instance method of CLLocation : 完成后,您可以使用CLLocation的distanceFromLocation实例方法:

CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

Distance will be your distance between both point in meters (CLLocationDistance is a double). 距离将是您以米为单位的距离(CLLocationDistance是双倍)。 Then you'll simply have to divide it by 1000 to get it in km ;-) 然后你只需要将它除以1000即可得到它;-)

Edit : 编辑:

As you have your database on your server it will be more efficient to compute the distance in a web service. 由于您的服务器上有数据库,因此计算Web服务中的距离会更有效。 As there is no "inverse method" of distanceFromLocation which would allow you to give a distance and retrieve the min and max latitudes and longitudes associated to the current user location, you need to perform the computation on the server side. 由于distanceFromLocation没有“反向方法”可以让您给出距离并检索与当前用户位置相关的最小和最大纬度和经度,因此您需要在服务器端执行计算。

So the solution would be to send the user's current location (latitude and longitude) to your web service, make him compute the max and min latitude and longitude associated to your distance (a square will be easier to compute and implement than a circle for the service stations). 因此,解决方案是将用户的当前位置(纬度和经度)发送到您的Web服务,让他计算与您的距离相关的最大和最小纬度和经度(正方形将比圆形更容易计算和实施服务站)。 You have some resources to perform those calculations here : Haversine formula 您有一些资源可以在这里执行这些计算: Haversine公式

看看“ 在纬度/经度点之间计算距离,方位和更多 ”它提供了除了客观c代码之外你需要的一切:算法,工作javascript代码,要测试的表格以及关于主题的大量信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM