数据来源:https://www.juhe.cn/docs/api/id/45
//
// HotelController.m
//
//
// Created by yinbo on 16/9/21.
// Copyright © 2016年 yinbo. All rights reserved.
//
#import "HotelController.h"
#import "Hotel.h"
@interface HotelController ()<MAMapViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)IBOutlet MAMapView *mapView;
@property(nonatomic,strong)NSMutableArray *annotationArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mapHeightLayout;
@property(nonatomic,strong)Hotel *selectHotel;
@property(nonatomic,assign)int selectIndex;
@end
@implementation HotelController
- (void)viewDidLoad {
[super viewDidLoad];
self.annotationArr=[[NSMutableArray alloc]initWithCapacity:0];
self.mapView.zoomLevel=14;
self.mapView.userTrackingMode = MAUserTrackingModeNone;
self.mapView.mapType=MAMapTypeStandard;
self.mapView.delegate=self;
self.mapHeightLayout.constant=YBScreenBoundsHeight-64;
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[[NSUserDefaults standardUserDefaults] valueForKey:@"lat"] doubleValue],[[[NSUserDefaults standardUserDefaults] valueForKey:@"long"] doubleValue]);
MACoordinateRegion region;
region.center.longitude = coordinate.longitude;
region.center.latitude = coordinate.latitude;
//缩放比例
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
self.mapView.region=region;
self.mapView.centerCoordinate=coordinate ;
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"自己";
[self.mapView addAnnotation:annotation];
[self.mapView selectAnnotation:annotation animated:YES];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mapTapDown)];
tap.delegate=self;
[self.mapView addGestureRecognizer:tap];
[self createNavWithLeftBtnImageName:@"navbar_back" leftBtnSelector:@selector(navbackBtnDonw) andCenterTitle:@"周边餐饮" isButtonOrNot:NO];
[self Requstdate];
//给contentview加手势
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe)];
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.contentview addGestureRecognizer:leftSwipeGesture];
UISwipeGestureRecognizer *rightSwipeGesture =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.contentview addGestureRecognizer:rightSwipeGesture];
UISwipeGestureRecognizer *downSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(downSwipe)];
downSwipeGesture.direction=UISwipeGestureRecognizerDirectionDown;
[self.contentview addGestureRecognizer:downSwipeGesture];
// Do any additional setup after loading the view from its nib.
}
-(void)navbackBtnDonw{
[YYToolClass playSpeechSound:@"返回" ];
[self.navigationController popToViewController:self.vc animated:YES];
}
#pragma mark----contentview手势方法
-(void)downSwipe{
// CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
// animation.toValue=[NSNumber numberWithFloat:YBScreenBoundsHeight];
// animation.duration=5; // 动画持续时间
// animation.removedOnCompletion=YES;
// animation.fillMode=kCAFillModeForwards;
// animation.delegate = self;
// [self.contentview.layer addAnimation:animation forKey:nil];
[UIView animateWithDuration:5 delay:3 options:UIViewAnimationOptionRepeat animations:^{
self.mapHeightLayout.constant=YBScreenBoundsHeight-64-10;
} completion:^(BOOL finished) {
self.mapHeightLayout.constant=YBScreenBoundsHeight-64;
NSLog(@"收起动画");
}];
}
-(void)rightSwipe{
[self transitionAnimation:YES];
}
-(void)leftSwipe{
[self transitionAnimation:NO];
}
- (void)transitionAnimation:(BOOL)isNext
{
Hotel *hotel;
//创建转场动画
CATransition *transition = [[CATransition alloc] init];
//设置动画类型
transition.type = @"cube";
//设置子类型
if (isNext) {
if (self.selectIndex<self.annotationArr.count-1) {
self.selectIndex++;
hotel=[self.dataArr objectAtIndex:self.selectIndex];
MAPointAnnotation *anno=(MAPointAnnotation *)[self.annotationArr objectAtIndex:self.selectIndex];
[self.mapView selectAnnotation:anno animated:YES];
}else{
return;
}
transition.subtype = kCATransitionFromRight;
}
else
{
if (self.selectIndex>1) {
self.selectIndex--;
hotel=[self.dataArr objectAtIndex:self.selectIndex];
MAPointAnnotation *anno=(MAPointAnnotation *)[self.annotationArr objectAtIndex:self.selectIndex];
[self.mapView selectAnnotation:anno animated:YES];
}else{
return;
}
transition.subtype = kCATransitionFromLeft;
}
//设置动画时常
transition.duration = .8f;
//设置转场后的新视图
[self setcontent:hotel];
[self.contentview.layer addAnimation:transition forKey:nil];
}
#pragma mark---手势协议用于地图手势的添加,并解决冲突问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[MAPinAnnotationView class]]){
return NO;
}
return YES;
}
#pragma mark----地图点击收起内容
-(void)mapTapDown{
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.mapHeightLayout.constant=YBScreenBoundsHeight-64;
} completion:^(BOOL finished) {
NSLog(@"动画完成2");
}];
}
#pragma mark----请求数据
-(void)Requstdate{
// NSString *url=[NSString stringWithFormat:@"http://apis.juhe.cn/catering/query?lng=%@&lat=%@&radius=%d&page=%d&key=%@&dtype=%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"long"],[[NSUserDefaults standardUserDefaults] valueForKey:@"lat"],500,1,@"33fe1c65a304adf6513f5a075f39f136",@"json"];
//
// [HJHttpManager GetRequestWithUrl:url finish:^(NSData *data) {
//
// NSDictionary *dic=(NSDictionary *)data;
// NSLog(@"请求成功") ;
//
// self.dataArr=[Hotel mj_objectArrayWithKeyValuesArray:dic[@"result"]];
// dispatch_async(dispatch_get_main_queue(), ^{
//
// for (int i=0; i<self.dataArr.count;i++) {
// Hotel *hot=self.dataArr[i];
// if (i==0) {
// MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
//
// CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([hot.latitude doubleValue],[hot.longitude doubleValue]);
// annotation.coordinate = coordinate;
// annotation.title = hot.name;
// [self.annotationArr addObject:annotation];
// [self.mapView addAnnotation:annotation];
//
[self setcontent:hot];
self.mapView.centerCoordinate=coordinate;
// // 选中标注
//
[self.mapView selectAnnotation:annotation animated:YES];
// }else{
// MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
//
// CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([hot.latitude doubleValue],[hot.longitude doubleValue]);
// annotation.coordinate = coordinate;
// annotation.title = hot.name;
// [self.annotationArr addObject:annotation];
// [self.mapView addAnnotation:annotation];
// }
// }
[self.mapView showAnnotations:self.annotationArr edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];
//
// });
//
// } failed:^(NSError *error) {
// NSLog(@"请求失败");
// }];
NSString *url=[[HJInterfaceManager sharedInstance]aroundRestaurant];
NSMutableDictionary *mdic=[[NSMutableDictionary alloc]initWithCapacity:0];
mdic[@"uuid"]=UUID;
mdic[@"lng"]=[[NSUserDefaults standardUserDefaults] valueForKey:@"long"];
mdic[@"lat"]=[[NSUserDefaults standardUserDefaults] valueForKey:@"lat"];
mdic[@"radius"]=@"3000";
[HJHttpManager PostRequestWithUrl:url param:mdic finish:^(NSData *data) {
NSDictionary *dic=(NSDictionary *)data;
NSLog(@"请求成功") ;
self.dataArr=[Hotel mj_objectArrayWithKeyValuesArray:dic[@"result"]];
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=0; i<self.dataArr.count;i++) {
Hotel *hot=self.dataArr[i];
if (i==0) {
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([hot.latitude doubleValue],[hot.longitude doubleValue]);
annotation.coordinate = coordinate;
annotation.title = hot.name;
[self.annotationArr addObject:annotation];
[self.mapView addAnnotation:annotation];
// [self setcontent:hot];
// self.mapView.centerCoordinate=coordinate;
// 选中标注
// [self.mapView selectAnnotation:annotation animated:YES];
}else{
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([hot.latitude doubleValue],[hot.longitude doubleValue]);
annotation.coordinate = coordinate;
annotation.title = hot.name;
[self.annotationArr addObject:annotation];
[self.mapView addAnnotation:annotation];
}
}
// [self.mapView showAnnotations:self.annotationArr edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];
});
} failed:^(NSError *error) {
NSLog(@"请求失败");
}];
}
#pragma mark---地图协议
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation;
{
CLLocationCoordinate2D mycoordinate = CLLocationCoordinate2DMake([[[NSUserDefaults standardUserDefaults] valueForKey:@"lat"] doubleValue],[[[NSUserDefaults standardUserDefaults] valueForKey:@"long"] doubleValue]);
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
MAPinAnnotationView *annotationView = nil;
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
}
if (annotation.coordinate.longitude==mycoordinate.longitude) {
annotationView.pinColor = MAPinAnnotationColorRed;
}else{
annotationView.pinColor = MAPinAnnotationColorGreen;
}
annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop = NO; //设置标注动画显示,默认为NO
annotationView.draggable = YES; //设置标注可以拖动,默认为NO
return annotationView;
}else{
return nil;
}
}
-(void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
for (int i=0; i<self.annotationArr.count; i++) {
if (view.annotation.coordinate.latitude==((MAPointAnnotation *)self.annotationArr[i]).coordinate.latitude&&view.annotation.coordinate.longitude==((MAPointAnnotation *)self.annotationArr[i]).coordinate.longitude) {
Hotel *hot=self.dataArr[i];
NSLog(@"name:%@",hot.name);
[self setcontent:hot];
self.selectHotel=hot;
self.selectIndex=i;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.mapHeightLayout.constant=YBScreenBoundsHeight-64-200;
} completion:^(BOOL finished) {
NSLog(@"动画完成");
}];
}
}
}
#pragma mark---填充弹出层内容
-(void)setcontent:(Hotel *)hot{
[self.hImageview sd_setImageWithURL:[NSURL URLWithString:hot.photos] placeholderImage:nil options:SDWebImageRefreshCached];
[self.namelab setText:hot.name];
[self.taglab setText:hot.tags];
if ([hot.phone isEqualToString:@""]||hot.phone==nil) {
[self.phoneStateBtn setImage:[UIImage imageNamed:@"service_uncall"] forState:UIControlStateNormal];
}else{
[self.phoneStateBtn setImage:[UIImage imageNamed:@"service_call"] forState:UIControlStateNormal];
}
[self.addresslab setText:hot.address];
}
#pragma mark---拨打电话按钮事件
-(IBAction)clickPhoneBtn:(id)sender{
if ([self.selectHotel.phone isEqualToString:@""]||self.selectHotel.phone==nil) {
NSLog(@"没有电话号码,不能拨打");
}else{
NSMutableArray *phones=[self getPhones];
if (phones.count==1) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"是否拨打%@",self.selectHotel.phone] message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *url=[NSString stringWithFormat:@"tel://%@",self.selectHotel.phone];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(@"点击确认");
}]];
[self presentViewController:alertController animated:YES completion:nil];
}else{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择号码拨号" message:nil preferredStyle:UIAlertControllerStyleAlert];
for (NSString *phone in phones) {
[alertController addAction:[UIAlertAction actionWithTitle:phone style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *url=[NSString stringWithFormat:@"tel://%@",phone];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(@"点击确认");
}]];
}
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消");
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
}
}
#pragma mark---手机号判断
-(NSMutableArray *)getPhones{
NSMutableArray *arr=[NSMutableArray arrayWithArray:[self.selectHotel.phone componentsSeparatedByString:@","]];
if (arr.count>0) {
if (arr.count==1) {
return arr;
}else{
if ([arr[0] rangeOfString:@"-"].location!=NSNotFound) {
NSArray *arr2=[arr[0] componentsSeparatedByString:@"-"];
for (int i=1;i<arr.count; i++) {
NSMutableString *mstr=[NSMutableString stringWithString:arr[i]];
[mstr insertString:[NSString stringWithFormat:@"%@-",arr2[0]] atIndex:0];
[arr replaceObjectAtIndex:i withObject:mstr];
}
}
return arr;
}
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end