【问题标题】:sort geo location points to my points Titanium将地理位置点排序到我的点 Titanium
【发布时间】:2015-05-20 17:13:57
【问题描述】:

我正在使用 Titanium Appcelerator Android 和 ios 创建一个应用程序。

我只有一个 lat lng 值数组,我想对最接近我指定的纬度和经度的数组进行排序。 我怎样才能得到这些值。请帮助。

【问题讨论】:

    标签: android ios titanium google-maps-api-2


    【解决方案1】:

    实现此目的的一种方法可能是: 1.使用Haversine公式计算您指定的位置(源纬度,源经度)与数组中的项目(目的地纬度,目的地经度)之间的距离。 2. 对数组中的每个项目重复步骤 1。 3. 对数组进行排序。

    这里有几个链接可能会派上用场:- Using the Haversine Formula in Javascript

    Sorting multiple JavaScript array

    【讨论】:

      【解决方案2】:

      你可以用它来计算两个经纬度之间的距离:

      function distance(lat1, lon1, lat2, lon2, unit) {
          var radlat1 = Math.PI * lat1/180
          var radlat2 = Math.PI * lat2/180
          var radlon1 = Math.PI * lon1/180
          var radlon2 = Math.PI * lon2/180
          var theta = lon1-lon2
          var radtheta = Math.PI * theta/180
          var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
      
          dist = Math.acos(dist)
          dist = dist * 180/Math.PI
          dist = dist * 60 * 1.1515
      
          if (unit=="K") { dist = dist * 1.609344 }
          if (unit=="N") { dist = dist * 0.8684 }
          return dist
      }
      
      //:::  Passed to function:                                                    :::
      //:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
      //:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
      //:::    unit = the unit you desire for results                               :::
      //:::           where: 'M' is statute miles (default)                         :::
      //:::                  'K' is kilometers                                      :::
      //:::                  'N' is nautical miles
      

      Source

      在推入数组中的所有距离后,您可以根据需要轻松排序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 2012-04-27
        • 2012-01-22
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多