您的位置:首页 > 移动开发 > IOS开发

iOS 百度地图SDK使用

2015-09-21 10:01 387 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: