百度地图的使用定位,逆地理编码,自定义不同类型的大头针
2016-06-16 10:34
309 查看
原先地图一直用高德的,但是在开发项目中 发现自定义大头针的时候高德会自动调整比例尺进行更适合的观看角度。我们的项目中是允许的,所以 临时更换了百度地图,在使用时用到了很多问题,记一下,以便以后使用
1.百度地图的配置
地图将所有的功能分为不同的包,根据需求进行下载不同的包,这点避免了增加不必要的包和代码,为项目“瘦身”
首先要倒入依赖库 CoreLocation.framework,QuartzCore.framework,OpenGLES.framework SystemConfiguration.framework,CoreGraphics.framework Security.framework libsqlite3.0.tbd,CoreTelephony.framework libstdc++.6.0.9.tbd,
在targets—>build settings 搜索Other Linker Flags 在里面添加 -Objc 如果不加 项目会报错 应该是md5加密的问题
plist文件中加入 NSLocationWhenInUseUsageDescription,使用时才会定位,避免浪费用户流量
定位功能:BaiduMapAPI_Location.framework BaiduMapAPI_Base.framework;需要这两个包,下载后倒入到项目中,
<pre name="code" class="objc">#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
@interface AppDelegate ()<BMKLocationServiceDelegate>//遵守定位的代理方法
@property (nonatomic,strong)BMKLocationService * locService;//实例化定位类
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"你在百度控制台申请的key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
//启动LocationService
[_locService startUserLocationService];
//定位的代理方法 在这里面会获得用户的位置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
2.逆地理编码 (地理编码用到很少 就不写了)
逆地理编码是让获取的用户坐标转化为坐标所对应的地址,所以要写在用户定位的代理方法中 ,获取了经纬度就进行你地理编码
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
BMKGeoCodeSearchDelegate//遵守逆地理编码的代理
@property (nonatomic,strong)BMKGeoCodeSearch *searcher;定义
#pragma mark --百度地图定位代理方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
userManager.userLat=userLocation.location.coordinate.latitude;
userManager.userLng =userLocation.location.coordinate.longitude;
_searcher =[[BMKGeoCodeSearch alloc]init];
_searcher.delegate = self;
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){<span style="font-family: Arial, Helvetica, sans-serif;">userManager.userLat</span>, userManager.userLng};
BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeoCodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
if(flag)
{
}
else
{
}
}
//接收反向地理编码结果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result
errorCode:(BMKSearchErrorCode)error{
if (error == BMK_SEARCH_NO_ERROR) {
<span style="font-family: Arial, Helvetica, sans-serif;"> result.addressDetail.字段;//包含了所有的信息 province: 省 city: 城市 district : 区 streetName: 路名 streetNumber:路号</span><pre name="code" class="objc"> <span style="font-family: Arial, Helvetica, sans-serif;"> result.address; 用户定位的地址 </span>
}
3.自定义大头针(就是定义大头针的图片)
包:BaiduMapAPI_Base.framework
我们的项目遇到了一个需求,更具不同的字段 进行添加不同的大头针,估计有4中,网上查了很多,都没有关于这种的讲解,经过几个小时的努力我发现大头针的代理方法是添加一个标示,就会调用一次,于是我就有了解决方案,实例化了一个可变字典,在添加大头针的时候 根据字段为字典添加一个图片,在代理方法中把图片赋值给大头针,这样大头针的图片就不同了, 试了试 发现可行。。。。。是不是很简单,简单的我都不敢相信
NSString * userLat;
NSString * userLng;
for (int i=0; i<self.dataArray.count; i++) {
[imageDic removeObjectForKey:@"imageName"];
if ([[[self.dataArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@""]) {
NSString * imageString =[[self.dataArray objectAtIndex:i] objectForKey:@"schoolstar"];
UIImage * logoImage =[UIImage imageNamed:[NSString stringWithFormat:@"map_%@.png",imageString]];
[imageDic setObject:logoImage forKey:@"imageName"];
}else{
UIImage * logoImage =[UIImage imageNamed:@"public.png"];
[imageDic setObject:logoImage forKey:@"imageName"];
}
userLat =[[self.dataArray objectAtIndex:i] objectForKey:@"lat"];
userLng =[[self.dataArray objectAtIndex:i] objectForKey:@"lng"];
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = [userLat floatValue];
coor.longitude =[userLng floatValue];
annotation.coordinate = coor;
annotation.title =[[self.dataArray objectAtIndex:i] objectForKey:@"schoolname"];
[self.mapView addAnnotation:annotation];
代理方法
<span style="white-space:pre"> </span> #pragma mark --百度地图自定义大头针的代理方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.image=[imageDic objectForKey:@"imageName"];
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
return newAnnotationView;
}
return nil;
}
其他的地图功能没有涉及到就没有研究,等以后研究了 再写吧
1.百度地图的配置
地图将所有的功能分为不同的包,根据需求进行下载不同的包,这点避免了增加不必要的包和代码,为项目“瘦身”
首先要倒入依赖库 CoreLocation.framework,QuartzCore.framework,OpenGLES.framework SystemConfiguration.framework,CoreGraphics.framework Security.framework libsqlite3.0.tbd,CoreTelephony.framework libstdc++.6.0.9.tbd,
在targets—>build settings 搜索Other Linker Flags 在里面添加 -Objc 如果不加 项目会报错 应该是md5加密的问题
plist文件中加入 NSLocationWhenInUseUsageDescription,使用时才会定位,避免浪费用户流量
定位功能:BaiduMapAPI_Location.framework BaiduMapAPI_Base.framework;需要这两个包,下载后倒入到项目中,
<pre name="code" class="objc">#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
@interface AppDelegate ()<BMKLocationServiceDelegate>//遵守定位的代理方法
@property (nonatomic,strong)BMKLocationService * locService;//实例化定位类
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"你在百度控制台申请的key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
//启动LocationService
[_locService startUserLocationService];
//定位的代理方法 在这里面会获得用户的位置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
2.逆地理编码 (地理编码用到很少 就不写了)
逆地理编码是让获取的用户坐标转化为坐标所对应的地址,所以要写在用户定位的代理方法中 ,获取了经纬度就进行你地理编码
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
BMKGeoCodeSearchDelegate//遵守逆地理编码的代理
@property (nonatomic,strong)BMKGeoCodeSearch *searcher;定义
#pragma mark --百度地图定位代理方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
userManager.userLat=userLocation.location.coordinate.latitude;
userManager.userLng =userLocation.location.coordinate.longitude;
_searcher =[[BMKGeoCodeSearch alloc]init];
_searcher.delegate = self;
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){<span style="font-family: Arial, Helvetica, sans-serif;">userManager.userLat</span>, userManager.userLng};
BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeoCodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
if(flag)
{
}
else
{
}
}
//接收反向地理编码结果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result
errorCode:(BMKSearchErrorCode)error{
if (error == BMK_SEARCH_NO_ERROR) {
<span style="font-family: Arial, Helvetica, sans-serif;"> result.addressDetail.字段;//包含了所有的信息 province: 省 city: 城市 district : 区 streetName: 路名 streetNumber:路号</span><pre name="code" class="objc"> <span style="font-family: Arial, Helvetica, sans-serif;"> result.address; 用户定位的地址 </span>
<pre name="code" class="objc"> poiList 最近标志物的列表
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">_businessCircle:</span></p>
}
else { }
3.自定义大头针(就是定义大头针的图片)
包:BaiduMapAPI_Base.framework
我们的项目遇到了一个需求,更具不同的字段 进行添加不同的大头针,估计有4中,网上查了很多,都没有关于这种的讲解,经过几个小时的努力我发现大头针的代理方法是添加一个标示,就会调用一次,于是我就有了解决方案,实例化了一个可变字典,在添加大头针的时候 根据字段为字典添加一个图片,在代理方法中把图片赋值给大头针,这样大头针的图片就不同了, 试了试 发现可行。。。。。是不是很简单,简单的我都不敢相信
NSString * userLat;
NSString * userLng;
for (int i=0; i<self.dataArray.count; i++) {
[imageDic removeObjectForKey:@"imageName"];
if ([[[self.dataArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@""]) {
NSString * imageString =[[self.dataArray objectAtIndex:i] objectForKey:@"schoolstar"];
UIImage * logoImage =[UIImage imageNamed:[NSString stringWithFormat:@"map_%@.png",imageString]];
[imageDic setObject:logoImage forKey:@"imageName"];
}else{
UIImage * logoImage =[UIImage imageNamed:@"public.png"];
[imageDic setObject:logoImage forKey:@"imageName"];
}
userLat =[[self.dataArray objectAtIndex:i] objectForKey:@"lat"];
userLng =[[self.dataArray objectAtIndex:i] objectForKey:@"lng"];
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = [userLat floatValue];
coor.longitude =[userLng floatValue];
annotation.coordinate = coor;
annotation.title =[[self.dataArray objectAtIndex:i] objectForKey:@"schoolname"];
[self.mapView addAnnotation:annotation];
代理方法
<span style="white-space:pre"> </span> #pragma mark --百度地图自定义大头针的代理方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.image=[imageDic objectForKey:@"imageName"];
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
return newAnnotationView;
}
return nil;
}
其他的地图功能没有涉及到就没有研究,等以后研究了 再写吧
相关文章推荐
- HTTP协议详解(真的很经典)
- 【DOS批处理】函数定义和用法
- 190. Reverse Bits
- ArcGIS教程:创建带状地图索引要素
- 用一个函数返回参数二进制中1的个数
- 不使用递归实现归并排序
- 浪潮之巅阅读笔记二
- Android(我还是个菜鸟)——UI-开源框架ImageLoader的完美例子
- Reids Cluster 部署
- iOS键盘高度的获取
- 关于九度OJ 1202问题的再讨论
- 观察者模式理解及demo
- golang map to struct
- tail和head命令
- Qt 常用类(5)—— QSize
- INS-35172 Target database memory(945MB) excessds the systems available shared memory({0}MB)
- DateTimePicker
- 网络号与子网号区别
- iOS SVN CornerStone上传静态库(.a文件)
- 华为机试---删数