iOS 百度地图SDK使用

1、配置开发环境,请参考官网http://developer.baidu.com/map/index.php?title=iossdk/guide/buildproject

2、需要地图的定位功能在后台长期运行的话就要在Info.plist->Information Property List->添加key = NSLocationAlwaysUsageDescription,Value = NSLocationWhenInUseUsageDescription;key = NSLocationWhenInUseUsageDescription,Value = NSLocationWhenInUseUsageDescription。

3、定位功能,要加上代理BMKLocationServiceDelegate

-(void)viewDidLoad  
{
    //设置定位精确度,默认:kCLLocationAccuracyBest
    [BMKLocationServicesetLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //指定最小距离更新(米),默认:kCLDistanceFilterNone
    [BMKLocationServicesetLocationDistanceFilter:100.f];

    //初始化BMKLocationService  
    _locService = [[BMKLocationService alloc]init];  
    _locService.delegate = self;  
    //启动定位功能  
    [_locService startUserLocationService];
    //[_locService stopUserLocationService];//停止定位功能 
}  
//实现相关delegate 处理位置信息更新  
//处理方向变更信息  
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation  
{  
    //NSLog(@"heading is %@",userLocation.heading);  
}  
//处理位置坐标更新  
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation  
{  
    //NSLog(@"纬度 = %f,经度 = %f,高度 = %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude,userLocation.location.altitude);
    //展示定位信息
    //普通态  
    //以下_mapView为BMKMapView对象  
    _mapView.showsUserLocation = YES;//显示定位图层  
    [_mapView updateLocationData:userLocation];
}

4、正地理编码(地址->经纬度)、反地理编码(经纬度->地址),添加BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate代理

- (void)viewDidLoad {     
    [super viewDidLoad]; 

    //反地理编码
    reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
    LocSearch = [[BMKGeoCodeSearch alloc]init];
    LocSearch.delegate = self;

    //正地理编码
    geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.address = @"这里填地址";
    [LocSearch geoCode:geoCodeSearchOption];  

}  

//正地理编码代理
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        MapLatitude = result.location.latitude;//纬度
        MapLongitude = result.location.longitude;//经度
        MapAddressStr = result.address;//地址
    }
    else {
        NSLog(@"抱歉,未找到结果");
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"抱歉,未找到结果!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

//反地理编码代理
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSLog(@"地址为:%@%@%@%@%@",result.addressDetail.province,result.addressDetail.city,result.addressDetail.district,result.addressDetail.streetName,result.addressDetail.streetNumber);
}

5、正常地图显示、大头针显示、轨迹显示等,可以参考百度SDK官方文档http://developer.baidu.com/map/index.php?title=iossdk/guide/basicmap

添加折线覆盖物

CLLocationCoordinate2D coors[MapArray.count];
                 for (int i = 0; i<MapArray.count; i++) {
                     NSLog(@"%f,%f",[[[MapArray objectAtIndex:i] objectForKey:@"Latitude"] floatValue], [[[MapArray objectAtIndex:i] objectForKey:@"Longitude"] floatValue]);
                     CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[MapArray objectAtIndex:i] objectForKey:@"Latitude"] floatValue], [[[MapArray objectAtIndex:i] objectForKey:@"Longitude"] floatValue]);
                     coors[i] = annotationCoord;
                 }
                 polyline1 = [BMKPolyline polylineWithCoordinates:coors count:MapArray.count];
                 [_mapView addOverlay:polyline1];
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKPolyline class]]){
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
        polylineView.lineWidth = 5.0;

        return polylineView;
    }
    return nil;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值