|
|
@@ -0,0 +1,217 @@
|
|
|
+//
|
|
|
+// 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;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation AMShipAnnotation
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@interface AMShipMap () <MKMapViewDelegate>
|
|
|
+
|
|
|
+@property (nonatomic,strong) AMMapView *mapView;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation AMShipMap
|
|
|
+
|
|
|
+- (void)handleLoaction:(NSDictionary *)location Priority:(AMShipAnnotationPriority)priority {
|
|
|
+ if (location == nil) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ NSString* addr = [location valueForKey:@"addr"];
|
|
|
+ NSString* name = [location valueForKey:@"name"];
|
|
|
+ double longitude = [[location valueForKey:@"lon"] doubleValue];
|
|
|
+ double latitude = [[location valueForKey:@"lat"] doubleValue];
|
|
|
+
|
|
|
+ // 创建大头针(标注)的数据模型(此处不创建视图,视图通过MKMapView的委托设置回调方法来生成的)
|
|
|
+ AMShipAnnotation *ann = [[AMShipAnnotation alloc] init];
|
|
|
+ CLLocationCoordinate2D c2d = CLLocationCoordinate2DMake(latitude, longitude);
|
|
|
+ // 指定大头针的经纬度坐标(位置)以及附加的信息
|
|
|
+ ann.coordinate = c2d;
|
|
|
+ ann.title = name;
|
|
|
+ ann.subtitle = addr;
|
|
|
+ ann.priority = priority;
|
|
|
+
|
|
|
+ // 将大头针数据模型添加到MKMapView上管理
|
|
|
+ [self.mapView addAnnotation:ann];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)showShipAnnotaion:(NSDictionary *)annotation {
|
|
|
+
|
|
|
+ if (annotation == nil) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ [self.mapView removeAnnotations:self.mapView.annotations];
|
|
|
+ NSDictionary *locationInfo = annotation;
|
|
|
+
|
|
|
+ // pol,pod,poe,por,origin,destination,current
|
|
|
+ if (locationInfo) {
|
|
|
+ NSDictionary *pol = [locationInfo objectForKey:@"pol"];
|
|
|
+ if (self.showPol) {
|
|
|
+ [self handleLoaction:pol Priority:AMShipAnnotationPriorityHigh];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *pod = [locationInfo objectForKey:@"pod"];
|
|
|
+ if (self.showPod) {
|
|
|
+ [self handleLoaction:pod Priority:AMShipAnnotationPriorityHigh];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *poe = [locationInfo objectForKey:@"poe"];
|
|
|
+ if (self.showPoe) {
|
|
|
+ [self handleLoaction:poe Priority:AMShipAnnotationPriorityLow];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *por = [locationInfo objectForKey:@"por"];
|
|
|
+ if (self.showPor) {
|
|
|
+ [self handleLoaction:por Priority:AMShipAnnotationPriorityLow];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *origin = [locationInfo objectForKey:@"origin"];
|
|
|
+ if (self.showOrigin) {
|
|
|
+ [self handleLoaction:origin Priority:AMShipAnnotationPriorityLow];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *destination = [locationInfo objectForKey:@"destination"];
|
|
|
+ if (self.showDestination) {
|
|
|
+ [self handleLoaction:destination Priority:AMShipAnnotationPriorityLow];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *current = [locationInfo objectForKey:@"current"];
|
|
|
+ if (self.showCurrent) {
|
|
|
+ [self handleLoaction:current Priority:AMShipAnnotationPriorityRequired];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#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"];
|
|
|
+ }
|
|
|
+ pinView.image = [UIImage imageNamed:@"ic_marker"];
|
|
|
+ AMShipAnnotation *shipAnnotation = (AMShipAnnotation *)annotation;
|
|
|
+ if (@available(iOS 11,*)) {
|
|
|
+ if (shipAnnotation.priority == AMShipAnnotationPriorityRequired) {
|
|
|
+ pinView.displayPriority = MKFeatureDisplayPriorityRequired;
|
|
|
+ } else if (shipAnnotation.priority == AMShipAnnotationPriorityHigh) {
|
|
|
+ pinView.displayPriority = MKFeatureDisplayPriorityDefaultHigh;
|
|
|
+ } else {
|
|
|
+ pinView.displayPriority = MKFeatureDisplayPriorityDefaultLow;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return pinView;
|
|
|
+ } else {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+
|
|
|
+// return nil;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Life
|
|
|
+
|
|
|
+- (void)setup {
|
|
|
+
|
|
|
+ [self setupMap];
|
|
|
+
|
|
|
+ self.showPol = YES;
|
|
|
+ self.showPod = YES;
|
|
|
+ self.showCurrent = 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
|