美文网首页
自定义大头针

自定义大头针

作者: J_HX | 来源:发表于2017-09-25 18:30 被阅读45次

//
// 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

相关文章

  • 大头针Annotation

    自定义大头针 使用大头针 点击添加大头针 自定义大头针颜色和动画 系统大头针的处理 自定义大头针图片 修改大头针模...

  • iOS 地图开发(MapKit)(二)

    相关类的介绍:MKAnnotation(大头针协议)大头针数据类(自定义的大头针需要遵守大头针协议)MKPoint...

  • iOS 系统自带API地图开发相关(二)

    地图-自定义大头针 关于大头针视图分两类: MKPinAnnotationView : apple自带的大头针,可...

  • 自定义大头针

    自定义大头针其实没什么东西,讲讲简单的自定义大头针吧! 1.需要定义大头针模型(里面至少有三个属性)#import...

  • 高德地图---iOS笔记摘录

    概念 使用 地图显示 交互 显示 计算 其他 1.1 大头针 自定义大头针: MAPointAnnotation ...

  • 百度地图大头针点击之后不调用mapView: viewForAn

    1.如何你自定义的大头针,看看大头针的UIImageView的userInteractionEnabled属性有没...

  • 地图高级 - 自定义大头针

    地图高级 - 自定义大头针 1. 理论支撑 2. 模拟实现系统大头针 实现当添加大头针数据模型时,地图回调的代理方...

  • iOS 自定义大头针

    一、自定义大头针 需求 需求:鼠标拖拽是在地图的哪个位置, 就在对应的位置加一个大头针,并反地理编码大头针所在城市...

  • 高德地图calloutView点击无响应,解决办法

    因为自定义的气泡是添加到大头针上的,而大头针的size只有下面很小一部分,所以calloutView是在大头针的外...

  • 自定义大头针Annotation

    (1)自定义大头针Annotation的样式,也就是定义view,主要的方法是如下,传递一个大头针annotati...

网友评论

      本文标题:自定义大头针

      本文链接:https://www.haomeiwen.com/subject/wnmdextx.html