sundaysgarden

Controller

//

//  LiveGasStationViewController.m

 

#import "LiveGasStationViewController.h"

#import <MAMapKit/MAMapKit.h>

#import <AMapSearchKit/AMapSearchAPI.h>

#import "LocateViewController.h"

#import "LivePoiTableView.h"

#import "LivePoiSectionHeader.h"

#import "LivePoiCell.h"

#import "POIAnnotation.h"

#import "UserLocationManager.h"

 

@interface LiveGasStationViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, MAMapViewDelegate, AMapSearchDelegate, LivePoiSectionHeaderDelegate, LivePoiCellDelegate>

{

    CGFloat _beginOffsetY;  // 起始Y轴偏移量

}

 

/**

 顶部栏

 */

@property (nonatomic, weak) UIView *topBar;

/**

 定位按钮

 */

@property (nonatomic, weak) UIButton *locateButton;

/**

 地图

 */

@property (nonatomic, strong) MAMapView *mapView;

/**

 表视图

 */

@property (nonatomic, weak) LivePoiTableView *tableView;

/**

 更多按钮

 */

@property (nonatomic, weak) UIButton *moreButton;

/**

 搜索视图

 */

@property (nonatomic, weak) UIView *searchView;

 

/**

 搜索

 */

@property (nonatomic, strong) AMapSearchAPI *search;

/**

 城市

 */

@property (nonatomic, copy) NSString *city;

/**

 位置

 */

@property (nonatomic, assign) CLLocationCoordinate2D location;

/**

 poi数组

 */

@property (nonatomic, strong) NSMutableArray <POIAnnotation *> *poiAnnotations;

/**

 筛选poi数组

 */

@property (nonatomic, copy) NSArray *sortedAnnotations;

/**

 筛选类型

 */

@property (nonatomic, assign) TVPOIType sortType;

 

@end

 

@implementation LiveGasStationViewController

 

static NSString *const kLivePoiSectionHeaderID = @"LivePoiSectionHeader";

static NSString *const kLivePoiCellID = @"LivePoiCell";

 

#pragma mark - LifeCycle

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = kBgColor;

 

    [self createUI];

    

    [self locate];

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

}

 

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    [self.view endEditing:YES];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (void)dealloc

{

    DDLogWarn(@"%@ dealloc!", [self.class description]);

}

 

#pragma mark - Initialization

 

- (void)createUI

{

    UIView *topBar = [[UIView alloc] init];

    _topBar = topBar;

    [self.view addSubview:topBar];

    topBar.width = kScreenWidth;

    topBar.height = 44.0;

    topBar.top = kStatusBarHeight;

    

    @weakify(self);

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

    backButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];

    [backButton setTitle:@"中国油站" forState:UIControlStateNormal];

    [backButton setTitleColor:kThemeColor forState:UIControlStateNormal];

    UIImage *backImage = [[UIImage imageNamed:@"common_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    [backButton setImage:backImage forState:UIControlStateNormal];

    backButton.tintColor = kThemeColor;

    backButton.titleEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 0);

#pragma mark Action - 返回

    [[backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        @strongify(self);

        [self.navigationController popViewControllerAnimated:YES];

    }];

    [topBar addSubview:backButton];

    [backButton sizeToFit];

    backButton.left = 10.0;

    backButton.width += backButton.titleEdgeInsets.left;

    backButton.centerY = topBar.height / 2;

    

    UIButton *manualButton = [UIButton buttonWithType:UIButtonTypeCustom];

    manualButton.titleLabel.font = [UIFont systemFontOfSize:14];

    [manualButton setTitleColor:kLightGrayColor forState:UIControlStateNormal];

    [manualButton setTitle:@"手动定位" forState:UIControlStateNormal];

#pragma mark Action - 手动定位

    [[manualButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        @strongify(self);

        LocateViewController *lvc = [[LocateViewController alloc] init];

        [lvc.doneSelectingCitySubject subscribeNext:^(NSString *city) {

            [self.locateButton setTitle:city forState:UIControlStateNormal];

            

            self.city = city;

            [self searchPoiByKeywords:nil];

        }];

        

        [self.navigationController pushViewController:lvc animated:YES];

    }];

    [topBar addSubview:manualButton];

    [manualButton sizeToFit];

    manualButton.left = backButton.right + 8.0;

    manualButton.centerY = backButton.centerY;

    

    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [searchButton setImage:[UIImage imageNamed:@"live_search"] forState:UIControlStateNormal];

#pragma mark Action - 搜索

    [[searchButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        @strongify(self);

        self.searchView.hidden = NO;

    }];

    [topBar addSubview:searchButton];

    [searchButton sizeToFit];

    searchButton.right = topBar.width - 10.0;

    searchButton.centerY = backButton.centerY;

    

    UIButton *locateButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _locateButton = locateButton;

    locateButton.titleLabel.font = [UIFont systemFontOfSize:14];

    [locateButton setTitleColor:kLightGrayColor forState:UIControlStateNormal];

    [locateButton setTitle:@"所在位置" forState:UIControlStateNormal];

    [locateButton setImage:[UIImage imageNamed:@"live_location"] forState:UIControlStateNormal];

    locateButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);

    locateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

