百度地图

来源:互联网 发布:传世彩虹数据库修改器 编辑:程序博客网 时间:2024/06/10 21:25

评测环境

wifi环境下:

测试对象

百度地图 iOS SDK v2.3.0

测试环境

Mac OS X(10.10) + Xcode (5.1)

测试设备

iPhone 4S

系统版本

iOS5.1

设备网络

wifi

测试方法

客户端运行实例+测试API接口

测试时间

2014-08-0915:30

测试时长

2小时

基本参数:

是否免费

免费

支持平台

Javascript、Android、Flash、iOS

支持地图样式

标准地图,卫星地图

实时交通图

支持

离线地图

支持

覆盖物显示

标注、折线、圆、多边形、弧线、图片

自定义图层

支持

POI检索

支持

公交详情检索

支持

路径规划

包括公交、步行、驾车

地理编码

支持正向和反向地理编码

短串分享

支持

LBS云检索

支持

集成过程

客户端集成

如何快速集成百度地图:

注册百度开发者帐号=》创建应用=》下载SDK=》集成开发=》测试应用=》发布应用

1、注册百度开发者账号

百度账号注册地址:https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F,如果你已经有百度账号可以跳过这步。

登录后,进入开发者中心,注册成为百度开发者,http://developer.baidu.com/user/info?u=http://lbsyun.baidu.com/apiconsole/key?from=developer,填写好个人信息,提交。

2、创建新应用

在使用百度地图SDK之前需要先获取百度地图移动版开发密钥。

百度地图iOS SDK开发密钥的申请地址为:http://lbsyun.baidu.com/apiconsole/key。

点击以上网址进入API控制台,选择创建应用,填写应用信息:

百度地图

确认后创建完成,可以在我的应用终看到应用的ak:

百度地图

3、下载IOS SDK

百度地图iOS SDK的下载地址:http://developer.baidu.com/map/sdkiosdev-download.htm

进入后可以下载全部功能,也可以根据自己需要选择模块选择下载:

百度地图

4、集成开发

1)新建一个工程

百度地图

2)添加百度SDK和静态库

解压下载后的iOS SDK压缩包将压缩包内的inc文件夹和mapapi.bundle文件拷贝到工程目录下。

接下来根据模拟器和真机两种环境选择使用静态库文件:

如果用的真机,就导入iphoneos文件夹内的libbaidumapapi.a文件到工程,如果用的模拟器,就导入iphonesimulator文件夹内的libbaidumapapi.a文件到工程。

引入如下图所示的framework:

百度地图

3)导入头文件,初始化并启动百度地图

首先在工程的.pch文件中导入头文件,并宏定义APPKEY:

?
1
2
#import"BMapKit.h"
#define APPKEY @"9D6E6FFvXskzZge2GuwXWsRi"

在Appdelegate.h中添加代码如下,必须property  BMKMapManager,不然无法启动地图服务:

?
1
2
3
4
5
_mapManager = [[BMKMapManageralloc] init ];
BOOL ret = [_mapManagerstart:APPKEYgeneralDelegate:nil];
if (!ret) {
NSLog(@"启动失败");
}

4)创建BMKMapView,显示百度地图视图

?
1
2
3
4
5
6
#import<UIKit/UIKit.h>
  
@interfaceBaseMapViewController : UIViewController<BMKMapViewDelegate>
  
@property (nonatomic, strong) BMKMapView *mapView;
@end

