IOS 调用百度地图(SDK) 进行定位以及自定义位置弹出框(气泡)

首先申请百度账号,在百度地图API 在里面添加相应的应用,并且选择开发平台以及对应的开发应用ID来获取AK,

导入SDK可以按照官方文档上的流程,导入相应的第三方库,以及相应的底层库,添加相应的资源即可。

提示本应用是否获取当前位置定位服务提示框,要在Plist文件中添加NSLocationAlwaysUsageDescription属性,后面设置的内容会在提示框中显示,可以不设置。        设置->隐私->地图定位->找到相应的APP

在控制器中创建一张百度地图:并且遵循代理方法

#import"BaseViewController.h"

#import<BaiduMapAPI_Map/BMKMapComponent.h>

#import<BaiduMapAPI_Location/BMKLocationComponent.h>

@interfaceTheMapSiteViewController :BaseViewController<BMKMapViewDelegate,BMKLocationServiceDelegate,UIActionSheetDelegate>

@property(nonatomic,strong)BMKMapView*mapView;

@property(nonatomic,strong)BMKLocationService*locService;

@end


在.m文件中代码如下:

创建一张地图并获取当前位置

//创建一张地图

   _mapView= [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,SCREENWIDTH,SCREENHEIGHT)];

   _mapView.delegate=self;

    [self.viewaddSubview:_mapView];

    

    [_locServicestartUserLocationService];

   _mapView.showsUserLocation=NO;//先关闭显示的定位图层

   _mapView.userTrackingMode=BMKUserTrackingModeNone;//设置定位的状态

   _mapView.showsUserLocation=YES;//显示定位图层

    

   //创建气球上面的位置显示框

   BMKPointAnnotation* annotation = [[BMKPointAnnotationalloc]init];

   CLLocationCoordinate2Dcoor;

    coor.latitude= [_endLatitudeStrfloatValue];

    coor.longitude= [_endLongitudeStrfloatValue];

    annotation.coordinate= coor;


    annotation.title=@"1";

    annotation.subtitle=@"this is a test!this is a test!";

    [_mapViewaddAnnotation:annotation];

   //关键代码如下:

    //这样就可以在初始化的时候将 气泡信息弹出

    [_mapViewselectAnnotation:annotationanimated:YES];

   BMKCoordinateRegiontheRegin;

    theRegin.center= coor;

    

   BMKCoordinateSpantheSpan;

   //设置地图显示的缩放比例

    theSpan.latitudeDelta  =0.1;

    theSpan.longitudeDelta=0.1;

    theRegin.span= theSpan;

    

    [_mapViewsetRegion:theRegin];

    [_mapViewregionThatFits:theRegin];


/**

 *用户位置更新后,会调用此函数(该方法能时刻获取用户当前位置)

 *@param userLocation新的用户位置

 */


代理方法之一

- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation

{

   //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    

   self.latitudeStr= [NSStringstringWithFormat:@"%lf", userLocation.location.coordinate.latitude];

   self.longitudeStr= [NSStringstringWithFormat:@"%lf", userLocation.location.coordinate.longitude];

    [_mapViewupdateLocationData:userLocation];

}


//这个代理方法能够修改定位大头针的样式以及自定义气泡弹出框的样式,可根据自己的需要进行自定义

- (BMKAnnotationView*)mapView:(BMKMapView*)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{

   

   BMKAnnotationView*annotationView = [[BMKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];

//    annotationView.animatesDrop = YES;//设置该标注点动画显示

    annotationView.image= [UIImageimageNamed:@"map_lbs"];  //把大头针换成别的图片

    

    //自定义内容气泡

   UIView*areaPaoView=[[UIViewalloc]initWithFrame:CGRectMake(0,0, widthX,74)];

    //这张图片是做好的半透明的

    areaPaoView.layer.contents=(id)[UIImageimageNamed:@"map_1"].CGImage;

    

   if([annotation.titleisEqualToString:@"1"]) {//假设title的标题为1,那么就把添加上这个自定义气泡内容

        //注意:这里面控件的代码不完全,改成你们想要的View)

       UIButton*navigationBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [navigationBtnsetImage:[UIImageimageNamed:@"map_2"]forState:UIControlStateNormal];

        [navigationBtnaddTarget:selfaction:@selector(navigation)forControlEvents:UIControlEventTouchUpInside];

        [areaPaoViewaddSubview:navigationBtn];

        [navigationBtnmas_makeConstraints:^(MASConstraintMaker*make) {

            make.right.equalTo(-5*SCALE);

            make.top.equalTo(15*SCALE);

            make.width.equalTo(64);

            make.height.equalTo(39);

        }];

        

       UILabel* labelNo = [[UILabelalloc]init];

        

        labelNo.textColor= [UIColorwhiteColor];

        labelNo.backgroundColor= [UIColorclearColor];

        labelNo.font= [UIFontsystemFontOfSize:17];

        labelNo.text=_siteNameString;

        [areaPaoViewaddSubview:labelNo];

        [labelNomas_makeConstraints:^(MASConstraintMaker*make) {

            make.top.equalTo(8*SCALE);

            make.left.equalTo(10*SCALE);

            make.right.equalTo(navigationBtn.left).offset(-5*SCALE);

        }];

        [labelNolayoutIfNeeded];

        


       UILabel* labelStationName = [[UILabelalloc]init];

        labelStationName.textColor= [UIColorwhiteColor];

        labelStationName.backgroundColor= [UIColorclearColor];

        labelStationName.font= [UIFontsystemFontOfSize:14];

        labelStationName.textAlignment=NSTextAlignmentLeft;

        [areaPaoViewaddSubview:labelStationName];

    

    //布局完之后将View整体添加到BMKActionPaopaoView上

   BMKActionPaopaoView*paopao=[[BMKActionPaopaoViewalloc]initWithCustomView:areaPaoView];

    

    annotationView.paopaoView= paopao;

    

   returnannotationView;

}


下面是我自己做出来的效果:



在我做的这个界面中,点击弹出框中的导航按钮会调用第三发地图APP的客户端进行路径规划及导航,需要的朋友可以进下方链接了解调用第三发地图APP功能的实现。

http://blog.csdn.net/qq_30608949/article/details/52230433 (本网址为原创内容)



版权声明:本文为qq_30608949原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。