自定義大頭針

//
// 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;
/
* 屬性 - 添加模型數(shù)據(jù) */
@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;
    }
    /
    *
  • 懶加載 - 數(shù)據(jù)源
    */
  • (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];

}
/**

  • 創(chuàng)建假數(shù)據(jù)
    */
  • (void)creatDummyDataFile {
    /* 創(chuàng)建右邊的二維碼 */
    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];
    }
    /
  • 創(chuàng)建右邊點擊 -
    */
  • (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 - 關(guān)聯(lián)方法

/**

  • 關(guān)聯(lián)二維碼 -
    */
  • (void)clickWithLeftButton:(UIButton )leftBtn {
    NSLog(@"二維碼");
    }
    /
    *
  • 切換 - 關(guān)聯(lián)方法
    */
  • (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;
    }
    /

  • 連續(xù)定位回調(diào)函數(shù)
  • 注意:如果實現(xiàn)了本方法,則定位信息不會通過amapLocationManager:didUpdateLocation:方法回調(diào)。
  • @param manager 定位 AMapLocationManager 類。
  • @param location 定位結(jié)果。
  • @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:^{
    
                  }];
              };
              // 需要一個數(shù)組下標
          [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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,818評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,185評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,656評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,647評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,446評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,951評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,041評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,189評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,718評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,800評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,419評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,420評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,755評論 2 371

推薦閱讀更多精彩內(nèi)容