百度地图开发API

12 篇文章 0 订阅

注意点:
1.由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。此外,地图SDK中还提供了相应的定位图层(支持定位三态效果),帮助开发者显示当前位置信息。(百度地图坐标系统
WGS84坐标系:即地球坐标系,国际上通用的坐标系。
GCJ02坐标系:即火星坐标系,WGS84坐标系经加密后的坐标系。
BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系。(该坐标系)
搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。 )
坐标
http://www.cnblogs.com/jay-dong/archive/2013/03/13/2957648.html
http://blog.csdn.net/m372897500/article/details/49799153

注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述

定位模式:

目前为止,BMKMapView的定位模式(userTrackingMode)有4种分别是:
1) BMKUserTrackingModeNone :普通定位模式,显示我的位置,我的位置图标和地图都不会旋转
2) BMKUserTrackingModeFollow : 定位跟随模式,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转
3) BMKUserTrackingModeFollowWithHeading : 定位罗盘模式,我的位置始终在地图中心,我的位置图标和地图都会跟着旋转
4) BMKUserTrackingModeHeading:普通定位+定位罗盘模式,显示我的位置,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转。即在普通定位模式的基础上显示方向。

 //设置自定义地图样式,会影响所有地图实例

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight * 0.2)];

    [self.view addSubview:_mapView];

    //注:必须在BMKMapView对象初始化之前调用
    //    NSString* path = [[NSBundle mainBundle] pathForResource:@"custom_config_清新蓝" ofType:@""];
    //    [BMKMapView customMapStyle:path];
    //    _mapView.mapType = BMKMapTypeNone;
    [_mapView setMapType:BMKMapTypeStandard];
//    _mapView.userTrackingMode = BMKUserTrackingModeNone;

    _locService = [[BMKLocationService alloc]init];

    displayParam = [[BMKLocationViewDisplayParam alloc]init];
    displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
    displayParam.isAccuracyCircleShow = false;//精度圈是否显示
    displayParam.locationViewImgName= @"currentPoint@2x.png";//定位图标名称
    displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
    displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
    [_mapView updateLocationViewWithParam:displayParam];

    _geocodesearch = [[BMKGeoCodeSearch alloc]init];

    [_mapView setZoomLevel:14];

    [_locService startUserLocationService];
    //    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层


    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    _locService.delegate = self;
    _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

代理方法

#pragma mark - BMKMapViewDelegate

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
//       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"BMKMapView控件初始化完成" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
//            [alert show];
//            alert = nil;
}

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: click blank");
}

- (void)mapview:(BMKMapView *)mapView onDoubleClick:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: double click");
}

/**
 *在地图View将要启动定位时,会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户方向更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    getLocation = userLocation;
    [self updateLocalX:userLocation.location.coordinate];
    if (userLocation) {
        [_locService stopUserLocationService];
    }



}

/**
 *用户位置更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{


    getLocation = userLocation;
    _mapView.zoomLevel = 5;
    NSSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [_mapView updateLocationData:userLocation];

    [self updateLocalX:userLocation.location.coordinate];




    BMKCoordinateRegion region ;//表示范围的结构体
    region.center = userLocation.location.coordinate;//中心点
    region.span.latitudeDelta = 0.005;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
    region.span.longitudeDelta = 0.005;//纬度范围
    [_mapView setRegion:region animated:YES];
    _mapView.centerCoordinate =  userLocation.location.coordinate;

    if (userLocation) {
        [_locService stopUserLocationService];
    }

}

/**
 *在地图View停止定位后,会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSSLog(@"stop locate");
}

/**
 *定位失败后,会调用此函数
 *@param mapView 地图View
 *@param error 错误号,参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSSLog(@"location error");
}
//反地理编码
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{

    NSLog(@"%d",error);
    geoCodeResult = result;
//    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
//    [_mapView removeAnnotations:array];
//    array = [NSArray arrayWithArray:_mapView.overlays];
//    [_mapView removeOverlays:array];
//    if (error == 0) {
//        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
//        item.coordinate = result.location;
//        item.title = result.address;
//        [_mapView addAnnotation:item];
//        _mapView.centerCoordinate = result.location;
//        _mapView.zoomLevel = 3;
//        NSString* titleStr;
//        NSString* showmeg;
//        titleStr = @"反向地理编码";
//        showmeg = [NSString stringWithFormat:@"%@",item.title];
//
//        NSLog(@"%@",showmeg);
//        //        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
//        //        [myAlertView show];
//    }

}

#pragma mark -配置搜索参数
- (void) setupSearchCode
{

    if (getLocation.location) {
        CLLocationCoordinate2D pt = getLocation.location.coordinate;
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeocodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
        if(flag)
        {
            NSLog(@"反geo检索发送成功");
        }
        else
        {
            NSLog(@"反geo检索发送失败");
        }
    }


}
//当界面将要销毁时,取消地图.定位.地理编码搜索的代理,置空对象.
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];

    _mapView.delegate = nil; // 不用时,置nil
    _locService.delegate = nil;
    _geocodesearch.delegate = nil; // 不用时,置nil
}

- (void)dealloc {
    if (_mapView) {
        _mapView = nil;
    }

    if (_locService) {
        _locService = nil;
    }

    if (_geocodesearch != nil) {
        _geocodesearch = nil;
    }
}

百度地图集成后不显示原因:1.申请的加密安全码是否正确
2.集成环境中添加Library,是否添加完全(先将要用到的百度库复制到项目文件夹下,然后通过TARGETS->Build Phases->Link Binary With Libraries -> + -> Add Other 将用到的百度地图库添加到项目中)
3.Framework Search Paths 中添加百度地图库所对应的项目中的相对路径文件夹 $(PROJECT_DIR)/EHR/ThirdParty/BaiduMapKit
4. 将BaiduMapAPI_Map.framework下的资源文件Resources添加到项目中,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值