iOS-百度地图之LBS.云检索学习


首先,要把百度地图sdk集成到项目中,集成教程可以参考百度地图api,里面有详细的说明。在集成sdk后,需要实现百度地图的基本地图。


一. 百度地图-基本地图实现:

1. 创建BMKMapView对象。

BMKMapView *_mapView;


2.对象的实现,以及设置相应的属性

    _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_mapView];

_mapView.userTrackingMode = BMKUserTrackingModeNone; //定位跟随模式
_mapView.mapType = BMKMapTypeStandard; //标准地图
_mapView.zoomLevel = 18; // 设置显示级别
_mapView.showsUserLocation = YES; //设定是否显示定位图层

这样基本地图就实现了。


二. 百度地图-定位

1. 创建BMKLocationService对象。

BMKLocationService *_locService;

2.对象的实现,以及设置相应的属性

_locService = [[BMKLocationService alloc] init];

    _locService.delegate = self;
// 启动locationService
[_locService startUserLocationService];

3. 实现代理方法

#pragma mark ---<BMKLocationServiceDelegate>

// 处理坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
// CLLocationCoordinate2D center = userLocation.location.coordinate;
// _mapView.centerCoordinate = center;
// [_mapView updateLocationData:userLocation];
if (!_isSetMapSpan)
{
_locationPoint = userLocation.location;
[_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
_isSetMapSpan = YES;
[_mapView updateLocationData:userLocation];
}
}

如此,定位功能就实现了。

三. 简单实现LBS.云检索功能

在这,就简单的实现下,本地云搜索,附近云搜索以及详细云搜索, 在次需要在百度地图云检索后台配置信息,如下图

百度云搜索后台

1.云搜索对象的实现

BMKCloudSearch *_search;

2.对象的实现以及相关属性的设置

    // 初始化云检索服务
_search = [[BMKCloudSearch alloc] init];
_search.delegate = self;
// 设置地图级别
[_mapView setZoomLevel:13];
_mapView.isSelectedAnnotationViewFront = YES; //设定是否总让选中的annotaion置于最前面

3.功能的实现,选择某个搜索

a.本地云搜索:

    // 本地搜索

BMKCloudLocalSearchInfo *cloudSearch = [[BMKCloudLocalSearchInfo alloc] init];

cloudSearch.ak = @"IdoZaK6nNotZlVKVHB3GY4ZKLmtqHF2L";
cloudSearch.geoTableId = 137542;
cloudSearch.pageIndex = 0;
cloudSearch.pageSize = 10;

cloudSearch.region = @"厦门市";
cloudSearch.keyword = @"KFC";

BOOL flag = [_search localSearchWithSearchInfo:cloudSearch];

b.周边云搜索:

   // 周边搜索
BMKCloudNearbySearchInfo *cloudLocalSearch = [[BMKCloudNearbySearchInfo alloc]init];
cloudLocalSearch.ak = @"IdoZaK6nNotZlVKVHB3GY4ZKLmtqHF2L";
cloudLocalSearch.geoTableId = 137542;
cloudLocalSearch.keyword = @"KFC";
cloudLocalSearch.radius = 10000;
cloudLocalSearch.location = [NSString stringWithFormat:@"%f,%f",_locationPoint.coordinate.longitude, _locationPoint.coordinate.latitude];
BOOL flag = [_search nearbySearchWithSearchInfo:cloudLocalSearch];

c.详情云搜索

    // 详情搜索
BMKCloudDetailSearchInfo *cloudDetailearch = [[BMKCloudDetailSearchInfo alloc] init];
cloudDetailearch.ak = @"IdoZaK6nNotZlVKVHB3GY4ZKLmtqHF2L";
cloudDetailearch.geoTableId = 137542;
cloudDetailearch.uid = @"1661834813";
BOOL flag = [_search detailSearchWithSearchInfo:cloudDetailearch];

4.代理相关处理操作

// 本地、周边、区域搜索回调
- (void)onGetCloudPoiResult:(NSArray *)poiResultList searchType:(int)type errorCode:(int)error
{
NSArray *array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];

if (error == BMKErrorOk)
{
BMKCloudPOIList *result = [poiResultList objectAtIndex:0];
for (int i = 0; i < result.POIs.count; i++)
{
BMKCloudPOIInfo *poi = [result.POIs objectAtIndex:i];
BMKPointAnnotation *item = [[BMKPointAnnotation alloc] init];
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){poi.longitude, poi.latitude};
item.coordinate = pt;
item.title = poi.title;
[_mapView addAnnotation:item];
}
}
else
{
NSLog(@"error == %d", error);
}
}

// 详情搜索回调
- (void)onGetCloudPoiDetailResult:(BMKCloudPOIInfo*)poiDetailResult searchType:(int)type errorCode:(int)error
{
// 清楚屏幕中所有的annotation
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];

if (error == BMKErrorOk) {
BMKCloudPOIInfo* poi = poiDetailResult;
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){ poi.longitude,poi.latitude};
item.coordinate = pt;
item.title = poi.title;
[_mapView addAnnotation:item];
//将第一个点的坐标移到屏幕中央
_mapView.centerCoordinate = pt;
} else {
NSLog(@"error ==%d",error);
}
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告