2013-02-18 109 views
0

我想根据距离对列表中的位置进行排序(显示在列表中)。 我已经有了一个可以工作的代码,但是因为我对整个mvc的事情都很陌生,所以我不太确定把它放在哪里才能使它工作。sencha触摸按距离排序列表

也许有人可以帮助我:

var geocoder = new google.maps.Geocoder(); 
var geo = Ext.create('Ext.util.Geolocation',{ 
autoUpdate: false, 
listeners: { 
    locationupdate:{ 
     scope: this, 
     fn: function(geo){ 
      var haversindeDistance = function(lat1,lon1,lat2,lon2){ 
       if(typeof(Number.prototype.toRad)=="undefined"){ 
         Number.prototype.toRad = function(){ 
          return this * Math.PI/180; 
         } 
       } 
       var R = 6371; //km 
       var dLat = (lat2-lat1).toRad(); 
       var dLon = (lon2-lon1).toRad(); 
       var lat1 = lat1.toRad(); 
       var lat2 = lat2.toRad(); 

       var a = Math.sin(dLat/2)*Math.sin(dLat/2)+ 
         Math.sin(dLong/2)*Math.sin(dLon/2)*Math.cos(lat1)*Math.cos(lat2); 
       var c = 2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); 
       var d = R*c; 
       // KM or MIles 
       //return d*0.621371192; //MIles 
       return d; 
      }; 
     var store = Ext.getStore('locationsstore'); 
     store.suspendEvents(true); 
     store.each(function(location){ 
      var lat2 = parseFloat(location.get(geocoder.geocode({ 'address': sAddress}, function(results, status) { })))||0; 
      var lon2 = parseFloat(location.get(geocoder.geocode({ 'address': sAddress}, function(results, status) { })))||0; 
      //var lat2 = parseFloat(location.get('lat'))||0;//try to put geocode on this ish 
      //var lon2 = parseFloat(location.get('lon'))||0; 
      if(lat2 && lon2){ 
       var distance = haversineDistance(geo.getLatitude(),geo.getLongitude(),lat2,lon2); 
       location.set('distance',distance); 
      } 
     }, this); 
     store.resumeEvents(); 
     store.filter('distance',/\d/); 
     store.sort('distance');//check if it is not done or can not be done somewhere else 
     list.setMasked(false); 
    } 
}, 
    locationerror:{ 
     scope: this, 
     fn:function(geo,bTimeout,bPermissionDenied,bLocationUnavailable,message){ 
     console.log([geo,bTimeout,bPermissionDenied,bLocationUnavailable,message]); 
     if(bTimeout){ 
      Ext.Msg.alert('Timed out getting your location.'); 
     }else{ 
      Ext.Msg.alert('Error getting location. Please make sure location services are enabled on your Device.'); 
     } 
     list.setMask(false); 
     } 
    } 
    } 
}); 
geo.updateLocation(); 

回答

0

我发现Solution.Just添加一个监听到导航/列表视图应该这样做。我在Controller中太多了,所以我决定把它直接放到View中。

+1

你能告诉我们你添加了什么样的听众吗?对于那些正在寻找类似东西的人可能会有所帮助 – ThinkFloyd 2013-02-24 03:53:31

+0

我在控制器中使用了整个代码。这个问题是通过将它移动到列表视图来解决的,我想将它排序为:paint:function(){^^ code – 2013-05-28 10:06:28