AMapViewController.m
21.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//
// AMapViewController.m
// IM_ios_client
//
// Created by zsq on 2018/10/25.
// Copyright © 2018 xiniu. All rights reserved.
//
#import "AMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <MAMapKit/MAMapView.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "SearchPOIListViewController.h"
#import "PlaceAroundTableView.h"
@interface AMapViewController ()<MAMapViewDelegate, PlaceAroundTableViewDeleagate>
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapSearchAPI *search;
@property (nonatomic, strong) AMapLocationManager *locationManager;
@property (nonatomic, strong) PlaceAroundTableView *tableview;
@property (nonatomic, strong) UIImageView *centerAnnotationView;
@property (nonatomic, assign) BOOL isMapViewRegionChangedFromTableView;
@property (nonatomic, strong) AMapTip *currentTip;
@property (nonatomic, assign) BOOL isLocated;
@property (nonatomic, assign) BOOL isSearch;
@property (nonatomic, strong) UIButton *locationBtn;
@property (nonatomic, strong) UIImage *imageLocated;
@property (nonatomic, strong) UIImage *imageNotLocate;
@property (nonatomic, assign) NSInteger searchPage;
@property (nonatomic, strong) UISegmentedControl *searchTypeSegment;
@property (nonatomic, copy) NSString *currentType;
@property (nonatomic, copy) NSArray *searchTypes;
@end
@implementation AMapViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *_title = @"位置";
NSString *_leftBtn = @"取消";
if (self.forceLocation) {
_title = @"";
_leftBtn = @"返回";
}
self.navigationItem.title = _title;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:_leftBtn style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];
self.navigationItem.leftBarButtonItem.tintColor = COLOR_S_BLACK;
[[AMapServices sharedServices] setEnableHTTPS:YES];
[AMapServices sharedServices].apiKey = AMapAppKey;
[self initAMapView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.mapView.showsCompass = NO;
self.mapView.showsScale = NO;
self.mapView.zoomLevel = 16.3;
//查看位置
if(!self.forceLocation){
if ((![self.longitude isEqualToString:@""] || ![self.latitude isEqualToString:@""])&& self.isSearch != YES ) {
CLLocationCoordinate2D location = CLLocationCoordinate2DMake([self.latitude floatValue], [self.longitude floatValue]);
[self.mapView setCenterCoordinate:location animated:YES];
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate = location;
[self.mapView addAnnotation:pointAnnotation];
[self.mapView selectAnnotation:pointAnnotation animated:NO];
}
}
}
#pragma mark - 初始化高德地图
- (void)initAMapView{
self.mapView = [[MAMapView alloc]initWithFrame:CGRectZero];
self.mapView.delegate = self;
[self.mainLayout addSubview:self.mapView];
[self initLocationButton];
// 带逆地理信息的一次定位(返回坐标和地址信息)
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
// 定位超时时间,最低2s,此处设置为2s
self.locationManager.locationTimeout =2;
// 逆地理请求超时时间,最低2s,此处设置为2s
self.locationManager.reGeocodeTimeout = 2;
//初始化界面
[self initSearchView];
[self initTableview];
[self initSearch];
if(!self.forceLocation){
[self initCenterView];
}
self.isLocated = NO;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reviewCenter:) name:@"noti_reviewCenter" object:nil];
if ([CLLocationManager locationServicesEnabled]) {
[[CLLocationManager new] requestWhenInUseAuthorization];
CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
[[AlertHandler sharedInstance]showAlert_default_title:nil message:@"无法获取你的位置信息。请到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务,并允许犀牛云商使用定位服务" delegate:self cancelButtonTitle:@"确定" tag:1];
}else{
self.mapView.showsUserLocation = YES;
}
}else{
[[AlertHandler sharedInstance]showAlert_default_title:nil message:@"无法获取你的位置信息。请到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务,并允许犀牛云商使用定位服务" delegate:self cancelButtonTitle:@"确定" tag:1];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)reviewCenter:(NSNotification *)noti{
self.currentTip = noti.object;
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(self.currentTip.location.latitude, self.currentTip.location.longitude);
self.search.delegate = self.tableview;
self.isSearch = YES;
self.isMapViewRegionChangedFromTableView = YES;
[self.mapView setCenterCoordinate:location animated:YES];
[self actionSearchAroundAt:location];
}
#pragma mark - 定位自身位置按钮
- (void)initLocationButton
{
self.imageLocated = [UIImage imageNamed:@"gpssearchbutton"];
self.imageNotLocate = [UIImage imageNamed:@"gpsnormal"];
self.locationBtn = [[UIButton alloc] initWithFrame:CGRectZero];
self.locationBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
self.locationBtn.backgroundColor = [UIColor whiteColor];
self.locationBtn.layer.borderColor = UIColorFromRGB(0x999999, 1.0).CGColor;
self.locationBtn.layer.borderWidth = 0.5;
self.locationBtn.layer.cornerRadius = 3;
[self.locationBtn addTarget:self action:@selector(actionLocation) forControlEvents:UIControlEventTouchUpInside];
[self.locationBtn setImage:self.imageNotLocate forState:UIControlStateNormal];
[self.mainLayout addSubview:self.locationBtn];
[self.locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mapView.mas_bottom).offset(-f_CalcDevHeightByiPhone7(15));
make.right.equalTo(self.mapView.mas_right).offset(-f_CalcDevWidthByiPhone7(15));
make.size.equalTo(CGSizeMake(f_CalcDevWidthByiPhone7(40), f_CalcDevWidthByiPhone7(40)));
}];
}
#pragma mark - 搜索框
- (void)initSearchView{
if(!self.forceLocation){
UIView *v_search = [[UIView alloc]init];
v_search.backgroundColor = COLOR_C_background;
[self.mainLayout addSubview:v_search];
[v_search mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mainLayout.mas_top).offset(TOP_OFFSET);
make.height.equalTo(f_CalcDevHeightByiPhone7(62));
make.centerX.equalTo(self.mainLayout);
make.width.equalTo(ScreenWidth);
}];
UILabel *lbl_search = [[UILabel alloc]init];
lbl_search.text = @"搜索地点";
lbl_search.font = FONT_S_14;
lbl_search.textColor = UIColorFromRGB(0x8e8e93, 1.0);
[self.mainLayout addSubview:lbl_search];
[lbl_search mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(v_search.mas_centerX);
make.centerY.equalTo(v_search.mas_centerY);
}];
UIImageView *img_search = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search"]];
[self.mainLayout addSubview:img_search];
[img_search mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lbl_search.mas_left).offset(-f_CalcDevWidthByiPhone7(10));
make.centerY.equalTo(v_search.mas_centerY);
make.height.equalTo(@(f_CalcDevHeightByiPhone7(14)));
make.width.equalTo(img_search.mas_height);
}];
UIButton *btn_search = [UIButton buttonWithType:UIButtonTypeCustom];
btn_search.layer.cornerRadius = 3;
btn_search.layer.masksToBounds = YES;
[btn_search addTarget:self action:@selector(searchPOIList:) forControlEvents:UIControlEventTouchUpInside];
btn_search.backgroundColor = COLOR_S_WHITE;
[v_search addSubview:btn_search];
[btn_search mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(v_search.mas_left).offset(f_CalcDevWidthByiPhone7(14));
make.right.equalTo(v_search.mas_right).offset(-f_CalcDevWidthByiPhone7(14));
make.centerY.equalTo(v_search.mas_centerY);
make.height.equalTo(@(f_CalcDevHeightByiPhone7(35)));
}];
[self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(v_search.mas_bottom);
make.height.equalTo(self.mainLayout.mas_height).multipliedBy(0.4);
make.centerX.equalTo(self.mainLayout);
make.width.equalTo(ScreenWidth);
}];
}else{
[self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.centerX.width.equalTo(self.mainLayout);
make.height.equalTo(self.mainLayout.mas_height).multipliedBy(0.5);
}];
}
}
#pragma mark - POI搜索代理设置
- (void)initSearch
{
self.searchPage = 1;
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self.tableview;
}
#pragma mark - POI列表
- (void)initTableview
{
self.tableview = [[PlaceAroundTableView alloc] initWithFrame:CGRectZero];
self.tableview.delegate = self;
[self.mainLayout addSubview:self.tableview];
[self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mapView.mas_bottom);
make.centerX.equalTo(self.mainLayout);
make.width.equalTo(ScreenWidth);
make.bottom.equalTo(self.mainLayout);
}];
}
- (void)initCenterView
{
self.centerAnnotationView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin"]];
[self.mapView addSubview:self.centerAnnotationView];
[self.centerAnnotationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mapView);
make.centerY.equalTo(self.mapView);
make.width.equalTo(@60);
make.height.equalTo(@81);
}];
}
#pragma mark - 搜索POI
- (void)searchPOIList:(UIButton *)sender{
SearchPOIListViewController *poiVC = [[SearchPOIListViewController alloc]init];
poiVC.cityAddress = self.tableview.cityAddress;
[self presentViewController:poiVC animated:NO completion:nil];
}
#pragma mark - 搜索周围
- (void)actionSearchAroundAt:(CLLocationCoordinate2D)coordinate
{
[self searchReGeocodeWithCoordinate:coordinate];
[self searchPoiWithCenterCoordinate:coordinate];
self.searchPage = 1;
[self centerAnnotationAnimimate];
}
#pragma mark - TableViewDelegate
- (void)didTableViewSelectedChanged:(AMapPOI *)selectedPoi
{
// 防止连续点两次
if(self.isMapViewRegionChangedFromTableView == YES)
{
return;
}
self.isMapViewRegionChangedFromTableView = YES;
if(selectedPoi.location.latitude > 0 && selectedPoi.location.longitude > 0){
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(selectedPoi.location.latitude, selectedPoi.location.longitude);
[self.mapView setCenterCoordinate:location animated:YES];
}
if (self.finshBlock) {
if(selectedPoi.location.latitude > 0 && selectedPoi.location.longitude > 0){
NSDictionary *_dic = [self l_setReturnDictionaryByAmapPOI:selectedPoi longitude:selectedPoi.location.longitude latitude:selectedPoi.location.latitude address:selectedPoi.name?selectedPoi.name:@"未知"];
self.finshBlock(_dic);
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[[AlertHandler sharedInstance] showToastHUD:self.mainLayout message:@"未获取到定位信息" sleepTime:Toast_Sleep];
}
}
}
- (void)didPositionCellTapped
{
// 防止连续点两次
if(self.isMapViewRegionChangedFromTableView == YES)
{
return;
}
self.isMapViewRegionChangedFromTableView = YES;
if (self.isSearch == YES) {
if(self.currentTip.location.latitude > 0 && self.currentTip.location.longitude > 0){
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(self.currentTip.location.latitude, self.currentTip.location.longitude);
[self.mapView setCenterCoordinate:location animated:YES];
self.isMapViewRegionChangedFromTableView = NO;
}
if (self.finshBlock) {
if(self.currentTip.location.longitude > 0 && self.currentTip.location.latitude > 0){
NSDictionary *_dic = [self l_setReturnDictionaryByAMapAddressComponent:self.tableview.cityAddress longitude:self.currentTip.location.longitude latitude:self.currentTip.location.latitude address:self.tableview.currentAddress?self.tableview.currentAddress:@"未知"];
self.finshBlock(_dic);
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[[AlertHandler sharedInstance] showToastHUD:self.mainLayout message:@"未获取到定位信息" sleepTime:Toast_Sleep];
}
}
}else{
if (self.finshBlock) {
if(self.mapView.centerCoordinate.longitude > 0 && self.mapView.centerCoordinate.latitude > 0){
NSDictionary *_dic = [self l_setReturnDictionaryByAMapAddressComponent:self.tableview.cityAddress longitude:self.mapView.centerCoordinate.longitude latitude:self.mapView.centerCoordinate.latitude address:self.tableview.currentAddress?self.tableview.currentAddress:@"未知"];
self.finshBlock(_dic);
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[[AlertHandler sharedInstance] showToastHUD:self.mainLayout message:@"未获取到定位信息" sleepTime:Toast_Sleep];
}
}
}
}
#pragma mark - MapViewDelegate
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if(!self.forceLocation){
if (!self.isMapViewRegionChangedFromTableView && self.mapView.userTrackingMode == MAUserTrackingModeNone)
{
[self actionSearchAroundAt:self.mapView.centerCoordinate];
}
self.isMapViewRegionChangedFromTableView = NO;
}
}
/* 根据中心点坐标来搜周边的POI. */
- (void)searchPoiWithCenterCoordinate:(CLLocationCoordinate2D )coord
{
AMapPOIAroundSearchRequest*request = [[AMapPOIAroundSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:coord.latitude longitude:coord.longitude];
request.sortrule = 0;
request.page = self.searchPage;
request.requireExtension = YES;
[self.search AMapPOIAroundSearch:request];
}
- (void)searchReGeocodeWithCoordinate:(CLLocationCoordinate2D)coordinate
{
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
regeo.requireExtension = YES;
[self.search AMapReGoecodeSearch:regeo];
}
- (void)didLoadMorePOIButtonTapped
{
self.searchPage++;
[self searchPoiWithCenterCoordinate:self.mapView.centerCoordinate];
}
#pragma mark - userLocation
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
if(!updatingLocation)
return ;
if (userLocation.location.horizontalAccuracy < 0)
{
return ;
}
// only the first locate used.
if (!self.isLocated)
{
self.isLocated = YES;
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude)];
[self actionSearchAroundAt:userLocation.location.coordinate];
}
}
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
{
if (mode == MAUserTrackingModeNone)
{
[self.locationBtn setImage:self.imageNotLocate forState:UIControlStateNormal];
}
else
{
[self.locationBtn setImage:self.imageLocated forState:UIControlStateNormal];
}
}
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"error = %@",error);
}
- (void)actionLocation
{
self.isSearch = NO;
if (self.mapView.userTrackingMode == MAUserTrackingModeFollow)
{
[self.mapView setUserTrackingMode:MAUserTrackingModeNone animated:YES];
}
else
{
self.searchPage = 1;
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// 因为下面这句的动画有bug,所以要延迟0.5s执行,动画由上一句产生
[self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
});
}
}
/* 移动窗口弹一下的动画 */
- (void)centerAnnotationAnimimate
{
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGPoint center = self.centerAnnotationView.center;
center.y -= 20;
[self.centerAnnotationView setCenter:center];}
completion:nil];
[UIView animateWithDuration:0.45
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
CGPoint center = self.centerAnnotationView.center;
center.y += 20;
[self.centerAnnotationView setCenter:center];}
completion:nil];
}
- (void)dismiss:(id)sender
{
if (self.navigationController.presentingViewController) {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}else{
[self.navigationController popViewControllerAnimated:YES];
}
}
/*
#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.
}
*/
-(NSDictionary *)l_setReturnDictionaryByAmapPOI:(nonnull AMapPOI *)object
longitude:(CGFloat)longitude
latitude:(CGFloat)latitude
address:(nonnull NSString *)address
{
NSMutableDictionary *_dic = [self l_initReturnDic];
_dic[@"longitude"] = [NSString stringWithFormat:@"%f",longitude];
_dic[@"latitude"] = [NSString stringWithFormat:@"%f",latitude];
_dic[@"address"] = address;
_dic[@"name"] = address;
_dic[@"province"] = object.province?object.province:@"";
_dic[@"urban"] = object.city?object.city:@"";
_dic[@"region"] = object.district?object.district:@"";
_dic[@"cityCode"] = object.citycode?object.citycode:@"";
_dic[@"adCode"] = object.adcode?object.adcode:@"";
return (NSDictionary *)_dic;
}
-(NSDictionary *)l_setReturnDictionaryByAMapAddressComponent:(nullable AMapAddressComponent *)object
longitude:(CGFloat)longitude
latitude:(CGFloat)latitude
address:(nonnull NSString *)address
{
NSMutableDictionary *_dic = [self l_initReturnDic];
_dic[@"longitude"] = [NSString stringWithFormat:@"%f",longitude];
_dic[@"latitude"] = [NSString stringWithFormat:@"%f",latitude];
_dic[@"address"] = address;
_dic[@"province"] = object.province?object.province:@"";
_dic[@"urban"] = object.city?object.city:@"";
_dic[@"region"] = object.district?object.district:@"";
_dic[@"cityCode"] = object.citycode?object.citycode:@"";
_dic[@"adCode"] = object.adcode?object.adcode:@"";
return (NSDictionary *)_dic;
}
-(NSMutableDictionary *)l_initReturnDic
{
// 目前考勤的RN中,只会识别longitude,latitude,address这三个值,其中address是完整地址
NSMutableDictionary *_dic = [NSMutableDictionary dictionaryWithDictionary:
@{
@"longitude":@"",
@"latitude":@"",
@"address":@"",
@"name":@"",
@"province":@"",
@"urban":@"",
@"region":@"",
@"cityCode":@"",
@"adCode":@"",
}
];
return _dic;
}
@end