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

本文详细介绍如何在iOS应用中集成百度地图SDK,包括创建地图、显示用户位置、自定义标注点图标及气泡样式等步骤。通过示例代码演示如何实现地图定位服务并展示当前位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

#import "BaseViewController.h"

#import <BaiduMapAPI_Map/BMKMapComponent.h>

#import <BaiduMapAPI_Location/BMKLocationComponent.h>

@interface TheMapSiteViewController : BaseViewController <BMKMapViewDelegate,BMKLocationServiceDelegate,UIActionSheetDelegate>

@property (nonatomic, strong) BMKMapView *mapView;

@property (nonatomic, strong) BMKLocationService *locService;

@end


在.m文件中代码如下:

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

//创建一张地图

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

    _mapView.delegate = self;

    [self.view addSubview:_mapView];

    

    [_locService startUserLocationService];

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

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

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

    

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

    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];

    CLLocationCoordinate2D coor;

    coor.latitude = [_endLatitudeStr floatValue];

    coor.longitude = [_endLongitudeStr floatValue];

    annotation.coordinate = coor;


    annotation.title = @"1";

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

    [_mapView addAnnotation:annotation];

    //关键代码 如下:

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

    [_mapView selectAnnotation:annotation animated:YES];

    BMKCoordinateRegion theRegin;

    theRegin.center = coor;

    

    BMKCoordinateSpan theSpan;

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

    theSpan.latitudeDelta  = 0.1;

    theSpan.longitudeDelta = 0.1;

    theRegin.span = theSpan;

    

    [_mapView setRegion:theRegin];

    [_mapView regionThatFits:theRegin];


/**

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

 *@param userLocation 新的用户位置

 */


代理方法之一

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

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

    

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

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

    [_mapView updateLocationData:userLocation];

}


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

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

   

    BMKAnnotationView *annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

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

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

    

    //自定义内容气泡

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

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

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

    

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

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

        UIButton *navigationBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [navigationBtn setImage:[UIImage imageNamed:@"map_2"] forState:UIControlStateNormal];

        [navigationBtn addTarget:self action:@selector(navigation) forControlEvents:UIControlEventTouchUpInside];

        [areaPaoView addSubview:navigationBtn];

        [navigationBtn mas_makeConstraints:^(MASConstraintMaker *make) {

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

            make.top.equalTo(15 * SCALE);

            make.width.equalTo(64);

            make.height.equalTo(39);

        }];

        

        UILabel * labelNo = [[UILabel alloc]init];

        

        labelNo.textColor = [UIColor whiteColor];

        labelNo.backgroundColor = [UIColor clearColor];

        labelNo.font = [UIFont systemFontOfSize:17];

        labelNo.text = _siteNameString;

        [areaPaoView addSubview:labelNo];

        [labelNo mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(8 * SCALE);

            make.left.equalTo(10 * SCALE);

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

        }];

        [labelNo layoutIfNeeded];

        


        UILabel * labelStationName = [[UILabel alloc]init];

        labelStationName.textColor = [UIColor whiteColor];

        labelStationName.backgroundColor = [UIColor clearColor];

        labelStationName.font = [UIFont systemFontOfSize:14];

        labelStationName.textAlignment = NSTextAlignmentLeft;

        [areaPaoView addSubview:labelStationName];

    

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

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

    

    annotationView.paopaoView = paopao;

    

    return annotationView;

}


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



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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值