#import"LYJ_BMKDemoController.h"
#import"DFS_NetworkTool.h"
#import
#import
#import"LYJ_SotreInfoModel.h"
#import"LYJ_PointAnnotaion.h"
@interfaceLYJ_BMKDemoController()
@property(nonatomic,strong)BMKMapView* mapView;
@property(nonatomic,strong)BMKLocationService* locService;
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)NSMutableArray* dataSoure;
@end
@implementationLYJ_BMKDemoController
-(void)viewWillAppear:(BOOL)animated{
[_mapViewviewWillAppear];
_mapView.delegate=self;// 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate=self;
}
-(void)viewWillDisappear:(BOOL)animated{
[_mapViewviewWillDisappear];
_mapView.delegate=nil;// 不用时,置nil
_locService.delegate=nil;
}
- (void)viewDidLoad {
[superviewDidLoad];
[self addBMKMapView];
[self addTabelView];
}
-(void)addBMKMapView{
self.navigationItem.title=@"LYJ_BMPDemo";
_mapView= [[BMKMapView alloc] init];
[self.view addSubview:_mapView];
_mapView.frame=CGRectMake(0,64,self.view.bounds.size.width,self.view.bounds.size.height/2);
_locService= [[BMKLocationService alloc]init];
[_locService startUserLocationService];
_mapView.userTrackingMode=BMKUserTrackingModeFollow;//设置定位的状态为普通定位模式
_mapView.showsUserLocation=YES;
self.mapView.zoomLevel=18;
}
-(void)addTabelView{
[self.view addSubview:self.tableView];
CGFloatheight = [UIScreenmainScreen].bounds.size.height-64-_mapView.bounds.size.height;
self.tableView.frame=CGRectMake(0,CGRectGetMaxY(_mapView.frame),self.view.bounds.size.width, height);
}
/**
* 模拟数据
*/
-(void)simulateData:(BMKUserLocation*)userLocation{
CGFloatlng =userLocation.location.coordinate.longitude;
CGFloatlat = userLocation.location.coordinate.latitude;
self.dataSoure= [NSMutableArrayarray];
for(inti =0; i <25; i++) {
LYJ_SotreInfoModel* model = [[LYJ_SotreInfoModel alloc]init];
NSString* lngStr =[NSStringstringWithFormat:@"0.00%d",i +1];
NSString* latStr =[NSStringstringWithFormat:@"0.00%d",i +1];
model.lng= [NSStringstringWithFormat:@"%f",lng + [lngStr doubleValue]];
model.lat= [NSStringstringWithFormat:@"%f",lat + [latStrdoubleValue]];
model.addr= [NSString stringWithFormat:@"第%d门店",i];
model.distance=0.5;
model.deptName= [NSString stringWithFormat:@"门店%d",i];
//小于一公里的门店 添加门店大头针
if(model.distance<1) {
[self.dataSoure addObject:model];
LYJ_PointAnnotaion* annotation = [[LYJ_PointAnnotaion alloc]init];
CLLocationCoordinate2Dcoor;
coor.longitude= [model.lng doubleValue];
coor.latitude= [model.lat doubleValue];
annotation.coordinate= coor;
annotation.title= model.deptName;
[_mapView addAnnotation:annotation];
}
}
[self.tableView reloadData];
}
-(void)requestDatas:(BMKUserLocation*)userLocation{
CGFloatlng =userLocation.location.coordinate.longitude;
CGFloatlat = userLocation.location.coordinate.latitude;
NSString* url = [NSStringstringWithFormat:@"http://api.steward.jjshome.com/ste/shop/getNearShops?lng=%f&lat=%f",lng,lat];
[[DFS_NetworkToolsharedNetworkTool]GET:urlparameters:nilfinished:^(id_NullableresponseObject,NSError*_Nullableerror) {
[selfsimulateData:userLocation];
}];
}
/**
* 添加用户位置大头针
*/
-(void)addMyAnnotation:(BMKUserLocation*)userLocation{
BMKPointAnnotation* myAnnotaion = [[BMKPointAnnotation alloc] init];
myAnnotaion.coordinate= userLocation.location.coordinate;
myAnnotaion.title=@"我的位置";
[_mapView addAnnotation:myAnnotaion];
}
/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation{
[_mapView updateLocationData:userLocation];
[self.locService stopUserLocationService];
[self requestDatas:userLocation];
[self addMyAnnotation:userLocation];
}
/**
*根据anntation生成对应的View
*@param mapView 地图View
*@param annotation 指定的标注
*@return 生成的标注View
*/
- (BMKAnnotationView*)mapView:(BMKMapView*)mapView viewForAnnotation:(id)annotation{
//加载所有的门店位置
if([annotationisKindOfClass:[LYJ_PointAnnotaionclass]]) {
BMKPinAnnotationView* storeAnnotation = (BMKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"storeAnnotation"];
if(!storeAnnotation) {
storeAnnotation = [[BMKPinAnnotationView alloc]initWithAnnotation:annotationreuseIdentifier:@"storeAnnotation"];
}
storeAnnotation.annotation= annotation;
storeAnnotation.pinColor=BMKPinAnnotationColorRed;
storeAnnotation.animatesDrop=YES;
storeAnnotation.image= [UIImageimageNamed:@"house"];
returnstoreAnnotation;
}
//我的大头针位置
BMKPinAnnotationView* myAnnotation = (BMKPinAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"];
if(!myAnnotation) {
myAnnotation = [[BMKPinAnnotationView alloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
}
myAnnotation.annotation= annotation;
myAnnotation.pinColor=BMKPinAnnotationColorRed;
myAnnotation.animatesDrop=YES;
returnmyAnnotation;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.dataSoure.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString* identifier =@"cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];
}
LYJ_SotreInfoModel* model =self.dataSoure[indexPath.row];
cell.textLabel.text= model.deptName;
cell.detailTextLabel.text= model.addr;
returncell;
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
return60;
}
-(UITableView*)tableView{
if(!_tableView) {
_tableView= [[UITableViewalloc]initWithFrame:CGRectZerostyle:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
}
return_tableView;
}
@end
网友评论