您的位置:首页 > 其它

百度地图的使用定位,逆地理编码,自定义不同类型的大头针

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>
<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;
}


其他的地图功能没有涉及到就没有研究,等以后研究了  再写吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: