ios百度地图的使用(基本定位和地理编码)

摘要  狗屎的百度地图,建议大家都别用,一坨屎,不过还是给大家分享一下了,基于百度地图2.3.0 SDK

在这里就不在介绍百度的具体配置,配置详见http://developer.baidu.com/map/index.php?title=iossdk

1.首先接受基本的地图功能

新建一个地图类,xib拖也行,我这边是代码实现的。

?
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
  _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)]; //添加mapVIew
  [self.view addSubview:_mapView];
#pragma mark - 设置mapView属性
-( void )setMapViewProperty
{
     _mapView.mapType = BMKUserTrackingModeFollowWithHeading;
     _mapView.showsUserLocation = YES;  //是否显示定位图层(即我的位置的小圆点)
     _mapView.zoomLevel = 16; //地图显示比例
     _mapView.rotateEnabled = NO;  //设置是否可以旋转
     
     [self  passLocationValue];
}
#pragma mark -传入定位坐标 
//设置定位到得用户的位置,这里是简单的应用方法(必须打开程序时已经获取到地理位置坐标,为了解决地图定位时总是先显示天安门)
-( void )passLocationValue
{
     BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake([UserLocationManager sharedInstance].clloction.coordinate, BMKCoordinateSpanMake(0.02f,0.02f));
     BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
     [_mapView setRegion:adjustedRegion animated:YES];
     
}
#pragma mark -设置定位圆点属性
-( void )setUserImage
{
     //用户位置类
     BMKLocationViewDisplayParam* param = [[BMKLocationViewDisplayParam alloc] init];
     param.locationViewOffsetY = 0; //偏移量
     param.locationViewOffsetX = 0;
     param.isAccuracyCircleShow =NO; //设置是否显示定位的那个精度圈
     param.isRotateAngleValid = NO;
     [_mapView updateLocationViewWithParam:param];
}

这样基本的地图界面就出来了

如果你需要在地图上做一些请求,可以实现BMKMapViewDelegate,以下是mapView的一些协议方法

?
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
**
  *地图区域即将改变时会调用此接口
  *@param mapview 地图View
  *@param animated 是否动画
  */
- ( void )mapView:(BMKMapView *)mapView regionWillChangeAnimated:( BOOL )animated
{
   //TODO
}
 
/**
  *地图区域改变完成后会调用此接口
  *@param mapview 地图View
  *@param animated 是否动画
  */
- ( void )mapView:(BMKMapView *)mapView regionDidChangeAnimated:( BOOL )animated
{
     //TODO
}
/**
  *地图状态改变完成后会调用此接口
  *@param mapview 地图View
  */
- ( void )mapStatusDidChanged:(BMKMapView *)mapView
{
     //TODO
}


2.地图定位

我这边是将定位封装了一个独立的manager类来管理定位和地图上滑动到的位置,是将定位功能和地图mapVIew独立开来,管理地理移动位置的变化

?
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
#import <Foundation/Foundation.h>
#import "BMapKit.h"
@interface UserLocationManager : NSObject <BMKMapViewDelegate,BMKLocationServiceDelegate>
{
     CLLocation *cllocation;
     BMKReverseGeoCodeOption *reverseGeoCodeOption; //逆地理编码
}
@property (strong,nonatomic) BMKLocationService *locService;
 
 
//城市名
@property (strong,nonatomic) NSString *cityName;
 
//用户纬度
@property (nonatomic,assign)  double  userLatitude;
 
//用户经度
@property (nonatomic,assign)  double  userLongitude;
 
//用户位置
@property (strong,nonatomic) CLLocation *clloction;
 
 
//初始化单例
+ (UserLocationManager *)sharedInstance;
 
//初始化百度地图用户位置管理类
- ( void )initBMKUserLocation;
 
//开始定位
-( void )startLocation;
 
//停止定位
-( void )stopLocation;
 
@end
 
 
 
#import "UserLocationManager.h"
@implementation UserLocationManager
 
+ (UserLocationManager *)sharedInstance
{
     static  UserLocationManager *_instance = nil;
     
     @synchronized (self) {
         if  (_instance == nil) {
             _instance = [[self alloc] init];
         }
     }
     
     return  _instance;
}
-(id)init
{
     if  (self == [super init])
     {
       [self initBMKUserLocation];
     }
     return  self;
}
 
#pragma 初始化百度地图用户位置管理类
/**
  *  初始化百度地图用户位置管理类
  */
- ( void )initBMKUserLocation
{
     _locService = [[BMKLocationService alloc]init];
     _locService.delegate = self;
     [self startLocation];
 
}
#pragma 打开定位服务
/**
  *  打开定位服务
  */
-( void )startLocation
{
     [_locService startUserLocationService];
}
#pragma 关闭定位服务
 
/**
  *  关闭定位服务
  */
-( void )stopLocation
{
     [_locService stopUserLocationService];
}
#pragma BMKLocationServiceDelegate
/**
  *用户位置更新后,会调用此函数
  *@param userLocation 新的用户位置
  */
- ( void )didUpdateUserLocation:(BMKUserLocation *)userLocation
{
      cllocation = userLocation.location;
     _clloction = cllocation;
     _userLatitude = cllocation.coordinate.latitude;
     _userLongitude = cllocation.coordinate.longitude;
     [self stopLocation];(如果需要实时定位不用停止定位服务)
}
/**
  *在停止定位后,会调用此函数
  */
- ( void )didStopLocatingUser
{
}
/**
  *定位失败后,会调用此函数
  *@param error 错误号
  */
- ( void )didFailToLocateUserWithError:(NSError *)error
{
     [self stopLocation];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值