iOS地图相关首页投稿(暂停使用,暂停投稿)程序员

根据百度地图实现附近资源的距离排序

2016-06-12  本文已影响2374人  Sean_Shi

先上效果图(数据是造的,UI上四个items就经纬度不一样):

根据当前位置从近到远排序

其实这个很简单,额.....但是还是觉得总结还是进步的关键。

先说思路:先根据接口返回的数据(经纬度)、根据百度地图的当前定位,然后计算出每条数据到到当前点的距离(精确度最好要高,)。 这里我使用数组arrayKeys保存所有的距离。然后创建一个字典dataDic把当前距离distance作为key,values为key对应下的数据model。然后我们将数据arrayKeys进行排序。这样只要arrayKeys排序了,对应的model其实也相当于排序了。

贴出关键代码:

    NSDictionary *params = @{@"networkProvince":@"安徽",@"pageIndex":@(1),@"pageSize":@(10)};
    [AFNetworkTool GET:queryNetworkListUrl parameters:params showHUD:YES success:^(id returnData) {
    
     NetworkListModel *model = [NetworkListModel mj_objectWithKeyValues:returnData];
    
      self.dataArray = (NSMutableArray *)model.data.data;
    
       //已无更多数据
       if (self.dataArray.count==0) {
       //停止上拉刷新
        [self.tableview.mj_footer endRefreshingWithNoMoreData];
       
        return;
    }
    
    //排序
    if (self.isLocationOK) {
        
        //获取所有key的集合
        NSMutableArray *arrayKeys = [NSMutableArray array];
        //key:距离  values:模型
        NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
        
        for (int i = 0; i < self.dataArray.count; i++) {
            
            NetworkListDetail *dictModel = self.dataArray[i];
            //根据用户指定的两个坐标点,计算这两个点的实际地理距离。核心代码如下:
            BMKMapPoint point1 = BMKMapPointForCoordinate(self.locationCoordinate2D);
            BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake([dictModel.network_latitude doubleValue],[dictModel.network_longitude doubleValue]));
            CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
            
            YLLog(@"self.arrayRecivedKeys ---distance %fd",distance);
            
            [arrayKeys addObject:[NSNumber numberWithDouble:distance]];
            [dataDic setObject:dictModel forKey:[NSNumber numberWithDouble:distance]];
            
            self.dictRecivedValues = dataDic;
        }
        
        //排序arrayKeys
        NSArray *sortedArray = [arrayKeys sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
            if ([obj1 floatValue] < [obj2 floatValue]) {
                return NSOrderedAscending;
            } else {
                return NSOrderedDescending;
            }
        }];
        
        self.arrayAllKeys = sortedArray;
        
        YLLog(@"arrayAllKeys --- %@",self.arrayAllKeys);
    }
    
    //总页数
    self.totalPage= [model.data.totalPageNum integerValue];
    
    if (self.dataArray) {
        //记录当前页
        self.currentPage = 1;
    }
    //重新载入数据
    [self.tableview reloadData];
    //停止下拉刷新
    [self.tableview.mj_header endRefreshing];
    
} failure:^(NSError *error) {
    
    //停止下拉刷新
    [self.tableview.mj_header endRefreshing];
}];

地图代理方法里:

  //处理位置坐标更新
  - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
  {
          YLLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

        //发起反向地理编码检索
        _searcher =[[BMKGeoCodeSearch alloc]init];
        _searcher.delegate = self;
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
        self.locationCoordinate2D = pt;

        BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
        BMKReverseGeoCodeOption alloc]init];
        reverseGeoCodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];

        if(flag)
      {
            YLLog(@"反geo检索发送成功");
             self.isLocationOK = YES;
              [self setupRefresh:self.tableview];
      }
       else
      {
              YLLog(@"反geo检索发送失败");
              self.isLocationOK = NO;
              [self setupRefresh:self.tableview];
      }

    [_locService stopUserLocationService];
}

  //接收反向地理编码结果
  -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:            (BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{

    if (error == BMK_SEARCH_NO_ERROR) {

        _mapLb.text = [NSString stringWithFormat:@"当前位置:%@",result.address];
         YLLog(@"%@",result.address);
         YLLog(@"%@",result.addressDetail.province);
    }
    else {
  
        _mapLb.text = @"当前定位失败";
        YLLog(@"抱歉,未找到结果");
    }
  }

Tableview代理方法:

  #pragma mark -UITableViewDataSource
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      return self.dataArray.count;
  }

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {

      AfterServerCell *cell = [AfterServerCell afterServerCellWithTableView:tableView];

      cell.model = self.dictRecivedValues[self.arrayAllKeys[indexPath.row]];
  //    cell.model = self.dataArray[indexPath.row];
      cell.distance = self.arrayAllKeys[indexPath.row];

      return cell;
    }

好了就是这样了,= = 。

上一篇下一篇

猜你喜欢

热点阅读