在.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void)viewDidLoad
{
//这里的frame请根据手机分辨率动态设置
self.mapView.frame = CGRectMake(0, 0, 320, 460-44);
self.mapView.delegate = self;
    [self.viewaddSubview:self.mapView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [_mapViewviewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
- (void)viewWillDisappear:(BOOL)animated
{
    [_mapViewviewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}

5)更改地图显示方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch (index) {
case0:{
//设置地图显示类型,有四种标准地图、实时路况、卫星地图、同时打开实时路况和卫星地图
self.mapView.mapType = BMKMapTypeStandard;
break;
        }
case1:{
self.mapView.mapType = BMKMapTypeTrafficOn;
break;
        }
case2:{
self.mapView.mapType = BMKMapTypeSatellite;
break;
        }
default:{
self.mapView.mapType = BMKMapTypeTrafficAndSatellite;
break;
        }
}

6)添加地图标注

首先设置标注的基本属性

在.h文件描述一下

?
1
@property (nonatomic, strong) BMKPointAnnotation *annotation;

在.m文件

?
1
2
3
4
5
6
7
8
9
10
11
self.annotation = [BMKPointAnnotationnew];
CLLocationCoordinate2Dcoor;
coor.latitude = 39.911447;
coor.longitude = 116.406026;
//设置标注的坐标
self.annotation.coordinate = coor;
//标题
self.annotation.title = @"北京";
//副标题
self.annotation.subtitle = @"这个annotation可以拖拽";
    [self.mapViewaddAnnotation:self.annotation];

详细设置标注,以下方法是SDK自带的,方法内容需要自己写

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (BMKAnnotationView *)mapView:(BMKMapView *)mapViewviewForAnnotation:(id<BMKAnnotation>)annotation{
if ([annotation isKindOfClass:[BMKPointAnnotationclass]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
//动画效果
newAnnotationView.animatesDrop = YES;
//可以拖拽
newAnnotationView.draggable = YES;
//3D效果
newAnnotationView.enabled3D = YES;
returnnewAnnotationView;
    }
returnnil;
}

在不用标注的时候,把它从视图上移除

?
1
2
3
4
5
6
7
- (void)viewDidDisappear:(BOOL)animated
{
    [superviewDidDisappear:animated];
if (self.annotation!=nil) {
        [self.mapViewremoveAnnotation:self.annotation];
    }
}

7)POI兴趣点检索

百度地图SDK提供了三种POI搜索(周边、区域、城市内搜索),搜索方式都类似,下面以周边搜索做个示例。

在.h文件:

?
1
2
3
4
#import"BaseMapViewController.h"
@interfacePoiViewController : BaseMapViewController<BMKPoiSearchDelegate>
@property (nonatomic, strong) BMKPoiSearch *searcher;
@end

在.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)viewDidLoad
{
    [superviewDidLoad];
self.searcher = [BMKPoiSearchnew];
self.searcher.delegate = self;
//搜索周边
BMKNearbySearchOption *option = [BMKNearbySearchOptionnew];
//搜索周边时的中心点坐标
option.location = CLLocationCoordinate2DMake(39.911447, 116.406026);
//搜索的关键字
option.keyword = @"肯德基";
BOOL flag = [self.searcherpoiSearchNearBy:option];
if (!flag) {
NSLog(@"周边检索失败");
    }
}

搜索到结果之后的代理方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResulterrorCode:(BMKSearchErrorCode)errorCode{
//在添加标注前把以前的标注都移除掉
NSMutableArray *poiAnnotations = [NSMutableArrayarrayWithCapacity:poiResult.poiInfoList.count];
    [self.mapViewremoveAnnotations:poiAnnotations];
if (errorCode == BMK_SEARCH_NO_ERROR) {
NSLog(@"找到结果");
for (inti = 0; i<poiResult.poiInfoList.count; i++) {
BMKPoiInfo *info = [poiResult.poiInfoListobjectAtIndex:i];
BMKPointAnnotation *pointAnnotation = [BMKPointAnnotationnew];
pointAnnotation.coordinate = info.pt;
pointAnnotation.title = info.name;
pointAnnotation.subtitle = [NSStringstringWithFormat:@"%@%@",info.city,info.address];
            [poiAnnotationsaddObject:pointAnnotation];
//设置第一个搜索到的兴趣点为中心点
if (i==0) {
self.mapView.centerCoordinate = info.pt;
            }
        }
        [self.mapViewaddAnnotations:poiAnnotations];
}elseif (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){
NSLog(@"起始点有歧义,有相同名字的别的城市:%@",poiResult.cityList);
}else{
NSLog(@"未找到结果");
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
    [self.mapViewremoveAnnotations:array];
self.searcher.delegate = nil;//不用的时候代理取消
}

8)路径规划

路径规划有三种类型(公交、驾车、步行),使用方式都类似,下面以驾车做个示例。

在.h文件

?
1
2
3
4
#import"BaseMapViewController.h"
@interfaceWayPointViewController : BaseMapViewController<BMKRouteSearchDelegate>
@property (nonatomic, strong) BMKRouteSearch *routeSearch;
@end

在.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
- (void)viewDidLoad
{
    [superviewDidLoad];
  
self.routeSearch = [BMKRouteSearchnew];
self.routeSearch.delegate = self;
//线路起点信息
BMKPlanNode *start = [BMKPlanNodenew];
    start.name = @"天安门";
start.cityName = @"北京市";
//终点信息
BMKPlanNode *end = [BMKPlanNodenew];
    end.name = @"百度大厦";
end.cityName = @"北京市";
//途经点信息
BMKPlanNode *wayPoint1 = [BMKPlanNodenew];
    wayPoint1.cityName = @"北京市";
    wayPoint1.name = @"东直门";
NSMutableArray *array = [NSMutableArraynew];
    [arrayaddObject:wayPoint1];
//驾车查询
BMKDrivingRoutePlanOption *drivePlan = [[BMKDrivingRoutePlanOptionalloc]init];
drivePlan.from = start;
drivePlan.to = end;
drivePlan.wayPointsArray = array;
BOOL flag = [self.routeSearchdrivingSearch:drivePlan];
if (!flag) {
NSLog(@"公交检索发送失败");
    }
}
//根据搜索返回的结果绘制折线
- (void)onGetDrivingRouteResult:(BMKRouteSearch *)searcher result:(BMKDrivingRouteResult *)result errorCode:(BMKSearchErrorCode)error
{
//移除地图的标注和覆盖物
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
    [self.mapViewremoveAnnotations:array];
array = [NSArrayarrayWithArray:self.mapView.overlays];
    [self.mapViewremoveAnnotations:array];
if (error == BMK_SEARCH_NO_ERROR) {
BMKDrivingRouteLine *plan = [result.routesobjectAtIndex:0];
int size = plan.steps.count;
//轨迹点
CLLocationCoordinate2DpolylineCoords[size];
for (inti=0 ;i<size ; i++) {
BMKDrivingStep *drivingStep = [plan.stepsobjectAtIndex:i];
//添加annotation节点
BMKPointAnnotation* item = [BMKPointAnnotationnew];
item.coordinate = drivingStep.entrace.location;
polylineCoords[i]= drivingStep.entrace.location;
item.title = drivingStep.entraceInstruction;
            [self.mapViewaddAnnotation:item];
// 添加起点标注
if (i==0) {
item.title = @"起点";
                [self.mapViewselectAnnotation:itemanimated:YES];
            }
// 添加起点标注
if(i==size-1){
item.title = @"终点";
            }
        }
//绘制折线
           BMKPolyline* polyLine = [BMKPolylinepolylineWithCoordinates:polylineCoordscount:size];
           [self.mapViewaddOverlay:polyLine];
    }
}
//定义折线样式
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
        if ([overlay isKindOfClass:[BMKPolylineclass]]) {
BMKPolylineView* polylineView = [[BMKPolylineViewalloc] initWithOverlay:overlay];
polylineView.fillColor = [UIColorblackColor];
polylineView.strokeColor = [UIColorblueColor];
polylineView.lineWidth = 6.0f;
returnpolylineView;
    }
        returnnil;
}
- (void)viewDidDisappear:(BOOL)animated
{
[superviewDidDisappear:animated];
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
    [self.mapViewremoveAnnotations:array];
array = [NSArrayarrayWithArray:self.mapView.overlays];
    [self.mapViewremoveAnnotations:array];
self.routeSearch.delegate = nil;//不用的时候代理取消
}

