#import "KSCVendorMapViewController.h"
#import "UIViewController+KSBackButton.h"
#import "KSVendorInterface.h"
#import "MyAnimatedAnnotationView.h"
#import "KSCMapPopView.h"
#import "UIImageView+WebCache.h"
#import "KSCHelper.h"
#import "KSCVendorViewController.h"
#import "UIViewController+KSStoryboard.h"
#import "Masonry.h"
#import
#import
#import
#import
#import
#import
#import
#import
#import
#import
#import
@interface KSCVendorMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
BMKPointAnnotation* pointAnnotation;
BMKPointAnnotation* animatedAnnotation;
}
@property (nonatomic,retain) NSArray *dataVendor;
@property (nonatomic,strong) BMKLocationService *locService;
@property (nonatomic,strong) BMKMapView * mapViewBMK;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;//经纬度
@property (nonatomic,assign) BMKUserLocation *userLocation;
@property (nonatomic,copy) NSString *vendorID;
@end
@implementation KSCVendorMapViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setNavigationTitle:@"地图详情"];
[self addNavigationPopBackButtonWithHandler:nil];
//适配ios7
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
{
self.navigationController.navigationBar.translucent = NO;
}
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.distanceFilter = 5;//指定定位的最小更新距离(米)
self.mapViewBMK = [[BMKMapView alloc]initWithFrame:self.view.bounds];
self.view = self.mapViewBMK;
_mapViewBMK.mapType = BMKMapTypeStandard;
_mapViewBMK.showMapScaleBar = YES;
_mapViewBMK.showMapPoi = YES;//设定地图是否显示底图
_mapViewBMK.mapScaleBarPosition = CGPointMake(30, self.view.frame.size.height*0.8);
_mapViewBMK.isSelectedAnnotationViewFront = YES;//设定是否总让选中的annotaion置于最前面
//指南针的位置,设定坐标以BMKMapView左上角为原点,向右向下增长
//_mapViewBMK.compassPosition = CGPointMake(80, self.view.frame.size.height*0.8);
UIStepper *stepper = [[UIStepper alloc]init];
NSLog(@"stepper:width%f,height%f",stepper.frame.size.width,stepper.frame.size.height);//94 29
stepper.minimumValue = 3;
stepper.maximumValue = 21;
stepper.stepValue = 1;
stepper.value = 17;
stepper.center = CGPointMake(self.view.frame.size.width - stepper.frame.size.width * 0.5- 10, self.view.frame.size.height-stepper.frame.size.height*0.5-20);
[_mapViewBMK addSubview:stepper];
[stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
UIButton* btn = [[UIButton alloc]init];
[btn setImage:[UIImage imageNamed:@"myLocation"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(moveMapCenterToMyPoint) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(self.view.frame.size.width - stepper.frame.size.width- 40, stepper.frame.origin.y, stepper.frame.size.height, stepper.frame.size.height);
[_mapViewBMK addSubview:btn];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_mapViewBMK.delegate = self;
_locService.delegate = self;
[self startLocation];
[self addAnimatedAnnotation];
[self loadVendorData];//获取零件对应的供应商信息
}
-(void)viewWillDisappear:(BOOL)animated {
_mapViewBMK.delegate = nil;
_locService.delegate = nil;
}
- (void)valueChanged:(UIStepper *)sender
{
UIStepper *st = (UIStepper *)sender;
_mapViewBMK.zoomLevel = st.value;
_mapViewBMK.showsUserLocation = NO;
_mapViewBMK.userTrackingMode = BMKUserTrackingModeFollow;
_mapViewBMK.showsUserLocation = YES;
}
- (void)moveMapCenterToMyPoint
{
_mapViewBMK.zoomLevel = 12;//地图级别
[_mapViewBMK setCenterCoordinate:self.userLocation.location.coordinate];
}
#pragma mark - Vendor //产品对应的供应商地图信息
/*
"4s_name" = "\U5f20\U5bb6\U5c6f4s\U5e97";
address = a;
distance = "1.1";
id = 2;
"intro_pic" = "savefile/4sMember/2016-02-20/7cf18a2a58fc4d1ea822f6e70eafbc14.jpg";
latitude = "39.947459";
longitude = "116.359764";
telephone = a;
*/
-(void)loadVendorData{
CLLocationCoordinate2D loc = self.userLocation.location.coordinate;
loc.latitude = 39.955596;//39.955596,long 116.353138
loc.longitude = 116.353138;
[KSVendorInterface vendorMapInterfaceWithProductID:[self.productid intValue] latitude:loc.latitude longitude:loc.longitude success:^(AFHTTPRequestOperation *operation, NSDictionary *param, id retData) {
//服务器成功
self.dataVendor = retData[@"list"];
NSLog(@"%@",self.dataVendor);
for (NSDictionary* vendorDic in self.dataVendor){
CLLocationCoordinate2D coor;
coor.latitude = [vendorDic[@"latitude"] floatValue];
coor.longitude = [vendorDic[@"longitude"] floatValue];
[self addPointAnnotation:coor];
}
} failuer:^(AFHTTPRequestOperation *operation, NSDictionary *param, NSError *error) {
}];
}
#pragma mark implement BMKMapViewDelegate
//根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
//普通annotation
if (annotation == pointAnnotation) {
NSString *AnnotationViewID = @"vendorMark";
BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
annotationView.pinColor = BMKPinAnnotationColorPurple;
// 从天上掉下效果
annotationView.animatesDrop = YES;
// 设置可拖拽
annotationView.draggable = YES;
int i = 0;
for (NSDictionary* vendorDic in self.dataVendor) {
i++;
if (annotation.coordinate.latitude == [vendorDic[@"latitude"] floatValue]&& annotation.coordinate.longitude == [vendorDic[@"longitude"] floatValue]) {
KSCMapPopView *popView = [[KSCMapPopView alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
[popView.iconImageView sd_setImageWithURL:[NSURL URLWithString:[KSCHelper covertRelativeURLToAbsolutURL:self.dataVendor[0][@"intro_pic"]]]
placeholderImage:DEFAULT_AVATAR_IMAGE];
_vendorID = [NSString stringWithFormat:@"%@",self.dataVendor[0][@"id"]];
//开启用户交互,否则无法识别手势
popView.iconImageView.userInteractionEnabled = YES;
//单击,单手指
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGotoVendor:)];
//单击
singleTap.numberOfTapsRequired = 1;
//单手指
singleTap.numberOfTouchesRequired = 1;
//添加到imageView
[popView.iconImageView addGestureRecognizer:singleTap];
popView.nameLabel.text = [@"名称:" stringByAppendingString: self.dataVendor[0][@"4s_name"]];
popView.phoneLabel.text = [@"电话:" stringByAppendingString: self.dataVendor[0][@"telephone"]];
popView.addressLabel.text = [@"地址:" stringByAppendingString: self.dataVendor[0][@"address"]];
[popView.addressLabel sizeToFit];
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
pView.frame = popView.frame;
pView.backgroundColor = [UIColor clearColor];
((BMKPinAnnotationView*)annotationView).paopaoView = nil;
((BMKPinAnnotationView*)annotationView).paopaoView = pView;
annotationView.image = [UIImage imageNamed:@"dingRed"];
annotationView.tag = i;
} else {
}
}
}
return annotationView;
}
//动画annotation
NSString *AnnotationViewID = @"mineMark";
MyAnimatedAnnotationView *annotationView = nil;
if (annotationView == nil) {
annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i < 4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_%d.png", i]];
[images addObject:image];
}
annotationView.annotationImages = images;
return annotationView;
}
// 当点击annotation view弹出的泡泡时,调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
NSLog(@"paopaoclick");
}
#pragma mark - 用户当前位置地图
-(void)startLocation{
[_locService startUserLocationService];//启动LocationService
_mapViewBMK.zoomLevel = 12;//地图级别 5公里
_mapViewBMK.showsUserLocation = NO;//先关闭显示的定位图层
_mapViewBMK.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
_mapViewBMK.showsUserLocation = YES;//显示定位图层
}
#pragma mark implement BMKLocationServiceDelegate
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
self.userLocation = userLocation;
[_mapViewBMK updateLocationData:userLocation];
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
- (void)dealloc {
if (_mapViewBMK) {
_mapViewBMK = nil;
}
}
- (void)addPointAnnotation:(CLLocationCoordinate2D)loc
{
if (pointAnnotation == nil) {
pointAnnotation = [[BMKPointAnnotation alloc]init];
pointAnnotation.coordinate = loc;
pointAnnotation.title = @"供应商";
// pointAnnotation.subtitle = @"YOYO";
}
[_mapViewBMK addAnnotation:pointAnnotation];
}
//// 添加动画Annotation
- (void)addAnimatedAnnotation{
if (animatedAnnotation == nil) {
animatedAnnotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = self.userLocation.location.coordinate.latitude;
coor.longitude = self.userLocation.location.coordinate.longitude;
animatedAnnotation.coordinate = coor;
animatedAnnotation.title = @"我的动画";
}
[_mapViewBMK addAnnotation:animatedAnnotation];
}
-(void) singleTapGotoVendor:(UIGestureRecognizer *) gesture {
KSCVendorViewController *vendorVC = (KSCVendorViewController *)[UIViewController KS_loadInitialFromStoryboard:@"KSCVendor"];
vendorVC.hidesBottomBarWhenPushed = YES;
vendorVC.memberID = _vendorID;
[self.navigationController pushViewController:vendorVC animated:YES];
}