//
// 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 ()
@property (nonatomic, strong) MAMapView mapView;
/* 属性 - 添加模型数据 */
@property (nonatomic, strong) NSMutableArray
/ 属性 - 定位管理者 /
@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
)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