zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
//
//  LinPullTopViewController.m
//  LINPullTableView
//
//  Created by earnestLin on 2019/1/16.
//  Copyright © 2019年 earnestLin. All rights reserved.
//
 
#import "LinPullTopViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "PBProjectModel.h"
#import "PBAnnotationView.h"
#import "PBPointAnnotation.h"
 
 
@interface LinPullTopViewController ()<MAMapViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIButton *leftBackButton;
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) NSArray *dataList;
@end
 
@implementation LinPullTopViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self loadProjectList];
}
- (void)addProjectAnnotation {
    for (PBProjectModel *obj in self.dataList) {
        PBPointAnnotation *pointAnnotation = [[PBPointAnnotation alloc] init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake(obj.bog_latitude, obj.bog_longitude);
        pointAnnotation.projectModel = obj;
        [_mapView addAnnotation:pointAnnotation];
    };
}
- (void)loadProjectList {
    [YJProgressHUD showProgress:@"正在加载项目数据" inView:self.view];
    [[PBNetworkTools sharedTools] getProjectListWithKeyword:@"" andIsPublic:@"" andSort:@"CreateDate desc" andSkip:@"0" andtake:@"999" andOnlyFavorite:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if(error) {
            NSLog(@"%@",error);
            [YJProgressHUD showMessage:@"加载项目列表失败" inView:self.view];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            self.dataList = [NSArray yy_modelArrayWithClass:[PBProjectModel class] json:[networkModel.Data valueForKey:@"rows"]];
            [self addProjectAnnotation];
        }else {
            [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
        }
    }];
}
#pragma mark - setupUI
 
- (void)setupUI {
//    self.view.backgroundColor = [UIColor whiteColor];
//    UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    imgView.image = [UIImage imageNamed:@"topviewBg"];
//    [self.view addSubview:imgView];
//    
//    self.leftBackButton = [[UIButton alloc] init];
//    self.leftBackButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
//    [self.leftBackButton setImage:[UIImage imageNamed:@"icon_return"]
//                     forState:UIControlStateNormal];
//    
//    [self.view addSubview:self.leftBackButton];
//    [self.leftBackButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).offset(0);
//        make.top.equalTo(self.view).offset(20);
//        make.height.width.mas_equalTo(60);
//    }];
//    
//    [self.leftBackButton addTarget:self
//                        action:@selector(leftBackButtonAction:)
//              forControlEvents:UIControlEventTouchUpInside];
//    
//    
    
    [AMapServices sharedServices].enableHTTPS = YES;
    self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.delegate = self;
    self.mapView.zoomLevel = 3; //3 ~ 9
    self.mapView.showsCompass = NO;
    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(23.901512367, 106.631821404);
    [self.view addSubview:self.mapView];
    
    UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
    mTap.delegate = self;
    [self.mapView addGestureRecognizer:mTap];
}
 
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {
//    CGPoint touchPoint = [gestureRecognizer locationInView:_mapView];//这里touchPoint是点击的某点在地图控件中的位置
//    CLLocationCoordinate2D  secondLocation = [_mapView convertPoint:touchPoint toCoordinateFromView:_mapView];//这里touchMapCoordinate就是该点的经纬度了
//    NSLog(@"%f=======%f",secondLocation.latitude,secondLocation.longitude);
    if (self.ShutDetailsBlock) {
        self.ShutDetailsBlock();
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
#pragma mark - event response
 
//- (void) leftBackButtonAction:(UIButton *)sender
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
 
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        PBPointAnnotation *point = annotation;
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[PBAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
        }
        if (point.projectModel.isFavorite) {
            annotationView.image = [UIImage imageNamed:@"red"];
        }else {
            annotationView.image = [UIImage imageNamed:@"blue"];
        }
        
        //设置中心点偏移,使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;
}
 
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    NSArray * array = [NSArray arrayWithArray:_mapView.annotations];
    for (int i=0; i<array.count; i++) {
       PBPointAnnotation *pointAnnotation = array[i];
       if (view.annotation.coordinate.latitude == pointAnnotation.coordinate.latitude) {
           NSLog(@"%@",pointAnnotation.projectModel.fullname);
           [self changeCenterTo:view.annotation.coordinate];
           if (self.OpenDetailsBlock) {
               self.OpenDetailsBlock(pointAnnotation.projectModel);
           }
       }
    }
}
- (void)changeCenterTo:(CLLocationCoordinate2D)newLoc {
    [self.mapView setCenterCoordinate:newLoc animated:YES];
}
- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
    for (PBPointAnnotation *annotation in _mapView.annotations) {
        NSLog(@"%@",annotation.projectModel.bimcomposerid);
        if([annotation.projectModel.bimcomposerid isEqual:projectModel.bimcomposerid]) {
            [_mapView removeAnnotation:annotation];
            PBPointAnnotation *pointAnnotation = [[PBPointAnnotation alloc] init];
            pointAnnotation.coordinate = CLLocationCoordinate2DMake(projectModel.bog_latitude, projectModel.bog_longitude);
            pointAnnotation.projectModel = projectModel;
            [_mapView addAnnotation:pointAnnotation];
        }
    }
}
@end