#pragma mark Action - 定位

    [[locateButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) {

        @strongify(self);

        [self locate];

    }];

    [topBar addSubview:locateButton];

    [locateButton sizeToFit];

    locateButton.left = manualButton.right + 4.0;

    locateButton.width = searchButton.left - locateButton.left - 4.0;

    locateButton.centerY = backButton.centerY;

}

 

- (MAMapView *)mapView

{

    if (!_mapView) {

        _mapView = [[MAMapView alloc] init];

        _mapView.top = self.topBar.bottom;

        _mapView.width = kScreenWidth;

        _mapView.height = kScreenHeight - self.topBar.height;

        _mapView.delegate = self;

        _mapView.showsUserLocation = YES;

        [self.view insertSubview:_mapView atIndex:0];

    }

    return _mapView;

}

 

- (UITableView *)tableView

{

    if (!_tableView) {

        LivePoiTableView *tableView = [[LivePoiTableView alloc] initWithFrame:self.mapView.frame style:UITableViewStylePlain];

        _tableView = tableView;

        tableView.dataSource = self;

        tableView.delegate = self;

        tableView.backgroundColor = [UIColor clearColor];

        tableView.layoutMargins = UIEdgeInsetsZero;

        tableView.separatorInset = UIEdgeInsetsZero;

        tableView.rowHeight = 56.0;

        [tableView registerClass:[LivePoiCell class] forCellReuseIdentifier:kLivePoiCellID];

        [tableView registerClass:[LivePoiSectionHeader class] forHeaderFooterViewReuseIdentifier:kLivePoiSectionHeaderID];

        [tableView.panGestureRecognizer addTarget:self action:@selector(handlePan:)];

        [self.view insertSubview:tableView aboveSubview:self.mapView];

    }

    return _tableView;

}

 

- (UIButton *)moreButton

{

    if (!_moreButton) {

        UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];

        _moreButton = moreButton;

        moreButton.backgroundColor = [UIColor whiteColor];

        moreButton.titleLabel.font = [UIFont systemFontOfSize:16];

        [moreButton setTitleColor:kTextColor forState:UIControlStateNormal];

        [moreButton setTitle:@"点击展开更多结果" forState:UIControlStateNormal];

#pragma mark Action - 显示列表

        @weakify(self);

        [[moreButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) {

            @strongify(self);

            [x removeFromSuperview];

            [self resetTableView];

        }];

        moreButton.layer.shadowColor = [UIColor blackColor].CGColor;

        moreButton.layer.shadowOpacity = 0.6;

        moreButton.layer.shadowOffset = CGSizeMake(0, -2);

        [self.view addSubview:moreButton];

        moreButton.width = kScreenWidth;

        moreButton.height = 44.0;

        moreButton.bottom = kScreenHeight;

    }

    return _moreButton;

}

 

- (UIView *)searchView

