【问题标题】:Sorting by distance with Titanium alloy models钛合金型号按距离排序
【发布时间】:2016-10-02 10:42:30
【问题描述】:

我最近使用 Titanium and Alloy 开发了一个 android 应用程序。现在我正在尝试(第一次)使用比较器函数按距离对绑定的主干集合进行排序,但它不起作用。

comparator: function(game) {
    var lon1, lat1, lat2, lon2;
    if (Ti.Geolocation.locationServicesEnabled) {
        Ti.Geolocation.getCurrentPosition(function(e) {
            if (e.error) {
                Ti.API.error('Error:' + e.error);
                return 0;
            } else {
                Ti.API.info(e.coords);

                lon1 = e.coords.longitude;
                lat1 = e.coords.latitude;

                Titanium.Geolocation.forwardGeocoder(game.get("camp"), function(e) {
                    if (e.success) {
                        lat2 = e.latitude;
                        lon2 = e.longitude;

                        var R = 6371; // km
                        var dLat = (lat2 - lat1) * Math.PI / 180;
                        var dLon = (lon2 - lon1) * Math.PI / 180;
                        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                            Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
                            Math.sin(dLon / 2) * Math.sin(dLon / 2);
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                        var d = R * c;

                        console.log("KM: " + parseInt(d));

                        return parseInt(d);
                    } else {
                        console.log("Unable to find address");

                        return 0;
                    }
                });
            }
        });
    } else {
        console.log('please enable location services')

        return 0;
    }
}

在我的控制器中,我使用:

var games = Alloy.Collections.allGames;
games.sort();
games.fetch();

你能告诉我有什么问题吗?

【问题讨论】:

    标签: javascript backbone.js titanium titanium-alloy


    【解决方案1】:

    我既不使用钛合金也不使用合金,但我明白为什么您的比较器功能不起作用。

    主干集合的comparator 属性

    首先,要了解它为什么不起作用,您需要了解集合的comparator 属性是什么,有什么可用以及如何实现。

    集合的comparator 属性可以采用(至少)3 种类型的值。

    • 字符串形式的属性名称

      comparator: 'fieldName'
      
    • 一个sortBy 函数,它接受一个参数

      comparator: function(model) {
          // return a numeric or string value by which the model 
          // should be ordered relative to others.
          return Math.sin(model.get('myNumber'));
      }
      
    • 需要两个参数的sort 函数

      comparator: compare(modelA, modelB) {
          var field = 'myNumber',
              numA = modelA.get(field),
              numB = modelB.get(field);
          if (numA < numB) {
              return -1;
          }
          if (numA > numB) {
              return 1;
          }
          // a must be equal to b
          return 0;
      }
      

    为什么你的失败了?

    简短的回答:它只返回undefined0,具体取决于Ti.Geolocation.locationServicesEnabled 的值。

    您已经创建了一个复杂的函数来对您的模型进行排序,您在其中使用异步函数(getCurrentPositionforwardGeocoder)并将所有逻辑放在回调中,当集合已经完成排序时会评估这些逻辑。

    【讨论】:

    • 我明白了,很多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 2018-02-02
    • 2015-04-27
    相关资源
    最近更新 更多