IOS开发征服iOS租房

百度地图(集成大头针和导航功能)

2016-06-05  本文已影响1911人  小茗

1.在百度地图开发者平台申请AppKey.

2.配置开发环境

touch Podfile

2.编辑Podfile内容如下:

pod 'BaiduMapKit' #百度地图SDK

3.在Podfile所在的文件夹下输入命令:

pod install (这个可能比较慢,请耐心等待……)

成功以后,会出现如下记录:

Analyzing dependencies   Downloading dependencies   Installing BaiduMapKit (2.9.1)   Generating Pods project   Integrating client project   [!] Please close any current Xcode sessions and **use** `***.xcworkspace` **for** **this** project from now on. Sending stats

恭喜你已成功导入百度地图IOS SDK,现在可以打开xcodeSpace文件,在你的项目中使用百度地图SDK了.

Snip20160525_19.png Snip20160525_20.png Snip20160526_4.png Snip20160526_6.png

3.导入所需要的系统库, 因此您需要在您的Xcode工程中引入CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework、libsqlite3.0.tbd(xcode7以前为 libsqlite3.0.dylib)、CoreTelephony.framework 、libstdc++.6.0.9.tbd(xcode7以前为libstdc++.6.0.9.dylib)。

Snip20160525_22.png

4.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。

Snip20160525_24.png

5.最后,我们Command + B 一下,如果你能编译成功了,那么就OK了.其实没多少复杂的,基础地图是最简单的.(最后在自己使用的类里面如果使用百度地图的类就导入一下就可以了).
6.IOS9之后改用Https形式,所以我们也要在Info.plist文件里配置一下,否则会影响百度地图的使用(空白一片),还有Bundle Identifier要与申请APPKey的Bundle Identifier 一致.而且我们也在info添加它的displayName与申请时应用名称也要一致.

3.进入开发,初始化BMKMapManager,(我们要拿到上面申请得到的APPKey)

Snip20160525_29.png

代码如下:
在AppDelegate文件中,导入#import <BaiduMapAPI_Base/BMKMapManager.h>,且强引用一个mapManager

#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BMKMapManager *mapManager;
@end

在AppDelegate.m文件中,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加APPKey.

_mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG" generalDelegate:nil];
    if (!ret) {
        NSLog(@"引擎启动失败");
    }
    [BNCoreServices_Instance initServices:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG"];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];

在ViewDidLoad方法里面初始化MapView.上面忘记说一件事,由于百度地图引用的是静态库,所以我们需要把其中一个.m文件改成.mm文件.这样才可以正常使用.

- (void)viewDidLoad {
    [super viewDidLoad];
    BMKMapView *mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:mapView];
    self.mapView = mapView;
}

这样基础地图信息就可以显示出来.当然坐标是在北京的,后面会教你怎么修改地图显示的坐标和大头针视图.

Snip20160525_26.png

4.设置大头针视图和设置地图显示相对应的位置.

我们知道地图的定位是根据经纬度来进行判断的.所以这里输入我们想显示地址的经纬度,同样我们要创建大头针视图,百度提供了2个大头针类,一种是百度大头针标准类,另一种是自定义类.这些都会用到BMKPointAnnotation类方法,而我们自定义大头针可以遵守BMKMapViewDelegate方法,重写它的协议方法.

- (void) viewDidAppear:(BOOL)animated {
    // 添加一个PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 22.50;
    coor.longitude = 113.41;
    annotation.coordinate = coor;
    [_mapView addAnnotation:annotation];
    //设置地图的缩放比例
    [_mapView setZoomLevel:16];
    //地图显示的位置
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(22.50,113.41)];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    BMKPinAnnotationView *newAnationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"anonationID"];
    //直接显示,不用点击弹出
    [newAnationView setSelected:YES];
    ((BMKPinAnnotationView *)newAnationView).image = [UIImage imageNamed:@"poi_3"];
    ((BMKPinAnnotationView *)newAnationView).animatesDrop = YES;
    UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 185, 56)];
    //设置弹出气泡背景图片
    UIImageView *bgImageV =[[UIImageView alloc]init];
    bgImageV.image = [[UIImage imageNamed:@"wl_map_icon_5"]stretchableImageWithLeftCapWidth:28 topCapHeight:16];
    bgImageV.frame = CGRectMake(0, 0, 185, 52);
    [popView addSubview:bgImageV];
    UIImageView *connerImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wl_map_icon_4"]];
    connerImageV.frame = CGRectMake(88, 52, 12, 4);
    [popView addSubview:connerImageV];
    UIImageView *navImageV = [[UIImageView alloc]initWithFrame:CGRectMake(120, 0, 65, 52)];
    navImageV.image = [UIImage imageNamed:@"wl_map_icon_1"];
    navImageV.userInteractionEnabled = YES;
    [bgImageV addSubview:navImageV];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"广东省中山市";
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 120, 30);
    [bgImageV addSubview:titleLabel];
    UILabel *subLabel = [[UILabel alloc]init];
    subLabel.text = @"博爱五路今科科技公司";
    subLabel.textAlignment = NSTextAlignmentLeft;
    subLabel.textColor = [UIColor whiteColor];
    subLabel.font = [UIFont systemFontOfSize:12];
    subLabel.frame = CGRectMake(0, 30, 120, 22);
    [bgImageV addSubview:subLabel];
    BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
    pView.frame = CGRectMake(0, 0, 185, 56);
    ((BMKPinAnnotationView*)newAnationView).paopaoView = nil;
    ((BMKPinAnnotationView*)newAnationView).paopaoView = pView;
    return newAnationView;
}

运行程序,会显示如下图:

Snip20160525_27.png

5.下面的是百度地图的导航功能开发

Snip20160525_30.png Snip20160525_31.png
ld: 'XXX' does not contain bitcode. You must rebuild it with bitcode enabled  (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Snip20160525_32.png Snip20160526_2.png
#import "BNCoreServices.h"
[BNCoreServices_Instance initServices:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG"];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];
self.service = [[BMKLocationService alloc]init];
    self.service.delegate = self;
    [self.service startUserLocationService];
//开启定位功能,获得坐标方法
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //获得坐标经度
    self.mylongitude = userLocation.location.coordinate.longitude;
    //获得坐标纬度
    self.mylatitude = userLocation.location.coordinate.latitude;
}
//点击泡泡的代理方法(实现导航功能)
-(void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
{
    //节点数组
    NSMutableArray *nodesArray = [[NSMutableArray alloc]    initWithCapacity:2];
    
    //起点
    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
    startNode.pos = [[BNPosition alloc] init];
    startNode.pos.x = self.mylongitude;
startNode.pos.y = self.mylatitude;       
startNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:startNode];
    
    //终点
    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
    endNode.pos = [[BNPosition alloc] init];
    endNode.pos.x = 113.41;
    endNode.pos.y = 22.50;
    endNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:endNode];
    //发起路径规划
    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
}
-(void)routePlanDidFinished:(NSDictionary *)userInfo
{
    NSLog(@"算路成功");
    
    //路径规划成功,开始导航
    [BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:self isNeedLandscape:YES];
}

由于代码今天毁了/(ㄒoㄒ)/~~,所以以后会更新上传代码地址.或者有需要的朋友可以联系我(QQ:1090981897)

上一篇下一篇

猜你喜欢

热点阅读