2012-05-12 34 views
1

所以我的问题似乎很简单,但对于上帝的爱我不知道。所以我要求你的帮助。我在包含商店的移动应用程序中有一个简单的列表。我想通过距离地图中心的距离来缩短它们。Flex 4.6自定义排序清单

这似乎是我需要一个自定义的排序功能,但我不知道必须在它做什么。

我使用

testDist.setLatLng(propertyList.selectedItem.lat,propertyList.selectedItem.lng); 

dist.text=""+GeodesicCalculatorUtil.calculateGeodesicDistance(FlexGlobals.topLevelApplication.currentLatLng2,testDist,DistanceUnits.KILOMETERS) 

以获取店铺的距离,我必须把它与下一个比较。但我无法弄清楚如何在比较函数中做到这一点。如果有人能帮助我,我会很高兴。

+0

什么是您的数据类型的列表?一个ArrayCollection?你试图做什么来分类?您是否阅读了这些文档:http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html – JeffryHouser

+0

我从php服务获取数据,我的列表数据提供者定义为从这里使用lat和lng获得我需要用于排序的距离。 – leossmith

+0

它看起来不像一个AsyncListView像ArrayCollection或XMLListCollection那样具有任何排序功能。你可以考虑转换;或者在检索它之前在服务器端对数据进行排序。 – JeffryHouser

回答

1

由于这对于使用MapQuest作为他们的地图系统的人来说似乎是一个常见问题,我提供了我的解决方案,可以根据任意列表的距离对自定义POI进行排序。这是针对移动应用程序的解决方案,这就是我在数据网格上使用列表的原因。

protected function sort_clickHandler():void 
      {    

       var dataSortField:SortField = new SortField(); 
       dataSortField.numeric = true; 

       /* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */ 
       var numericDataSort:Sort = new Sort(); 

       numericDataSort.compareFunction=sortFunction; 

       /* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */ 
       getAllMarkersResult.lastResult.sort = numericDataSort; 
       getAllMarkersResult.lastResult.refresh(); 
      } 

      private function sortFunction(a:Object, b:Object, array:Array = null):int 
      { 

       var aPoi:LatLng = new LatLng(a.lat,a.lng); 
       var bPoi:LatLng = new LatLng(b.lat,b.lng); 

       var i:Number=GeodesicCalculatorUtil.calculateGeodesicDistance(FlexGlobals.topLevelApplication.currentLatLng2,aPoi,DistanceUnits.KILOMETERS); 
       var j:Number=GeodesicCalculatorUtil.calculateGeodesicDistance(FlexGlobals.topLevelApplication.currentLatLng2,bPoi,DistanceUnits.KILOMETERS); 


       return ObjectUtil.numericCompare(i, j); 


      }