無標題文章

#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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容