-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddressPickerViewController.m
More file actions
211 lines (167 loc) · 5.68 KB
/
AddressPickerViewController.m
File metadata and controls
211 lines (167 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// AddressPickerViewController.m
// DuDu
//
// Created by i-chou on 11/9/15.
// Copyright © 2015 i-chou. All rights reserved.
//
#import "AddressPickerViewController.h"
@interface AddressPickerViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tips;
@end
@implementation AddressPickerViewController
- (id)initWithLocation:(NSString *)location
{
return [super init];
}
/* 输入提示 搜索.*/
- (void)searchTipsWithKey:(NSString *)key
{
if (key.length == 0)
{
return;
}
AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
tips.keywords = key;
tips.city = @"北京";
[self.search AMapInputTipsSearch:tips];
}
#pragma mark - AMapSearchDelegate
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error.debugDescription);
}
/* 输入提示回调. */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
[self.tips setArray:response.tips];
[self.tableView reloadData];
}
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
[self.tips setArray:response.pois];
[self.tableView reloadData];
}
//实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
[self.tips setArray:response.geocodes];
// for (AMapTip *p in response.geocodes) {
// [self.tips addObject:p];
// }
[self.tableView reloadData];
}
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *key = searchBar.text;
/* 按下键盘enter, 搜索poi */
AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
request.keywords = key;
request.city = @"010";
request.requireExtension = YES;
[self.search AMapPOIKeywordsSearch:request];
self.searchBar.placeholder = key;
[self.searchBar endEditing:YES];
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchTipsWithKey:searchText];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tips.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tipCellIdentifier = @"tipCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipCellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:tipCellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"locate"];
}
AMapTip *tip = self.tips[indexPath.row];
if (tip.location == nil)
{
cell.imageView.image = [UIImage imageNamed:@"search"];
}
cell.textLabel.text = tip.name;
cell.detailTextLabel.text = tip.district;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AMapTip *tip = self.tips[indexPath.row];
if ([self.delegate respondsToSelector:@selector(addressPicker:fromAddress:toAddress:)]) {
if (self.isFrom) {
[self.delegate addressPicker:self fromAddress:tip toAddress:nil];
} else {
[self.delegate addressPicker:self fromAddress:nil toAddress:tip];
}
}
if (!self.isFromAddressVC) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark - Initialization
- (void)initSearchBar
{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, NAV_BAR_HEIGHT_IOS7, CGRectGetWidth(self.view.bounds), 44)];
self.searchBar.barStyle = UIBarStyleDefault;
self.searchBar.translucent = YES;
self.searchBar.delegate = self;
self.searchBar.placeholder = @"输入地点";
self.searchBar.keyboardType = UIKeyboardTypeDefault;
[self.searchBar becomeFirstResponder];
[self.view addSubview:self.searchBar];
}
- (void)initTableView
{
self.tableView = [[UITableView alloc] initWithFrame:ccr(0, CGRectGetMaxY(self.searchBar.frame), SCREEN_WIDTH, SCREEN_HEIGHT-CGRectGetMaxY(self.searchBar.frame))];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (void)startLocation
{
//构造AMapGeocodeSearchRequest对象,address为必选项,city为可选项
AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
geo.address = self.location;
//发起正向地理编码
[self.search AMapGeocodeSearch:geo];
}
#pragma mark - Life Cycle
- (id)init
{
if (self = [super init])
{
self.tips = [NSMutableArray array];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initSearchBar];
[self initTableView];
[AMapSearchServices sharedServices].apiKey = AMAP_KEY;
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self;
// [self startLocation];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.search.delegate = nil;
}
@end