在这里就不在介绍百度的具体配置,配置详见http://developer.baidu.com/map/index.php?title=iossdk
1.首先接受基本的地图功能
新建一个地图类,xib拖也行,我这边是代码实现的。
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)]; [self.view addSubview:_mapView]; #pragma mark - 设置mapView属性 -(void)setMapViewProperty { _mapView.mapType = BMKUserTrackingModeFollowWithHeading; _mapView.showsUserLocation = YES; _mapView.zoomLevel = 16; _mapView.rotateEnabled = NO; [self passLocationValue]; } #pragma mark -传入定位坐标 (必须打开程序时已经获取到地理位置坐标,为了解决地图定位时总是先显示*) -(void)passLocationValue { BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake([UserLocationManager sharedInstance].clloction.coordinate, BMKCoordinateSpanMake(0.02f,0.02f)); BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion]; [_mapView setRegion:adjustedRegion animated:YES]; } #pragma mark -设置定位圆点属性 -(void)setUserImage { BMKLocationViewDisplayParam* param = [[BMKLocationViewDisplayParam alloc] init]; param.locationViewOffsetY = 0; param.locationViewOffsetX = 0; param.isAccuracyCircleShow =NO; param.isRotateAngleValid = NO; [_mapView updateLocationViewWithParam:param]; }
实现BMKMapViewDelegate,以下是mapView的一些协议方法
** *地图区域即将改变时会调用此接口 *@param mapview 地图View *@param animated 是否动画 */ - (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { //TODO } /** *地图区域改变完成后会调用此接口 *@param mapview 地图View *@param animated 是否动画 */ - (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { //TODO } /** *地图状态改变完成后会调用此接口 *@param mapview 地图View */ - (void)mapStatusDidChanged:(BMKMapView *)mapView { //TODO }
这样基本的地图界面就出来了
2.地图定位
我这边是将定位封装了一个独立的manager类来管理定位和地图上滑动到的位置
#import "UserLocationManager.h" #import "BMKGeocodeSearch.h" @implementation UserLocationManager + (UserLocationManager *)sharedInstance { static UserLocationManager *_instance = nil; @synchronized (self) { if (_instance == nil) { _instance = [[self alloc] init]; } } return _instance; } -(id)init { if (self == [super init]) { //[self getBMKUserLocation]; //[self initGeoCodeSearch]; } return self; } #pragma 初始化百度地图用户位置管理类 /** * 初始化百度地图用户位置管理类 */ - (void)initBMKUserLocation; { _locService = [[BMKLocationService alloc]init]; _locService.delegate = self; [self startLocation]; } #pragma 打开定位服务 /** * 打开定位服务 */ -(void)startLocation { [_locService startUserLocationService]; } #pragma 关闭定位服务 /** * 关闭定位服务 */ -(void)stopLocation { [_locService stopUserLocationService]; } #pragma BMKLocationServiceDelegate /** *用户位置更新后,会调用此函数 *@param userLocation 新的用户位置 */ - (void)didUpdateUserLocation:(BMKUserLocation *)userLocation { cllocation = userLocation.location; _clloction = cllocation; _userLatitude = cllocation.coordinate.latitude; _userLongitude = cllocation.coordinate.longitude; [self stopLocation]; [self initGeoCodeSearch]; } /** *在停止定位后,会调用此函数 */ - (void)didStopLocatingUser { [_mapViewVC setMapViewProperty]; } /** *定位失败后,会调用此函数 *@param error 错误号 */ - (void)didFailToLocateUserWithError:(NSError *)error { [self stopLocation]; } #pragma BMKGeoCodeSearchDelegate -(void)initGeoCodeSearch { _geoCodeSearch = [[BMKGeoCodeSearch alloc]init]; _geoCodeSearch.delegate = self; BMKReverseGeoCodeOption *reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init]; reverseGeoCodeOption.reverseGeoPoint = cllocation.coordinate; [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption]; } /** *返回反地理编码搜索结果 *@param searcher 搜索对象 *@param result 搜索结果 *@param error 错误号,@see BMKSearchErrorCode */ -(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { if (error == 0) { _cityName = result.addressDetail.city; NSLog(@"%@",_cityName); } } @end
后续继续更新