//
// FujinViewController.m
// BestBeant
//
// Copyright © 2015年 cgb. All rights reserved.
//
#import "FujinViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface FujinViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate,BMKLocationServiceDelegate>{
int curPage;
BMKLocationService * _locService;
BMKPoiSearch* _poisearch;
BMKNearbySearchOption * _nearbyOption;
}
@property (strong,nonatomic) IBOutletBMKMapView *mapView;
@property (strong,nonatomic) IBOutletUIButton *leftButton;
@property (strong,nonatomic) IBOutletUIButton *rightButton;
@property (nonatomic)CLLocationCoordinate2D currentLocation;
@end
@implementation FujinViewController
- (IBAction)leftClick:(id)sender {
[selfstartLocation];
}
- (IBAction)rightClick:(id)sender {
[selfonClickOk];
}
- (void)viewDidLoad {
[superviewDidLoad];
[selfsetUI];
//地图
//定位
_locService = [[BMKLocationServicealloc]init];
//启动LocationService
[_locServicestartUserLocationService];
[selfstartLocation];
//搜索
_poisearch = [[BMKPoiSearchalloc]init];
[self.mapViewsetZoomLevel:13];
self.mapView.isSelectedAnnotationViewFront =YES;
}
- (void)setUI{
self.view.backgroundColor = [UIColorwhiteColor];
}
//周边与检索
-(void)onClickOk
{
curPage =0;
_nearbyOption = [[BMKNearbySearchOptionalloc]init];
_nearbyOption.pageIndex =0;
_nearbyOption.pageCapacity =10;
_nearbyOption.location = (CLLocationCoordinate2D){23.181429,113.347267};
//_nearbyOption.location = self.currentLocation;
_nearbyOption.keyword =@"餐馆";
_nearbyOption.radius = (CLLocationDistance){1000};
[_poisearchpoiSearchNearBy:_nearbyOption];
}
#pragma mark -
#pragma mark implement BMKMapViewDelegate
/**
*根据anntation生成对应的View
*@param mapView地图View
*@param annotation指定的标注
*@return生成的标注View
*/
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
// 生成重用标示identifier
NSString *AnnotationViewID =@"xidanMark";
// 检查是否有重用的缓存
BMKAnnotationView* annotationView = [viewdequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
// 缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
if (annotationView ==nil) {
annotationView = [[BMKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:AnnotationViewID];
((BMKPinAnnotationView*)annotationView).pinColor =BMKPinAnnotationColorRed;
// 设置重天上掉下的效果(annotation)
((BMKPinAnnotationView*)annotationView).animatesDrop =YES;
}
// 设置位置
annotationView.centerOffset =CGPointMake(0, -(annotationView.frame.size.height * 0.5));
annotationView.annotation = annotation;
// 单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
annotationView.canShowCallout =YES;
// 设置是否可以拖拽
annotationView.draggable =NO;
return annotationView;
}
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
[mapView bringSubviewToFront:view];
[mapView setNeedsDisplay];
}
- (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
}
#pragma mark -
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
// 清楚屏幕中所有的annotation
NSArray* array = [NSArrayarrayWithArray:self.mapView.annotations];
[self.mapViewremoveAnnotations:array];
if (error ==BMK_SEARCH_NO_ERROR) {
NSMutableArray *annotations = [NSMutableArrayarray];
for (int i =0; i < result.poiInfoList.count; i++) {
BMKPoiInfo* poi = [result.poiInfoListobjectAtIndex:i];
BMKPointAnnotation* item = [[BMKPointAnnotationalloc]init];
item.coordinate = poi.pt;
item.title = poi.name;
[annotations addObject:item];
}
[self.mapViewaddAnnotations:annotations];
[self.mapViewshowAnnotations:annotations animated:YES];
} elseif (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
NSLog(@"起始点有歧义");
} else {
// 各种情况的判断。。。
}
}
//基础地图
-(void)viewWillAppear:(BOOL)animated
{
[self.mapViewviewWillAppear];
_locService.delegate =self;
_poisearch.delegate =self;
self.mapView.delegate =self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.mapViewviewWillDisappear];
self.mapView.delegate =nil; // 不用时,置nil
_locService.delegate =nil;
_poisearch.delegate =nil;
}
//定位
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
//NSLog(@"heading is %@",userLocation.heading);
[self.mapViewupdateLocationData:userLocation];
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[self.mapViewupdateLocationData:userLocation];
// self.currentLocation = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude};
}
-(void)startLocation
{
NSLog(@"进入普通定位态");
self.mapView.showsUserLocation =NO;
self.mapView.userTrackingMode =BMKUserTrackingModeFollow;
self.mapView.showsUserLocation =YES;
}
/*
#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.
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
百度附近功能 定位
最新推荐文章于 2021-12-04 20:42:47 发布