{

    if (!_searchView) {

        UIView *searchView = [[UIView alloc] init];

        _searchView = searchView;

        searchView.backgroundColor = [UIColor whiteColor];

        searchView.layer.shadowColor = [UIColor blackColor].CGColor;

        searchView.layer.shadowOpacity = 0.6;

        searchView.layer.shadowOffset = CGSizeMake(0, 1);

        searchView.layer.cornerRadius = kCornerRadius;

        [self.view addSubview:searchView];

        searchView.top = self.topBar.bottom + 10.0;

        searchView.left = 10.0;

        searchView.height = 44.0;

        searchView.width = kScreenWidth - searchView.left * 2;

        

        @weakify(self);

#pragma mark Action - 移除搜索视图

        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [closeButton setImage:[UIImage imageNamed:@"live_close_black"] forState:UIControlStateNormal];

        [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

            @strongify(self);

            [self.searchView removeFromSuperview];

        }];

        [searchView addSubview:closeButton];

        [closeButton sizeToFit];

        closeButton.right = searchView.width - 10.0;

        closeButton.centerY = searchView.height / 2;

        

        UIView *line = [[UIView alloc] init];

        line.userInteractionEnabled = NO;

        line.backgroundColor = kBorderColor;

        [searchView addSubview:line];

        line.width = CGFloatFromPixel(1.0);

        line.right = closeButton.left - 10.0;

        line.top = 8.0;

        line.height = searchView.height - line.top * 2;

        

        UITextField *searchTextField = [[UITextField alloc] init];

        searchTextField.delegate = self;

        searchTextField.borderStyle = UITextBorderStyleNone;

        searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

        searchTextField.placeholder = @"关键字";

        searchTextField.returnKeyType = UIReturnKeySearch;

        searchTextField.font = [UIFont systemFontOfSize:16];

        searchTextField.textColor = kTextColor;

        [searchTextField becomeFirstResponder];

        [searchView addSubview:searchTextField];

        searchTextField.left = 10.0;

        searchTextField.width = line.left - searchTextField.left - 10.0;

        searchTextField.height = searchView.height;

    }

    return _searchView;

}

 

- (AMapSearchAPI *)search

{

    if (!_search) {

        _search = [[AMapSearchAPI alloc] init];

        _search.delegate = self;

    }

    return _search;

}

 

- (NSMutableArray<POIAnnotation *> *)poiAnnotations

{

    if (!_poiAnnotations) {

        _poiAnnotations = [[NSMutableArray alloc] init];

    }

    return _poiAnnotations;

}

 

#pragma mark - TableViewDataSource

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.sortedAnnotations.count;

}

 

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

{

    LivePoiCell *cell = [tableView dequeueReusableCellWithIdentifier:kLivePoiCellID];

    POIAnnotation *annotation = self.sortedAnnotations[indexPath.row];

    [cell refreshData:annotation];

    cell.delegate = self;

    return cell;

}

 

#pragma mark - TableViewDelegate

 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    LivePoiSectionHeader *sectionHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kLivePoiSectionHeaderID];

    [sectionHeader refreshData:self.sortType];

    sectionHeader.delegate = self;

    return sectionHeader;

}

 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 44.0;

}

 

#pragma mark - LivePoiCellDelegate

#pragma mark 打电话

- (void)clickPhoneButton:(LivePoiCell *)cell

{

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    POIAnnotation *annotation = self.sortedAnnotations[indexPath.row];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", annotation.poi.tel]]];

}

 

#pragma mark 导航

- (void)clickNavigationButton:(LivePoiCell *)cell

{

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    POIAnnotation *annotation = self.sortedAnnotations[indexPath.row];

    [self navigate:annotation.poi];

}

 

#pragma mark - LivePoiSectionHeaderDelegate

#pragma mark 筛选

- (void)clickTypeButton:(LivePoiSectionHeader *)sectionHeader withType:(TVPOIType)type

{

    self.sortType = type;

    NSMutableArray *annotations = [[NSMutableArray alloc] init];

    for (POIAnnotation *annotation in self.poiAnnotations) {

        if (self.sortType & annotation.type) {

            [annotations addObject:annotation];

        }

    }

    self.sortedAnnotations = annotations;

    [self.mapView removeAnnotations:self.mapView.annotations];

    [self.mapView addAnnotations:self.sortedAnnotations];

    [self.mapView showAnnotations:self.sortedAnnotations edgePadding:UIEdgeInsetsMake(20, 20, self.mapView.height - self.tableView.contentInset.top + 20, 20) animated:NO];

    

    [self.tableView reloadData];

}

 

#pragma mark - TextFieldDelegate

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    [self searchPoiByKeywords:textField.text];

    return YES;

}

 

#pragma mark - HandleAction

#pragma mark 滑动列表

- (void)handlePan:(UIPanGestureRecognizer *)pan

{

    CGPoint offset = [pan translationInView:pan.view];

    if (pan.state == UIGestureRecognizerStateBegan) {

        _beginOffsetY = offset.y;

    }else if (pan.state == UIGestureRecognizerStateChanged) {

        if ((offset.y - _beginOffsetY < 0) && self.tableView.contentInset.top > 0) {

            pan.enabled = NO;

            [_searchView removeFromSuperview];

            [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseIn animations:^{

                self.tableView.contentInset = UIEdgeInsetsZero;

            } completion:^(BOOL finished) {

                pan.enabled = YES;

            }];

        }else if ((offset.y - _beginOffsetY > 0) && self.tableView.contentOffset.y <= 0) {

            pan.enabled = NO;

            [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseIn animations:^{

                [self.tableView setContentOffset:CGPointMake(self.tableView.contentOffset.x, -self.tableView.height)];

            } completion:^(BOOL finished) {

                pan.enabled = YES;

                self.tableView.hidden = YES;

                self.moreButton.hidden = NO;

            }];

            [self.mapView showAnnotations:self.sortedAnnotations edgePadding:UIEdgeInsetsMake(20, 20, 20, 20) animated:YES];

        }

    }

}

 