9)地图定位

在.h文件

?
1
2
3
4
#import"BaseMapViewController.h"
@interfaceUserLocationViewController : BaseMapViewController<BMKLocationServiceDelegate>
@property (nonatomic, strong)BMKLocationService *localService;
@end

在.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
self.localService = [BMKLocationServicenew];
self.localService.delegate = self;
[self.localServicestartUserLocationService];
//改变地图定位样式的时候必须先关闭图层,改变样式后再打开
//关闭显示的定位图层
self.mapView .showsUserLocation = NO;
switch (index) {
case0:{
                 //三种定位模式:普通、跟随、罗盘
self.mapView.userTrackingMode = BMKUserTrackingModeNone;
break;
        }
case1:{
self.mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
break;
        }
default:{
self.mapView.userTrackingMode = BMKUserTrackingModeFollow;
break;
        }
    }
//打开显示的定位图层
self.mapView .showsUserLocation = YES;

10)短串分享

短串分享是在用户搜索查询后得到的每一个地理位置结果会对应一个短连接,用户可以通过短信等方式分享给别人。当用户受到分享的短串后,点击短串可以打开百度地图客户端或手机浏览器进行查看。

定义几个属性用来存放搜素的数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//名称
NSString* geoName;
//地址
NSString* addr;
//坐标
CLLocationCoordinate2Dpt;
//短串
NSString* shortUrl;
//分享字符串
NSString* showmeg;
首先初始化对象,并发起一个搜索
self.shareSearch = [BMKShareURLSearchnew];
self.shareSearch.delegate = self;
  
