| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- //
- // AMShipMap.m
- // Apex Mobile
- //
- // Created by Jack on 2018/5/4.
- // Copyright © 2018年 United Software Applications, Inc. All rights reserved.
- //
- #import "AMShipMap.h"
- #import <MapKit/MapKit.h>
- #import "AMMapView.h"
- #import "AMAnnotationView.h"
- typedef enum {
-
- AMShipAnnotationPriorityRequired,
- AMShipAnnotationPriorityHigh,
- AMShipAnnotationPriorityLow
-
- } AMShipAnnotationPriority;
- @interface AMShipAnnotation : MKPointAnnotation
- @property (nonatomic,assign) AMShipAnnotationPriority priority;
- @property (nonatomic,copy) NSString *imageName;
- @property (nonatomic,assign) BOOL isCurrent;
- @end
- @implementation AMShipAnnotation
- @end
- @interface AMShipMap () <MKMapViewDelegate>
- {
- MKAnnotationView *_currentAnnotaion;
- NSTimer *_twinkleTimer;
- float _timerAngle;
- }
- @property (nonatomic,strong) AMMapView *mapView;
- @property (nonatomic,strong) UIButton *resizeBtn;
- @end
- @implementation AMShipMap
- - (void)handleLoaction:(NSDictionary *)location Priority:(AMShipAnnotationPriority)priority ImageName:(NSString *)imgName {
- if (location == nil) {
- return;
- }
- NSString *port = [location valueForKey:@"port"];
- NSString* addr = [location valueForKey:@"addr"];
- NSString* name = [location valueForKey:@"name"];
- NSString *lonStr = [location valueForKey:@"lon"];
- NSString *latStr = [location valueForKey:@"lat"];
- if (lonStr && lonStr.length > 0 && latStr && latStr.length > 0) {
-
- double longitude = [lonStr doubleValue];
- double latitude = [latStr doubleValue];
-
- if (port == nil) {
- port = @"";
- }
-
- if (addr == nil) {
- addr = @"";
- }
-
- // 创建大头针(标注)的数据模型(此处不创建视图,视图通过MKMapView的委托设置回调方法来生成的)
- AMShipAnnotation *ann = [[AMShipAnnotation alloc] init];
- CLLocationCoordinate2D c2d = CLLocationCoordinate2DMake(latitude, longitude);
- // 指定大头针的经纬度坐标(位置)以及附加的信息
- ann.coordinate = c2d;
- ann.title = name;
- ann.subtitle = [NSString stringWithFormat:@"%@ %@",port,addr];
- ann.priority = priority;
- ann.imageName = imgName;
-
- if ([port isEqualToString:@"Current"]) {
- ann.isCurrent = YES;
- }
-
- // 将大头针数据模型添加到MKMapView上管理
- [self.mapView addAnnotation:ann];
-
- } else {
- return;
- }
- }
- - (void)moveToLocation:(NSDictionary *)location {
- if (location == nil) {
- return;
- }
-
- NSString *lonStr = [location valueForKey:@"lon"];
- NSString *latStr = [location valueForKey:@"lat"];
- if (lonStr && lonStr.length > 0 && latStr && latStr.length > 0) {
-
- double longitude = [lonStr doubleValue];
- double latitude = [latStr doubleValue];
-
- CLLocationCoordinate2D c2d = CLLocationCoordinate2DMake(latitude, longitude);
-
- MKCoordinateSpan span = MKCoordinateSpanMake(180, 360);
- // 创建一个区域结构体变量
- MKCoordinateRegion region = MKCoordinateRegionMake(c2d, span);
-
- // 将大头针数据模型添加到MKMapView上管理
- [self.mapView setRegion:region animated:YES];
-
- }
-
- }
- - (void)showShipAnnotaion:(NSDictionary *)annotation backupLocation:(NSString*)port{
-
- if (annotation == nil) {
- return;
- }
- [self.mapView removeAnnotations:self.mapView.annotations];
-
- NSDictionary *locationInfo = annotation;
- if (_currentAnnotaion) {
- // [self stopTwinkle];
- _currentAnnotaion.alpha = 1.f;
- _currentAnnotaion = nil;
- }
-
- // pol,pod,poe,por,origin,destination,current
- if (locationInfo) {
- NSMutableDictionary *pol = [[locationInfo objectForKey:@"pol"] mutableCopy];
- if (self.showPol) {
- [pol setObject:@"Port Of Load" forKey:@"port"];
- [self handleLoaction:pol Priority:AMShipAnnotationPriorityHigh ImageName:@"new_location_pol"];
- }
-
- NSMutableDictionary *pod = [[locationInfo objectForKey:@"pod"] mutableCopy];
- if (self.showPod) {
- [pod setObject:@"Port Of Discharge" forKey:@"port"];
- [self handleLoaction:pod Priority:AMShipAnnotationPriorityHigh ImageName:@"new_location_pod"];
- }
-
- NSMutableDictionary *poe = [[locationInfo objectForKey:@"poe"] mutableCopy];
- if (self.showPoe) {
- [poe setObject:@"Place Of Deliver" forKey:@"port"];
- [self handleLoaction:poe Priority:AMShipAnnotationPriorityLow ImageName:@"new_location_poe"];
- }
-
- NSMutableDictionary *por = [[locationInfo objectForKey:@"por"] mutableCopy];
- if (self.showPor) {
- [por setObject:@"Place Of Receipt" forKey:@"port"];
- [self handleLoaction:por Priority:AMShipAnnotationPriorityLow ImageName:@"new_location_por"];
- }
-
- NSMutableDictionary *origin = [[locationInfo objectForKey:@"origin"] mutableCopy];
- if (self.showOrigin) {
- [origin setObject:@"Origin" forKey:@"port"];
- [self handleLoaction:origin Priority:AMShipAnnotationPriorityLow ImageName:@"new_location_origin"];
- }
-
- NSMutableDictionary *destination = [[locationInfo objectForKey:@"destination"] mutableCopy];
- if (self.showDestination) {
- [destination setObject:@"Destination" forKey:@"port"];
- [self handleLoaction:destination Priority:AMShipAnnotationPriorityLow ImageName:@"new_location_destination"];
- }
-
- NSMutableDictionary *current = [[locationInfo objectForKey:@"current"] mutableCopy];
- if (self.showCurrent) {
- [current setObject:@"Current" forKey:@"port"];
- [self handleLoaction:current Priority:AMShipAnnotationPriorityRequired ImageName:@"ic_marker"];
- }
- if (current && self.showCurrent) {
- [self moveToLocation:current];
-
- } else if ([port isEqualToString:@"pol"]&& pol && self.showPol) {
- [self moveToLocation:pol];
- } else if ([port isEqualToString:@"pod"]&&pod && self.showPod) {
- [self moveToLocation:pod];
- }
- }
-
- // [self startTwinkle];
- }
- #pragma mark - Twinkle Current
- - (void)startTwinkle {
- if (_twinkleTimer) {
- return;
- }
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
- strongSelf->_twinkleTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(twinkleTimerExecute:) userInfo:nil repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:strongSelf->_twinkleTimer forMode:NSDefaultRunLoopMode];
- [[NSRunLoop currentRunLoop] run];
- }
- });
- }
- - (void)twinkleTimerExecute:(NSTimer *)timer {
- if (!_currentAnnotaion) {
- return;
- }
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_main_queue(), ^{
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
-
- strongSelf->_timerAngle += 0.1;
- if (strongSelf->_timerAngle == 3.1) {
- strongSelf->_timerAngle = 0;
- }
- float alpha = ABS(cos(strongSelf->_timerAngle));
- strongSelf->_currentAnnotaion.alpha = alpha;
- }
- });
- }
- - (void)stopTwinkle {
- if (_twinkleTimer) {
- [_twinkleTimer invalidate];
- _twinkleTimer = nil;
- }
- }
- #pragma mark - Apear
- - (void)shipMapWillAppear {
- if (self.twinkleCurrent) {
- [self startTwinkle];
- }
- }
- - (void)shipMapWillDisappear {
- [self stopTwinkle];
- }
- #pragma mark - Map Delegate
- // 当地图上添加了标注以后要执行的回调方法
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
-
- if ([annotation isKindOfClass:[AMShipAnnotation class]]) {
- // 获取可复用的大头针视图
- AMAnnotationView *pinView = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
-
- if (!pinView) { // 如果没有可重用的大头针视图就创建并指定重用标识
- pinView = [[AMAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];
- }
- AMShipAnnotation *shipAnnotation = (AMShipAnnotation *)annotation;
-
- if (shipAnnotation.imageName) {
- pinView.image = [UIImage imageNamed:shipAnnotation.imageName];
- } else {
- pinView.image = [UIImage imageNamed:@"ic_marker"];
- }
-
- if (@available(iOS 11,*)) {
- if (shipAnnotation.priority == AMShipAnnotationPriorityRequired) {
- pinView.displayPriority = MKFeatureDisplayPriorityRequired;
- } else if (shipAnnotation.priority == AMShipAnnotationPriorityHigh) {
- pinView.displayPriority = MKFeatureDisplayPriorityDefaultHigh;
- } else {
- pinView.displayPriority = MKFeatureDisplayPriorityDefaultLow;
- }
- }
-
- if (shipAnnotation.isCurrent) {
- _currentAnnotaion = pinView;
- }
-
- return pinView;
- } else {
- return nil;
- }
-
- // return nil;
- }
- #pragma mark - Action
- - (void)resizeButtonClick:(UIButton *)sender {
- sender.selected = !sender.selected;
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(shipMap:tryToZoomIn:)]) {
- [self.delegate shipMap:self tryToZoomIn:sender.selected];
- }
- }
- #pragma mark - Setter
- - (void)setShowZoomControl:(BOOL)showZoomControl {
- _showZoomControl = showZoomControl;
- self.resizeBtn.hidden = !_showZoomControl;
- }
- #pragma mark - Life
- - (void)dealloc {
-
- [self stopTwinkle];
- }
- - (void)setup {
-
- [self setupMap];
-
- self.resizeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [self.resizeBtn setImage:[UIImage imageNamed:@"resize_max"] forState:UIControlStateNormal];
- [self.resizeBtn setImage:[UIImage imageNamed:@"resize_min"] forState:UIControlStateSelected];
- [self.resizeBtn addTarget:self action:@selector(resizeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
- self.resizeBtn.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:self.resizeBtn];
-
- NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.resizeBtn
- attribute:NSLayoutAttributeTop
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeTop
- multiplier:1.0
- constant:10];
- NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.resizeBtn
- attribute:NSLayoutAttributeRight
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeRight
- multiplier:1.0
- constant:-10];
-
- [self addConstraints:@[top,right]];
-
- [self layoutIfNeeded];
-
- self.showPol = YES;
- self.showPod = YES;
- self.showCurrent = YES;
- self.showZoomControl = YES;
- self.twinkleCurrent = YES;
- }
- - (void)setupMap {
-
- self.mapView = [[AMMapView alloc] init];
- self.mapView.delegate = self;
- self.mapView.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:self.mapView];
- self.mapView.rotateEnabled = NO;
- // 显示比例尺
- self.mapView.showsScale = YES;
- // 显示用户的位置
- // self.mapView.showsUserLocation = YES;
-
- NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.mapView
- attribute:NSLayoutAttributeTop
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeTop
- multiplier:1.0
- constant:0];
- NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.mapView
- attribute:NSLayoutAttributeLeft
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeLeft
- multiplier:1.0
- constant:0];
- NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.mapView
- attribute:NSLayoutAttributeBottom
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeBottom
- multiplier:1.0
- constant:0];
- NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.mapView
- attribute:NSLayoutAttributeRight
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeRight
- multiplier:1.0
- constant:0];
- [self addConstraints:@[top,left,bottom,right]];
- }
- - (instancetype)init {
- if (self = [super init]) {
- [self setup];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- [self setup];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self setup];
- }
- return self;
- }
- @end
|