2013-02-18 147 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