self.poiSearch = [BMKPoiSearchnew];
self.poiSearch.delegate = self;
  
BMKNearbySearchOption *option = [BMKNearbySearchOptionnew];
option.pageIndex = 0;
option.pageCapacity = 10;
option.location = CLLocationCoordinate2DMake(39.911447, 116.406026);
option.keyword = @"肯德基";
BOOL flag = [self.poiSearchpoiSearchNearBy:option];
if (!flag) {
NSLog(@"周边检索失败");
    }

搜索成功后,处理数据,获取要分享的url,发起分享

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResulterrorCode:(BMKSearchErrorCode)errorCode{
if (errorCode == BMK_SEARCH_NO_ERROR) {
for (inti = 0; i<poiResult.poiInfoList.count; i++) {
BMKPoiInfo *info = [poiResult.poiInfoListobjectAtIndex:i];
BMKPointAnnotation *pointAnnotation = [BMKPointAnnotationnew];
pointAnnotation.coordinate = info.pt;
pointAnnotation.title = info.name;
pointAnnotation.subtitle = [NSStringstringWithFormat:@"%@%@",info.city,info.address];
            [poiAnnotationsaddObject:pointAnnotation];
if (i==0) {
self.mapView.centerCoordinate = info.pt;
geoName = info.name;
addr = info.address;
pt = info.pt;
BMKPoiDetailShareURLOption *option = [BMKPoiDetailShareURLOptionnew];
option.uid = info.uid;
BOOL flag = [self.shareSearchrequestPoiDetailShareURL:option];
if (!flag) {
NSLog(@"详情url检索发送失败");
                }}}}}

处理分享的方法

?
1
2
3
4
5
6
7
8
9
10
-(void)onGetPoiDetailShareURLResult:(BMKShareURLSearch *)searcher result:(BMKShareURLResult *)result errorCode:(BMKSearchErrorCode)error
{
shortUrl = result.url;
if (error == BMK_SEARCH_NO_ERROR)
{
showmeg = [NSStringstringWithFormat:@"这里是:%@\r\n%@\r\n%@",geoName,addr,shortUrl];
UIAlertView *myAlertView = [[UIAlertViewalloc] initWithTitle:@"短串分享"message:showmegdelegate:selfcancelButtonTitle:nilotherButtonTitles:@"分享",@"取消",nil];
myAlertView.tag = 1000;
[myAlertViewshow];
}}

判断如果设备可以发送短信,则发送分享

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1000 )
    {
if (buttonIndex == 0)
        {
            Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
if ([messageClasscanSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate = self;
picker.body = [NSStringstringWithFormat:@"%@",showmeg];
                    [selfpresentModalViewController:pickeranimated:YES];
                else {
UIAlertView *myAlertView = [[UIAlertViewalloc] initWithTitle:@"当前设备无法发送短信"message:nildelegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定",nil];
                        [myAlertViewshow];
}}}}}

11)调启导航

调启导航分为两种:百度地图客户端和Web导航。

首先要配置info.plist,自定义一个URL Scheme和APP绑定,选择工程Targets》Info》URL Types,点击+号添加一个url type。

如图所示:

百度地图