#pragma mark - Utility

#pragma mark 定位

- (void)locate

{

    @weakify(self);

    [[UserLocationManager sharedManager] startLocationWithCompletionHandler:^(UserLocationModel *userLocation, NSError *error) {

        @strongify(self);

        if (error) {

            [TVProgressHUD showErrorWithStatus:error.localizedDescription];

        }else {

            [self.locateButton setTitle:userLocation.Street forState:UIControlStateNormal];

            self.city = userLocation.City;

            self.location = userLocation.coordinate;

            [self searchPoiByCenterCoordinate:userLocation.coordinate];

        }

    }];

}

 

#pragma mark 根据定位点搜索加油站

- (void)searchPoiByCenterCoordinate:(CLLocationCoordinate2D)coordinate

{

    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];

    request.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];

    request.types = @"加油站";

    request.keywords = @"中石化|中石油";

    request.radius = 5000;

    /* 按照距离排序. */

    request.sortrule = 0;

    request.requireExtension = YES;

    

    [self.search AMapPOIAroundSearch:request];

}

 

#pragma mark 根据关键字搜索加油站

- (void)searchPoiByKeywords:(NSString *)keywords

{

    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];

    request.types = @"加油站";

    if (![keywords isNotBlank]) {

        keywords = @"中石化|中石油";

    }

    request.keywords = keywords;

    request.city = self.city;

    request.cityLimit = YES;

    /* 按照距离排序. */

    request.sortrule = 0;

    request.requireExtension = YES;

    

    [self.search AMapPOIKeywordsSearch:request];

}

 

#pragma mark 导航

- (void)navigate:(AMapPOI *)poi

{

    if (self.location.latitude == 0 && self.location.longitude == 0 ) {

        [TVProgressHUD showErrorWithStatus:@"定位失败,无法进行导航"];

        return;

    }

    NSURL *scheme = [NSURL URLWithString:@"iosamap://"];

    BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:scheme];

    if (canOpen) {

        NSString *navigationScheme = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&poiname=%@&poiid=%@&lat=%f&lon=%f&dev=1&style=2", [AppInfo appName], poi.name, poi.uid, self.location.latitude, self.location.longitude];

        navigationScheme = [navigationScheme stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

        NSURL *URL = [NSURL URLWithString:navigationScheme];

        if (kiOS10Later) {  // iOS10以后,使用新API

            [[UIApplication sharedApplication] openURL:URL options:nil completionHandler:NULL];

        }else {  // iOS10以前,使用旧API

            [[UIApplication sharedApplication] openURL:URL];

        }

    }else {

        [TVProgressHUD showErrorWithStatus:@"打开高德地图失败"];

    }

}

 

#pragma mark 重置列表

- (void)resetTableView

{

    [_moreButton removeFromSuperview];

    self.tableView.hidden = NO;

    self.tableView.contentInset = UIEdgeInsetsMake(kScreenWidth * 0.5625, 0, 0, 0);

    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationCurveEaseIn animations:^{

        [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top)];

    } completion:NULL];

    

    /* 如果有多个结果, 设置地图使所有的annotation都可见. */

    [self.mapView showAnnotations:self.sortedAnnotations edgePadding:UIEdgeInsetsMake(20, 20, self.mapView.height - self.tableView.contentInset.top + 20, 20) animated:YES];

}

 

#pragma mark - MAMapViewDelegate

 

- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

{

    id<MAAnnotation> annotation = view.annotation;

    

    if ([annotation isKindOfClass:[POIAnnotation class]])

    {

        POIAnnotation *poiAnnotation = (POIAnnotation*)annotation;

        [self navigate:poiAnnotation.poi];

    }

}

 

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation

