You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

342 lines
13 KiB

//
// TrackViewController.m
// watch
//
// Created by xTT on 2017/7/13.
// Copyright © 2017年 xTT. All rights reserved.
//
#import "TrackViewController.h"
#import "NewTrackMsgCell.h"
//高德
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
@interface TrackViewController ()<AMapSearchDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong) AMapSearchAPI *searchAPI;
/**
这个是逆地理位置编码的请求的列表 和 Locate 的 Model 对应
*/
@property (nonatomic, strong) NSMutableArray *AMapSearchList;
/**
需要上传的 解析完的 坐标点的 参数
*/
@property (nonatomic, strong) NSMutableArray *shouldUpdateLocateAddressList;
/**
需要上传的定位点的ID 集合列表
*/
@property (nonatomic, strong) NSMutableArray *shouldUpdateIDList;
@end
@implementation TrackViewController
-(NSMutableArray *)shouldUpdateIDList{
if(!_shouldUpdateIDList){
_shouldUpdateIDList = [NSMutableArray array];
}
return _shouldUpdateIDList;
}
-(NSMutableArray *)shouldUpdateLocateAddressList{
if(!_shouldUpdateLocateAddressList){
_shouldUpdateLocateAddressList = [NSMutableArray array];
}
return _shouldUpdateLocateAddressList;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.zx_hideBaseNavBar = YES;
//self.emptyText = @"暂无历史轨迹";
[self.myTableView registerNib:[UINib nibWithNibName:@"NewTrackMsgCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewTrackMsgCellID"];
//去线
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.myTableView.mj_footer beginRefreshing];
self.myTableView.emptyDataSetSource = self;
self.myTableView.emptyDataSetDelegate = self;
//self.myTableView.estimatedRowHeight = 70;
self.view.backgroundColor = self.myTableView.backgroundColor;
//初始化 定位解析
self.searchAPI = [[AMapSearchAPI alloc] init];
self.searchAPI.delegate = self;
[self setUI];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
-(void)setUI{
Device * mainDevice = cUser.cDevice;
UIImage * defaImage ;
// self.headCollectionView.backgroundColor = [UICommon setGradientBackgroundColor:RGBA(255, 239, 232, 1) Color2:RGBA(255, 255, 255, 1) Width:ScreenWidth-30 Height:72];
[self.headCollectionView.layer addSublayer:[UICommon setGradualChangingColor:self.headCollectionView fromColor:RGBA(255, 239, 232, 1) toColor:RGBA(255, 255, 255, 1)]];
if([mainDevice.sex isEqualToString:@"girl"]){
//女
defaImage = [myHelper getImageWithName: @"icon_girl_head_portrait_1"];
}else{
//男
defaImage = [myHelper getImageWithName: @"icon_boy_head_portrait_1"];
}
//self.deviceImageView = [UIImageView new];
[self.headCollectionView addSubview:self.deviceImageView];
[self.headCollectionView addSubview:self.nameLabel];
[self.headCollectionView addSubview:self.allCountTrackLabel];
[self.headCollectionView addSubview:self.delectBtn];
[self.deviceImageView sd_setImageWithURL:[NSURL URLWithString:mainDevice.avator] placeholderImage:defaImage];
self.deviceImageView.layer.borderWidth = 1.5;
self.deviceImageView.layer.borderColor = KKWhiteColorColor.CGColor;
self.deviceImageView.layer.cornerRadius = self.deviceImageView.height/2;
self.deviceImageView.layer.masksToBounds = YES;
self.nameLabel.textColor = KKGrey153;
self.nameLabel.font = Font_(12);
self.nameLabel.text = mainDevice.name;
self.allCountTrackLabel.text = @"";
[self.delectBtn addTarget:self action:@selector(delectAction:) forControlEvents:UIControlEventTouchUpInside];
[UICommon ui_uiViewFillet:self.headCollectionView.bounds Viewdirection:PYUIdirectionTop toView:self.headCollectionView sizeMake:12];
}
- (void)delectAction:(UIButton*)sender{
WEAKSELF
LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"确定删除轨迹?" message:@"" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) {
[Track delectTrackWithDailyIds:@[self.infoTrack.id] success:^(id responseObject) {
[weakSelf.myDataSource removeAllObjects];
[weakSelf.myTableView reloadData];
weakSelf.headCollectionView.hidden = YES;
weakSelf.infoTrack = nil;
//显示 无数据
if(weakSelf.delectBlockCall){
weakSelf.delectBlockCall();
}
xLog(@"删除轨迹点成功");
} failure:^{
xLog(@"删除轨迹点失败");
} ];
} cancelHandler:nil destructiveHandler:nil];
[alertView showAnimated:YES completionHandler:nil];
}
- (void)loadWithInfoTrack:(Track*)track Back:(BlockCall)blockCall{
// [self.view getViewController]
if(!track){
self.headCollectionView.hidden = YES;
[self.myDataSource removeAllObjects];
[self.myTableView reloadData];
if(blockCall){
blockCall();
}
return;
}else{
self.headCollectionView.hidden = NO;
}
self.infoTrack = track;
NSString *tmp = [NSString stringWithFormat:@"%ld条轨迹",(long)track.amount.integerValue];
NSInteger lenght = tmp.length;
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp];
[att addAttribute:NSForegroundColorAttributeName
value:mainColor
range:NSMakeRange(0,lenght-3)];
self.allCountTrackLabel.attributedText = att;
if(self.AMapSearchList){
[self.AMapSearchList removeAllObjects];
self.AMapSearchList = nil;
}
self.AMapSearchList = [NSMutableArray array];
WEAKSELF
[track getTrackInfoSuccess:^(NSMutableArray *arr) {
NSArray *tempArr = [arr copy];
[weakSelf.shouldUpdateIDList removeAllObjects];
for (int i=0; i<tempArr.count; i++) {
Locate * model = tempArr[i];
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:[model.lat floatValue] longitude:[model.lon floatValue]];
[weakSelf.AMapSearchList addObject:regeo];
if ((model.address && model.address.length == 0 )|| model.address == nil){
[weakSelf.shouldUpdateIDList addObject:model.id];
}
}
[weakSelf.myDataSource removeAllObjects];
[weakSelf.myDataSource addObjectsFromArray:@[tempArr]];
[weakSelf.myTableView reloadData];
if(blockCall){
blockCall();
}
} failure:^(NSError *error){
if (!error) {
}
if(blockCall){
blockCall();
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Locate *locate = self.myDataSource[indexPath.section][indexPath.row];
CGFloat hh = [self getCellheight:locate];
if (self.AMapSearchList.count > 0) {
AMapReGeocodeSearchRequest *regeo = self.AMapSearchList[indexPath.row];
if( (locate.address && locate.address.length == 0 )|| locate.address == nil){
[self.searchAPI AMapReGoecodeSearch:regeo];
}
}
NSString *dateStr = [myHelper getDateFormatWithStr:@"HH:mm"
date:[NSDate dateWithTimeIntervalSince1970:[locate.timestamp doubleValue]]];
// MARK: 增加停留时间 add by lsz 2021-06-24
long seconds = [locate.endtime integerValue] - [locate.timestamp integerValue];
NSString *stayTime = [self secondToTime:seconds];
NewTrackMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTrackMsgCellID" forIndexPath:indexPath];
if([self.myDataSource[indexPath.section] count] -1 == indexPath.row)
[UICommon ui_uiViewFillet:cell.bounds Viewdirection:PYUIdirectionBotton toView:cell sizeMake:12];
if([self.myDataSource[indexPath.section] count] == 1)
{
//起,不绘制虚线
[cell setIconType:0 WithAddress:locate.address WithTime:dateStr CanShapeToView:NO StayTime:stayTime Cellheight:hh];
return cell;
}
if(indexPath.section == 0)
{
if (indexPath.row == 0)
{
[cell setIconType:2 WithAddress:locate.address WithTime:dateStr CanShapeToView:YES StayTime:stayTime Cellheight:hh];
}
else if([self.myDataSource[indexPath.section] count] -1 == indexPath.row)
{
[cell setIconType:0 WithAddress:locate.address WithTime:dateStr CanShapeToView:YES StayTime:stayTime Cellheight:hh];
}
else
{
[cell setIconType:1 WithAddress:locate.address WithTime:dateStr CanShapeToView:YES StayTime:stayTime Cellheight:hh];
}
}
return cell;
}
- (NSString *)secondToTime:(long)seconds {
long days = seconds/86400;
seconds = seconds%86400;
long hours = seconds/3600;
seconds = seconds%3600;
long minutes = ceil(seconds/60.0);
seconds = seconds%60;
if (days > 0) {
return [NSString stringWithFormat:@"停留 %zd 天 %zd 小时 %zd 分",days,hours,minutes];
} else if (hours > 0) {
return [NSString stringWithFormat:@"停留 %zd 小时 %zd 分",hours,minutes];
} else if(minutes > 0) {
return [NSString stringWithFormat:@"停留 %zd 分",minutes];
} else {
return @"0";
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
Locate *locate = self.myDataSource[indexPath.section][indexPath.row];
return [self getCellheight:locate];
}
/// 获取cell高度(根据内容动态计算)
- (CGFloat)getCellheight:(Locate *)locate
{
CGRect rect = [UICommon GetTextWidth:locate.address ViewHeight:ScreenWidth-32-123 fontSize:FontBold_(14) type:@"h"];
// MARK: 增加停留时间 add by lsz 2021-06-24
long seconds = [locate.endtime integerValue] - [locate.timestamp integerValue];
NSString *stayTime = [self secondToTime:seconds];
CGFloat hh = stayTime.length > 1 ? rect.size.height+20+5+35 : rect.size.height+40;
return hh;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ====高德地图位置解析回调====
-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
if (response.regeocode != nil)
{
NSString *name = response.regeocode.formattedAddress;
//解析response获取地址描述,具体解析见 Demo
// //获取市
// NSString *city = response.regeocode.addressComponent.city;
// NSString *province = response.regeocode.addressComponent.province;
// if(city && city.length>0){
// name = [name componentsSeparatedByString:city].lastObject;
// if([city isEqualToString:province]){
// //直辖市
// name = [NSString stringWithFormat:@"%@ %@",name,city];
// }else{
// //普通市
// name = [NSString stringWithFormat:@"%@ %@ %@",name,city,province];
// }
// }
if(self.AMapSearchList.count > 0){
NSInteger index = [self.AMapSearchList indexOfObject:request];
if(index != NSNotFound &&
self.myDataSource.count > 0 &&
index < [self.myDataSource[0] count]){
Locate * indexModel = self.myDataSource[0][index];
if([self.shouldUpdateIDList containsObject:indexModel.id]){
indexModel.address = name;
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[param setValue:indexModel.id forKey:@"locationId"];
[param setValue:name forKey:@"address"];
[self.shouldUpdateLocateAddressList addObject:param];
[self.myTableView reloadData];
}
if (self.shouldUpdateLocateAddressList.count == self.shouldUpdateIDList.count){
//上传某天定位点 的 解析位置数据
WEAKSELF
[self.infoTrack postLocationsAddress:self.shouldUpdateLocateAddressList success:^(id responseObject) {
xLog(@"APP解析的 位置上传成功");
[weakSelf.shouldUpdateLocateAddressList removeAllObjects];
[weakSelf.shouldUpdateIDList removeAllObjects];
} failure:^{
[weakSelf.shouldUpdateLocateAddressList removeAllObjects];
[weakSelf.shouldUpdateIDList removeAllObjects];
xLog(@"APP解析的 位置上传失败");
}];
}
}
}
}
}
@end