下面开始编写代码,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#defineMAPScheme @"baidumapsdk://mapsdk.baidu.com"
@property (nonatomic, strong)BMKNaviPara *para;
//判读是否能打开百度地图客户端
- (BOOL)canOpenBaiDuMap
{
return [[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:MAPScheme]];
}
- (void)viewDidLoad
{
    [superviewDidLoad];
self.para = [BMKNaviParanew];
  
BOOL flag = [selfcanOpenBaiDuMap];
if (flag) {
//使用百度地图客户端导航
self.para.naviType = BMK_NAVI_TYPE_NATIVE;
//初始化终点节点
BMKPlanNode* end = [BMKPlanNodenew];
//指定终点经纬度
CLLocationCoordinate2D coor2;
coor2.latitude = 116.3956;
coor2.longitude = 39.90868;
end.pt = coor2;
//指定终点名称
        end.name = @"北京市天安门";
//指定终点
self.para.endPoint = end;
  
//指定返回自定义scheme,具体定义方法请参考常见问题
self.para.appScheme = MAPScheme;
//调启百度地图客户端导航
        [BMKNavigationopenBaiduMapNavigation:self.para];
}else{
//使用WEB导航
self.para.naviType = BMK_NAVI_TYPE_WEB;
//初始化起点节点
BMKPlanNode* start = [BMKPlanNodenew];
//指定起点经纬度
CLLocationCoordinate2D coor1;
coor1.latitude = 116.204;
coor1.longitude = 39.90868;
start.pt = coor1;
//指定起点名称
        start.name = @"西直门";
//指定起点
self.para.startPoint = start;
  
//初始化终点节点
BMKPlanNode* end = [BMKPlanNodenew];
CLLocationCoordinate2D coor2;
coor2.latitude = 116.3956;
coor2.longitude = 39.90868;
end.pt = coor2;
self.para.endPoint = end;
//指定终点名称
        end.name = @"北京市天安门";
//指定调启导航的app名称
self.para.appName = @"BaiDuMap";
//调启web导航
        [BMKNavigationopenBaiduMapNavigation:self.para];
    }
}

主要功能

实时路况:

百度地图

卫星地图:

百度地图

地图标注:

百度地图

功能特色

兴趣点搜索:

百度地图

路径规划:

百度地图

短串分享:

百度地图

DEMO展示

部分测试DEMO展示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#import"ShareUrlViewController.h"
#import<MessageUI/MessageUI.h>
#import<MessageUI/MFMessageComposeViewController.h>
@interfaceShareUrlViewController ()<MFMessageComposeViewControllerDelegate>
{
//名称
NSString* geoName;
//地址
NSString* addr;
//坐标
CLLocationCoordinate2Dpt;
//短串
NSString* shortUrl;
//分享字符串
NSString* showmeg;
}
  
@end
  
@implementationShareUrlViewController
  
- (BMKAnnotationView *)mapView:(BMKMapView *)mapViewviewForAnnotation:(id<BMKAnnotation>)annotation{
if ([annotation isKindOfClass:[BMKPointAnnotationclass]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
//动画效果
newAnnotationView.animatesDrop = YES;
//3D效果
newAnnotationView.enabled3D = YES;
//单击弹出泡泡,默认是YES
//newAnnotationView.canShowCallout = YES;
  
returnnewAnnotationView;
    }
returnnil;
}
  
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
    }
returnself;
}
  