{

    if ([annotation isKindOfClass:[POIAnnotation class]])

    {

        static NSString *poiIdentifier = @"poiIdentifier";

        MAAnnotationView *poiAnnotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:poiIdentifier];

        if (poiAnnotationView == nil)

        {

            poiAnnotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:poiIdentifier];

        }

        

        POIAnnotation *poiAnnotation = (POIAnnotation *)annotation;

        switch (poiAnnotation.type) {

            case TVPOIOil:

                poiAnnotationView.image = [UIImage imageNamed:@"live_oil"];

                break;

            case TVPOIGas:

                poiAnnotationView.image = [UIImage imageNamed:@"live_gas"];

                break;

            default:

                poiAnnotationView.image = [[UIImage imageNamed:@"live_oilBag"] imageByResizeToSize:CGSizeMake(30, 30)];

                break;

        }

        poiAnnotationView.canShowCallout = YES;

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        detailButton.tintColor = kThemeColor;

        poiAnnotationView.rightCalloutAccessoryView = detailButton;

        

        return poiAnnotationView;

    }

    

    return nil;

}

 

#pragma mark - AMapSearchDelegate

 

- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error

{

    DDLogError(@"%@", error);

}

 

/* POI 搜索回调. */

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response

{

    [self.mapView removeAnnotations:self.mapView.annotations];

    [self.poiAnnotations removeAllObjects];

    self.sortType |= TVPOIOil;

    self.sortType |= TVPOIGas;

    self.sortType |= TVPOIOther;

    

    if (response.pois.count == 0)

    {

        [TVProgressHUD showInfoWithStatus:@"查无结果"];

        return;

    }

    

    [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {

        

        [self.poiAnnotations addObject:[[POIAnnotation alloc] initWithPOI:obj]];

        

    }];

    self.sortedAnnotations = self.poiAnnotations;

    /* 将结果以annotation的形式加载到地图上. */

    [self.mapView addAnnotations:self.sortedAnnotations];

    

    [self.tableView reloadData];

    [self resetTableView];

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

@end

 ////////////////////////////

PoiModel

//

//  POIAnnotation.h

//  SearchV3Demo

 

#import <MAMapKit/MAMapKit.h>

#import <AMapSearchKit/AMapCommonObj.h>

 

/**

 poi类型

 */

typedef NS_ENUM(NSInteger, TVPOIType) {

    TVPOIOil = 1 << 0,

    TVPOIGas = 1 << 1,

    TVPOIOther = 1 << 2

};

 

/**

 poi标注点

 */

@interface POIAnnotation : NSObject <MAAnnotation>

 

/**

 构造方法

 

 @param poi poi

 @return 实例

 */

- (id)initWithPOI:(AMapPOI *)poi;

 

/**

 poi

 */

@property (nonatomic, readonly, strong) AMapPOI *poi;

 

/**

 经纬度

 */

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

 

/**

 poi类型

 */

@property (nonatomic, assign) TVPOIType type;

 

/*!

 @brief 获取annotation标题

 @return 返回annotation的标题信息

 */

- (NSString *)title;

 

/*!

 @brief 获取annotation副标题

 @return 返回annotation的副标题信息

 */

- (NSString *)subtitle;

 

@end

 

//

//  POIAnnotation.m

 

#import "POIAnnotation.h"

 

@interface POIAnnotation ()

 

@property (nonatomic, readwrite, strong) AMapPOI *poi;

 

@end

 

@implementation POIAnnotation

@synthesize poi = _poi;

 

#pragma mark - MAAnnotation Protocol

 

- (NSString *)title

{

    return self.poi.name;

}

 

- (NSString *)subtitle

{

    return self.poi.address;

}

 

- (CLLocationCoordinate2D)coordinate

{

    return CLLocationCoordinate2DMake(self.poi.location.latitude, self.poi.location.longitude);

}

 

#pragma mark - Life Cycle

 

- (id)initWithPOI:(AMapPOI *)poi

{

    if (self = [super init])

    {

        self.poi = poi;

        if ([poi.type containsString:@"中国石化"]) {

            self.type = TVPOIOil;

        }else if ([poi.type containsString:@"中石油"]) {

            self.type = TVPOIGas;

        }else {

            self.type = TVPOIOther;

        }

    }

    

    return self;

}

 

@end

 UITableView

//

//  POIAnnotation.m

 

#import "POIAnnotation.h"

 

@interface POIAnnotation ()

 

@property (nonatomic, readwrite, strong) AMapPOI *poi;

 

@end

 

@implementation POIAnnotation

@synthesize poi = _poi;

 

#pragma mark - MAAnnotation Protocol

 

- (NSString *)title

{

    return self.poi.name;

}

 

- (NSString *)subtitle

{

    return self.poi.address;

}

 

- (CLLocationCoordinate2D)coordinate

{

    return CLLocationCoordinate2DMake(self.poi.location.latitude, self.poi.location.longitude);

}

 

#pragma mark - Life Cycle

 

- (id)initWithPOI:(AMapPOI *)poi

{

    if (self = [super init])

    {

        self.poi = poi;

        if ([poi.type containsString:@"中国石化"]) {

            self.type = TVPOIOil;

        }else if ([poi.type containsString:@"中石油"]) {

            self.type = TVPOIGas;

        }else {

            self.type = TVPOIOther;

        }

    }

    

    return self;

}

 

@end

 SectionHeaderView

//

//  LivePoiSectionHeader.h

 

#import <UIKit/UIKit.h>

#import "POIAnnotation.h"

 

@class LivePoiSectionHeader;

 

/**

 poi节头

 */

@protocol LivePoiSectionHeaderDelegate <NSObject>

 

/**

 点击筛选类型按钮代理方法

 */

- (void)clickTypeButton:(LivePoiSectionHeader *)sectionHeader withType:(TVPOIType)type;

 

@end

 

@interface LivePoiSectionHeader : UITableViewHeaderFooterView

 

/**

 按钮数组

 */

@property (nonatomic, copy) NSArray <UIButton *> *buttons;

/**

 代理

 */

@property (nonatomic, weak) id<LivePoiSectionHeaderDelegate> delegate;

 

/**

 刷新数据

 

 @param type 筛选类型

 */

- (void)refreshData:(TVPOIType)type;

 

@end

 

//

//  LivePoiSectionHeader.m

//  AutoTV

//

//  Created by BoxingWoo on 2017/10/11.

//  Copyright © 2017年 autotv. All rights reserved.

//

 

#import "LivePoiSectionHeader.h"

 

@interface LivePoiSectionHeader ()

 

@property (nonatomic, assign) TVPOIType type;

@property (nonatomic, weak) UIView *line;

 

@end

 

@implementation LivePoiSectionHeader

 

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier

{

    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {

        self.contentView.backgroundColor = [UIColor whiteColor];

        NSArray *arr = @[@{@"title":@"中石化", @"type":@(TVPOIOil)}, @{@"title":@"中石油", @"type":@(TVPOIGas)}];

        NSMutableArray *buttons = [[NSMutableArray alloc] init];

        for (NSInteger i = 0; i < arr.count; i++) {

            NSDictionary *dict = arr[i];

            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            button.titleLabel.font = [UIFont systemFontOfSize:12];

            [button setTitleColor:kTextColor forState:UIControlStateNormal];

            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

            [button setTitle:dict[@"title"] forState:UIControlStateNormal];

            [button setBackgroundImage:[[UIImage imageWithColor:kBorderColor size:CGSizeMake(60, 30)] imageByRoundCornerRadius:15] forState:UIControlStateNormal];

            [button setBackgroundImage:[[UIImage imageWithColor:kThemeColor size:CGSizeMake(60, 30)] imageByRoundCornerRadius:15] forState:UIControlStateSelected];

            [button addTarget:self action:@selector(handleSort:) forControlEvents:UIControlEventTouchUpInside];

            button.tag = [dict[@"type"] integerValue];

            [buttons addObject:button];

            [self.contentView addSubview:button];

            

            UIView *line = [[UIView alloc] init];

            _line = line;

            line.userInteractionEnabled = NO;

            line.backgroundColor = [UIColor colorWithWhite:221 / 255.0 alpha:1.0];

            [self.contentView addSubview:line];

        }

        _buttons = buttons;

    }

    return self;

}

 

- (void)handleSort:(UIButton *)button

{

    button.selected = !button.isSelected;

    if (button.isSelected) {

        self.type |= button.tag;

    }else {

        self.type ^= button.tag;

    }

    

    [self.delegate clickTypeButton:self withType:self.type];

}

 

- (void)refreshData:(TVPOIType)type

{

    self.type = type;

    for (UIButton *button in self.buttons) {

        button.selected = self.type & button.tag;

    }

}

 

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    for (NSInteger i = 0; i < self.buttons.count; i++) {

        UIButton *button = self.buttons[i];

        button.frame = CGRectMake(20 + (60 + 10) * i, (self.contentView.height - 30) / 2, 60, 30);

    }

    

    self.line.width = self.contentView.width;

    self.line.height = CGFloatFromPixel(1.0);

    self.line.bottom = self.contentView.height - self.line.height;

}

 

@end

 

分类:

技术点:

相关文章: