自定义大头针

//
// FindViewController.m
// AllCloud
//
// Created by 韩旭 on 2017/8/30.
// Copyright © 2017年 韩旭. All rights reserved.
//

import "AllCloudFrame.h"

import "FindShopListViewController.h"

import "MyAnnotation.h"

import "CustomAlertViewOfMap.h"

import "CustomPinAnnotationView.h"

import "FindViewController.h"

import "AllCloudHeadFile.h"

import "Masonry.h"

@import CoreLocation;
@import MapKit;

@interface FindViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate>

@property (nonatomic, strong) MAMapView mapView;
/
* 属性 - 添加模型数据 */
@property (nonatomic, strong) NSMutableArray<MyAnnotation > myAnnotationModel;
/
属性 - 定位管理者 /
@property (nonatomic, strong) AMapLocationManager locationManger;
/
属性 - 微商地图点击 */
@property (nonatomic, strong) UIButton *weiChatConsult;

@property (nonatomic , strong) MAPointAnnotation *pointMation;

@property (nonatomic, strong) NSString *urlScheme;
@property (nonatomic, strong) NSString *appName;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) MyAnnotation *annotation;

@end
@implementation FindViewController

  • (MyAnnotation *)annotation {
    if (!_annotation) {
    _annotation = [MyAnnotation new];
    }
    return _annotation;
    }
  • (UIButton )weiChatConsult {
    if (!_weiChatConsult) {
    _weiChatConsult = [UIButton buttonWithType:UIButtonTypeCustom];
    [_weiChatConsult setImage:[UIImage imageNamed:@"IW_weishangmap"] forState:UIControlStateNormal];
    [_weiChatConsult setImage:[UIImage imageNamed:@"IW_shq"] forState:UIControlStateSelected];
    [_weiChatConsult addTarget:self action:@selector(clickWithWeichatContanstButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weiChatConsult;
    }
    /
    *
  • 懒加载 - 定位管理者
    */
  • (AMapLocationManager )locationManger {
    if (!_locationManger) {
    _locationManger = [[AMapLocationManager alloc] init];
    _locationManger.delegate = self;
    }
    return _locationManger;
    }
    /
    *
  • 懒加载 - 数据源
    */
  • (NSMutableArray )myAnnotationModel {
    if (!_myAnnotationModel) {
    _myAnnotationModel = [NSMutableArray array];
    }
    return _myAnnotationModel;
    }
    /
    *
  • 懒加载
    */
  • (MAMapView )mapView {
    if (!_mapView) {
    _mapView = [[MAMapView alloc] initWithFrame:SCREEN_RECT];
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    _mapView.showsCompass = NO;
    }
    return _mapView;
    }
    /
    *
  • 视图显示
    */
  • (void)viewDidLoad {
    [super viewDidLoad];
    // [self creatDummyDataFile];
    [self creatRightNavgiationItem];
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.weiChatConsult];
    [self.weiChatConsult mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.mas_offset(-20);
    make.top.mas_offset(15);
    }];
    [self.locationManger startUpdatingLocation];

}
/**

  • 创建假数据
    */
  • (void)creatDummyDataFile {
    /* 创建右边的二维码 */
    NSString *arrFile = [[NSBundle mainBundle] pathForResource:@"LocationMap" ofType:@"plist"];
    NSDictionary *arr = [NSDictionary dictionaryWithContentsOfFile:arrFile];
    NSArray *arrLocation = arr[@"arrLocationArr"];
    for (NSDictionary json in arrLocation) {
    MyAnnotation annotation = [[MyAnnotation alloc] initWithAnnotationModelWithDict:json];
    [self.myAnnotationModel addObject:annotation];
    }
    [self.mapView addAnnotations:self.myAnnotationModel];
    [self.locationManger startUpdatingLocation];
    }
    /
  • 创建右边点击 -
    */
  • (void)creatRightNavgiationItem {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [leftBtn setImage:[UIImage imageNamed:@"IW_scan"] forState:UIControlStateNormal];
    leftBtn.frame = CGRectMake(0, 0, 35, 35);
    [leftBtn addTarget:self action:@selector(clickWithLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }

pragma mark - 关联方法

/**

  • 关联二维码 -
    */
  • (void)clickWithLeftButton:(UIButton )leftBtn {
    NSLog(@"二维码");
    }
    /
    *
  • 切换 - 关联方法
    */
  • (void)clickWithWeichatContanstButton:(UIButton *)weichat {
    weichat.selected = !weichat.selected;
    FindShopListViewController *shopListVc = [FindShopListViewController new];
    [self.navigationController pushViewController:shopListVc animated:YES];
    }

pragma mark - 地图代理方法

/**

  • 自定义 - 大头针
    */
  • (nullable MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {

    if ([annotation isKindOfClass:[MAUserLocation class]]) {
    MAAnnotationView *annotationView = [[MAAnnotationView alloc] init];
    annotationView.image = [UIImage imageNamed:@"shangjia2"];
    annotationView.canShowCallout = YES;
    return annotationView;
    }
    CustomPinAnnotationView *customPinAnnotation = (CustomPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"customPinAnnotation"];
    if (customPinAnnotation == nil) {
    customPinAnnotation = [[CustomPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customPinAnnotation"];
    }
    MyAnnotation myAnnotation = (MyAnnotation )annotation;
    customPinAnnotation.image = [UIImage imageNamed:@"shangjia2"];
    [customPinAnnotation.imageView sd_setImageWithURL:[NSURL URLWithString:myAnnotation.shop_head_url] placeholderImage:[UIImage imageNamed:@"shangjia2"]];
    return customPinAnnotation;
    }
    /

  • 连续定位回调函数
  • 注意:如果实现了本方法,则定位信息不会通过amapLocationManager:didUpdateLocation:方法回调。
  • @param manager 定位 AMapLocationManager 类。
  • @param location 定位结果。
  • @param reGeocode 逆地理信息。
    */
  • (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {

    [self.mapView setRegion:MACoordinateRegionMake(CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), MACoordinateSpanMake(0.15, 0.15)) animated:YES];

    NSString *body = [NSString stringWithFormat:@"lng=%@&lat=%@&adcode=%@", [NSString stringWithFormat:@"%lf", location.coordinate.longitude],[NSString stringWithFormat:@"%lf", location.coordinate.latitude] , @"220100"];
    [HXNetTool postNetWithUrl:@"http://192.168.31.10/YiDianYuanService/find_merchant/query.do" body:body BodyStyle:HXBodyString Header:nil Response:HXJSON Success:^(id result) {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
    NSLog(@"%@", json);
    IWStatus *status = [IWStatus mj_objectWithKeyValues:json];
    if ([status.status isEqualToString:@"0000"]) {
    for (NSDictionary *jsonDic in json[@"object"]) {
    MyAnnotation *annotation = [[MyAnnotation alloc] initWithAnnotationModelWithDict:jsonDic];
    [self.myAnnotationModel addObject:annotation];
    }
    [self.mapView addAnnotations:self.myAnnotationModel];
    }
    } Failure:^(NSError *error) {

    }];
    [manager stopUpdatingLocation];
    }

  • (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    [self.mapView deselectAnnotation:view.annotation animated:YES];
    NSArray *array = [NSArray arrayWithArray:self.mapView.annotations];

    for (MyAnnotation *annotation in array) {
    if (![annotation isKindOfClass:[MAUserLocation class]]) {
    if (view.annotation.coordinate.latitude == annotation.coordinate.latitude) {
    CustomAlertViewOfMap *customView = [[CustomAlertViewOfMap alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    customView.animationModel = annotation;
    __weak typeof(self)weakSelf = self;
    __weak CustomAlertViewOfMap *customViewWeak = customView;
    customView.goinShopBlock = ^{
    [customViewWeak removeFromSuperview];
    FindShopListViewController *shopListVc = [FindShopListViewController new];
    [weakSelf.navigationController pushViewController:shopListVc animated:YES];
    };
    customView.goinMapNavigationBlock = ^{
    [customViewWeak removeFromSuperview];
    self.urlScheme = @"WangNuoAllCloud123321";
    self.appName = @"全民云";
    self.coordinate = CLLocationCoordinate2DMake(view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);

                  __block NSString *urlScheme = self.urlScheme;
                  __block NSString *appName = self.appName;
                  __block CLLocationCoordinate2D coordinate = self.coordinate;
    
                  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
                  //这个判断其实是不需要的
                  if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://maps.apple.com/"]])
                  {
                      UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
                          MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
                          MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
    
                          [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                                         launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
                      }];
    
                      [alert addAction:action];
                  }
    
                  if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
                  {
                      UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
                          NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
                          //            NSLog(@"%@",urlString);
    
                          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    
                      }];
    
                      [alert addAction:action];
                  }
    
    
                  if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
                  {
                      UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
                          NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
                          //            NSLog(@"%@",urlString);
    
                          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    
                      }];
    
                      [alert addAction:action];
                  }
                  UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                  [alert addAction:action];
    
                  [self presentViewController:alert animated:YES completion:^{
    
                  }];
              };
              // 需要一个数组下标
          [LTCustomSuperView addSubview:customView];
          }
      }
    

    }
    }

  • (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction {

}

  • (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
    self.annotation.coordinate = coordinate;
    self.annotation.image = @"shangjia2";
    [self.mapView addAnnotation:self.annotation];
    [self.mapView addAnnotations:self.myAnnotationModel];
    }
  • (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

}
@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容