- (void)viewDidLoad
{
    [superviewDidLoad];
self.shareSearch = [BMKShareURLSearchnew];
self.shareSearch.delegate = self;
  
self.poiSearch = [BMKPoiSearchnew];
self.poiSearch.delegate = self;
  
BMKNearbySearchOption *option = [BMKNearbySearchOptionnew];
option.pageIndex = 0;
option.pageCapacity = 10;
option.location = CLLocationCoordinate2DMake(39.911447, 116.406026);
option.keyword = @"肯德基";
BOOL flag = [self.poiSearchpoiSearchNearBy:option];
if (!flag) {
NSLog(@"周边检索失败");
    }
}
  
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResulterrorCode:(BMKSearchErrorCode)errorCode{
NSMutableArray *poiAnnotations = [NSMutableArrayarrayWithCapacity:poiResult.poiInfoList.count];
//移除地图的标注和覆盖物
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
    [self.mapViewremoveAnnotations:array];
if (errorCode == BMK_SEARCH_NO_ERROR) {
NSLog(@"找到结果");
for (inti = 0; i<poiResult.poiInfoList.count; i++) {
BMKPoiInfo *info = [poiResult.poiInfoListobjectAtIndex:i];
BMKPointAnnotation *pointAnnotation = [BMKPointAnnotationnew];
pointAnnotation.coordinate = info.pt;
pointAnnotation.title = info.name;
pointAnnotation.subtitle = [NSStringstringWithFormat:@"%@%@",info.city,info.address];
            [poiAnnotationsaddObject:pointAnnotation];
if (i==0) {
self.mapView.centerCoordinate = info.pt;
geoName = info.name;
addr = info.address;
pt = info.pt;
BMKPoiDetailShareURLOption *option = [BMKPoiDetailShareURLOptionnew];
option.uid = info.uid;
BOOL flag = [self.shareSearchrequestPoiDetailShareURL:option];
if (!flag) {
NSLog(@"详情url检索发送失败");
                }
            }
        }
        [self.mapViewaddAnnotations:poiAnnotations];
        [self.mapViewselectAnnotation:[poiAnnotationsobjectAtIndex:0] animated:YES];
}elseif (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){
NSLog(@"起始点有歧义,有相同名字的别的城市:%@",poiResult.cityList);
}else{
NSLog(@"未找到结果");
    }
}
  
- (void)onGetPoiDetailShareURLResult:(BMKShareURLSearch *)searcher result:(BMKShareURLResult *)result errorCode:(BMKSearchErrorCode)error
{
shortUrl = result.url;
if (error == BMK_SEARCH_NO_ERROR)
    {
showmeg = [NSStringstringWithFormat:@"这里是:%@\r\n%@\r\n%@",geoName,addr,shortUrl];
UIAlertView *myAlertView = [[UIAlertViewalloc] initWithTitle:@"短串分享"message:showmegdelegate:selfcancelButtonTitle:nilotherButtonTitles:@"分享",@"取消",nil];
myAlertView.tag = 1000;
        [myAlertViewshow];
    }
}
  
- (void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1000 )
    {
if (buttonIndex == 0)
        {
            Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
if ([messageClasscanSendText]) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate = self;
picker.body = [NSStringstringWithFormat:@"%@",showmeg];
                    [selfpresentModalViewController:pickeranimated:YES];
                else {
UIAlertView *myAlertView = [[UIAlertViewalloc] initWithTitle:@"当前设备无法发送短信"message:nildelegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定",nil];
                        [myAlertViewshow];
             }}}}}
  
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                      didFinishWithResult:(MessageComposeResult)result
{
        switch (result) {
           caseMessageComposeResultCancelled://用户自己取消,不用提醒
                 break;
           caseMessageComposeResultSent://后续可能不能够成功发送,所以暂时不提醒
                 break;
           caseMessageComposeResultFailed: NSLog(@"短信发送失败");
                 break;
           default:  NSLog(@"短信没有发送");
                 break;
        }
        [selfdismissModalViewControllerAnimated:YES];
}
  
-(void)viewWillDisappear:(BOOL)animated {
    [superviewWillDisappear:animated];
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
    [self.mapViewremoveAnnotations:array];
self.poiSearch.delegate = nil;
self.shareSearch.delegate = nil;
}

测试日志

周边兴趣点搜索:

百度地图

遇到问题

1.刚开始开启百度服务的时候,一直无法显示百度地图图层,笔者确定了APPKEY正确,keyStatus也返回的是0,但就是无法显示。

最后确定出错原因在ARC,在开启ARC的项目中,BMKMapManager一定要在头文件里面声明属性。

2.在使用调起百度导航的时候需要配置URL scheme并使用,在代码中需要设置appScheme,笔者按照URL Schemes+”://”+Identifier的方式拼接字符串,发现不能调启。原因是Identifier拼接的时候要反向写,比如”com.baidu.mapsdk”要改写成”mapsdk.baidu.com”。


0 0
原创粉丝点击