DistanceModel.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // DistanceModel.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/10/24.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. #import "DistanceModel.h"
  8. #import "LivingModel.h"
  9. @implementation DistanceModel
  10. - (NSMutableArray *)CalculateDistanceWithArray:(NSMutableArray *)DisArray andPoint:(QMapPoint )point
  11. {
  12. self.distanceArray = [[NSMutableArray alloc]init];
  13. //计算距离
  14. for (int i = 0; i < DisArray.count; i ++)
  15. {
  16. LivingModel *model = DisArray[i];
  17. float distance;
  18. if (model.xponit== 0 && model.yponit== 0)
  19. {
  20. distance = 10000000000;
  21. }else
  22. {
  23. QMapPoint point2 = QMapPointForCoordinate(CLLocationCoordinate2DMake(model.xponit,model.yponit));
  24. distance = QMetersBetweenMapPoints(point, point2);
  25. }
  26. model.distance = distance;
  27. DisArray[i] = model;
  28. }
  29. //距离的排序
  30. for (int i = 0; i < DisArray.count-1; i ++)
  31. {
  32. for (int j = i+1; j < DisArray.count; j++)
  33. {
  34. LivingModel *model1 = DisArray[i];
  35. LivingModel *model2 = DisArray[j];
  36. if (model1.distance >= model2.distance)
  37. {
  38. [DisArray exchangeObjectAtIndex:i withObjectAtIndex:j];
  39. }
  40. }
  41. }
  42. return DisArray;
  43. }
  44. @end