集成百度地图,实现基本定位,检索,自定义大头针。
1.集成百度地图,当然前提还是去注册百度地图开发者
2.接下来创建应用
创建应用.png
3.百度地图AK
百度地图AK.png
4.cocoaPods导入百度SDK
platform :ios, '6.0'
pod 'BaiduMapKit'
5.项目plist配置
plist配置.png接下来正事开始!
6.在AppDelegate.m中导入头文件:
#import <BaiduMapAPI_Base/BMKMapManager.h>
在didFinishLaunchingWithOptions方法中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建并初始化一个引擎对象
BMKMapManager *manager = [[BMKMapManager alloc] init];
//启动地图引擎
BOOL success = [manager start:@"百度地图AK" generalDelegate:nil];
if (!success) {
NSLog(@"失败");
}
return YES;
}
7.在viewController.m 中导入头文件,根据需要导入
#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
#import "MZLPopImage.h"
//声明BMKMapView
@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate>
@property (nonatomic,strong) BMKMapView *mapView; //地图视图
@property (nonatomic,strong) BMKLocationService *service;//定位
@property (nonatomic,strong) BMKPoiSearch *poiSearch;//搜索服务
@property (nonatomic,strong)UIView *popView;
@end
//mapView内存问题
-(void)viewWillAppear:(BOOL)animated {
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
_service.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated {
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
_service.delegate = nil;
}
//地图初始化
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化地图
self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate =self;
self.mapView.userTrackingMode = BMKUserTrackingModeNone;
//定位之后的箭头
BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc] init]; //此类表示定位图层自定义样式参数
displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
displayParam.isAccuracyCircleShow = true;//精度圈是否显示
displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
[_mapView updateLocationViewWithParam:displayParam]; //动态定制我的位置样式
self.service.allowsBackgroundLocationUpdates = YES; //是否允许后台定位更新。默认为NO。只在iOS 9.0之后起作用。设为YES时,Info.plist中 UIBackgroundModes 必须包含 "location"
//添加到view上
[self.view addSubview:self.mapView];
//初始化定位
self.service = [[BMKLocationService alloc] init];
//设置代理
self.service.delegate = self;
//开启定位
[self.service startUserLocationService];
}
//用户位置更新后,会调用此函数
/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//展示定位
self.mapView.showsUserLocation = YES;
//更新位置数据
[self.mapView updateLocationData:userLocation];
//获取用户的坐标
self.mapView.centerCoordinate = userLocation.location.coordinate;
self.mapView.zoomLevel = 20;
//初始化搜索
self.poiSearch =[[BMKPoiSearch alloc] init];
self.poiSearch.delegate = self;
//初始化一个周边云检索对象
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init];
//索引 默认为0
option.pageIndex = 0;
//页数默认为10
option.pageCapacity = 50;
//搜索半径
option.radius = 200;
//检索的中心点,经纬度
option.location = userLocation.location.coordinate;
//搜索的关键字
option.keyword = @"酒店";
//根据中心点、半径和检索词发起周边检索
BOOL flag = [self.poiSearch poiSearchNearBy:option];
if (flag) {
NSLog(@"搜索成功");
//关闭定位
[self.service stopUserLocationService];
}
else {
NSLog(@"搜索失败");
}
}
//返回POI搜索结果
/**
*返回POI搜索结果
*@param searcher 搜索对象
*@param poiResult 搜索结果列表
*@param errorCode 错误号,@see BMKSearchErrorCode
*/
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
//若搜索成功
if (errorCode ==BMK_SEARCH_NO_ERROR) {
//POI信息类
//poi列表
for (BMKPoiInfo *info in poiResult.poiInfoList) {
[self.dataArray addObject:info];
//初始化一个点的注释 //只有三个属性
BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init];
//坐标
annotoation.coordinate = info.pt;
//title
annotoation.title = info.name;
//子标题
annotoation.subtitle = info.address;
//将标注添加到地图上
[self.mapView addAnnotation:annotoation];
}
}
}
//检索结果
/**
*返回POI详情搜索结果
*@param searcher 搜索对象
*@param poiDetailResult 详情搜索结果
*@param errorCode 错误号,@see BMKSearchErrorCode
*/
- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode {
NSLog(@"%@",poiDetailResult.name);
}
//根据anntation生成对应的View,自定义大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
NSString *AnnotationViewID = @"AnnotationView";
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
// 从天上掉下效果
newAnnotationView.animatesDrop = NO;
// 设置可拖拽
newAnnotationView.draggable = YES;
UIImage *image = [self getAnnotation:annotation bSelected:NO];
if (image){
newAnnotationView.image = image;
}
_popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 230, 60)];
_popView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.95];
_popView.layer.cornerRadius = 5.f;
_popView.clipsToBounds = YES;
_popView.layer.borderColor = [UIColor redColor].CGColor;
_popView.layer.borderWidth = 1.f;
//自定义显示的内容
UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 170, 30)];
driverName.text = @"老北京炸酱面";
driverName.font = [UIFont systemFontOfSize:14.f];
driverName.textColor = [UIColor redColor];
driverName.textAlignment = NSTextAlignmentCenter;
[_popView addSubview:driverName];
UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(driverName.frame), 170, 30)];
carName.text = @"就是这个味";
carName.font = [UIFont systemFontOfSize:15.f];
carName.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
carName.textAlignment = NSTextAlignmentCenter;
[_popView addSubview:carName];
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(CGRectGetMaxX(driverName.frame), 0, 60, CGRectGetHeight(_popView.frame));
[button setTitle:@"去购买" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor colorWithRed:0.9877 green:0.1089 blue:0.1633 alpha:1.0];
[button addTarget:self action:@selector(buyClick:) forControlEvents:UIControlEventTouchUpInside];
[_popView addSubview:button];
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:_popView];
pView.frame = CGRectMake(0, 0, 200, 60);
pView.userInteractionEnabled = YES;
newAnnotationView.paopaoView = nil;
newAnnotationView.paopaoView = pView;
return newAnnotationView;
}
return nil;
}
//设置自定义大头针图片
- (UIImage*)getAnnotation:(id<BMKAnnotation>)annotation bSelected:(BOOL)bSel
{
return [MZLPopImage getUIImageByValue:[@"99" intValue] andIsSelect:bSel];
}
//当选中一个annotation views时,调用此接口,检索信息
/**
*当选中一个annotation views时,调用此接口
*@param mapView 地图View
*@param views 选中的annotation views
*/
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view {
//poi详情检索信息类
BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init];
BMKPoiInfo *info = self.dataArray.firstObject;
//poi的uid,从poi检索返回的BMKPoiResult结构中获取
option.poiUid = info.uid;
/**
*根据poi uid 发起poi详情检索
*异步函数,返回结果在BMKPoiSearchDelegate的onGetPoiDetailResult通知
*@param option poi详情检索参数类(BMKPoiDetailSearchOption)
*@return 成功返回YES,否则返回NO
*/
BOOL flag = [self.poiSearch poiDetailSearch:option];
if (flag) {
NSLog(@"检索成功");
}
else {
NSLog(@"检索失败");
}
view.image = [self getAnnotation:view.annotation bSelected:YES];
}
//当取消选中一个annotation views时,调用此接口
/**
*当取消选中一个annotation views时,调用此接口
*@param mapView 地图View
*@param views 取消选中的annotation views
*/
- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view
{
view.image = [self getAnnotation:view.annotation bSelected:NO];
}
//定位失败的调用函数
/**
*定位失败后,会调用此函数
*@param error 错误号
*/
- (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"%@", error);
}
//点击地图空白处
/**
*点中底图空白处会回调此接口
*@param mapview 地图View
*@param coordinate 空白处坐标点的经纬度
*/
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
{
[UIView animateWithDuration:0.1 animations:^{
_popView.alpha = 0;
} completion:^(BOOL finished) {
}];
}
//购买事件
- (void)buyClick:(UIButton *)btn {
NSLog(@"-- 我去购买了 --");
}
简单效果:
效果图.gif终于完了,够详细了吧。自己写的,看看就好。
【Update】
点击大头针切换图片MZLPopImage类
在MZLPopImage.h中
@interface MZLPopImage : NSObject
+ (UIImage*)getUIImageByValue:(NSInteger)nValue andIsSelect:(BOOL)bSelect;
@end
在MZLPopImage.m中
#define IMAGEOF(x) ([UIImage imageNamed:x])
@implementation MZLPopImage
+(UIImage*)convertViewToImage:(UIView*)view
{
CGSize s = view.bounds.size;
//第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//图片选中,可设置nVlaue图片上文字
+ (UIImage*)getUIImageByValue:(NSInteger)nValue andIsSelect:(BOOL)bSelect
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 17, 25)];
imageView.image = bSelect ? IMAGEOF(@"icon_btn_red") : IMAGEOF(@"icon_btn_black");
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 40, 28)];
lb.text = [NSString stringWithFormat:@"%ld",(long)nValue];
lb.font = [UIFont boldSystemFontOfSize:18.f];
lb.adjustsFontSizeToFitWidth = YES;
lb.textAlignment = NSTextAlignmentCenter;
lb.textColor = [UIColor whiteColor];
[imageView addSubview:lb];
return [MZLPopImage convertViewToImage:imageView];
}
以上是全部代码,如果实现有问题请在下面回复。
最后附上官方文档。
网友评论