1.百度地图使用
- MessageUI.framework
- Security.framework
- CoreGraphics.framework
- SystemContiguration.framework
- QuartzCore.framework
- CoreLocation.framework
- OpenGLES.framework
以上是地图使用导入的系统类库
BaiduMapAPI.framework是下载官方SDK中获取,包括mapapi.bundle和inc文件夹一起导入到工程里
代码:
#import "AppDelegate.h"
#import <BaiduMapAPI/BMapKit.h>
#import <BaiduMapAPI/BMKMapView.h>
#import "ViewController.h"
@interface AppDelegate ()
//主引擎属性
@property(strong,nonatomic)BMKMapManager *mapManager;
@property(strong,nonatomic)UINavigationController *navigationController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//百度appkey FzfCFmGV8m6ZwZfxF57B3H4o
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//启动BaiduMapManager
self.mapManager = [[BMKMapManager alloc] init];
BOOL ret = [self.mapManager start:@"FzfCFmGV8m6ZwZfxF57B3H4o" generalDelegate:self];
if (!ret) {
NSLog(@"百度地图开启定位");
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *Root = [[ViewController alloc] init];
UINavigationController *naV = [[UINavigationController alloc] initWithRootViewController:Root];
self.window.rootViewController = naV;
return YES;
}
ViewController.m中
#import "ViewController.h"
@interface ViewController ()
/** 百度地图View */
@property(strong, nonatomic)BMKMapView *mapView;
@property(strong, nonatomic)BMKMapView *bmkMapView;
@property(nonatomic,retain)BMKLocationService *locationService;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化定位服务对象
// self.locationService = [[BMKLocationService alloc] init];
// self.locationService.delegate = self;
// //显示定位的蓝点儿必须先开启定位服务
// [self.locationService startUserLocationService];
[self initBMLocationService];
//初始化地图视图
self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
//切换卫星图
//[self.mapView setMapType:BMKMapTypeSatellite];
[self setMapViewProperty];
//地图级别
self.mapView.zoomLevel = 18;
self.mapView.mapType = BMKMapTypeStandard;
//显示比例尺
// self.mapView.showMapScaleBar = YES;
//自定义比例尺
self.mapView.mapScaleBarPosition = CGPointMake(240, 280);
self.view = self.mapView;
// http://bbs.lbsyun.baidu.com/viewthread.php?tid=3621&extra=page3D1
[self.mapView setShowsUserLocation:NO];
self.mapView.userTrackingMode = BMKUserTrackingModeNone; // 地图模式
[self.mapView setShowsUserLocation:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(leftButton)];
[self.navigationItem.leftBarButtonItem setTitle:@"定位"];
BMKLocationViewDisplayParam *testparam = [[BMKLocationViewDisplayParam alloc]init];
//跟随态旋转角度是否生效
testparam.isRotateAngleValid = YES;
//精度圈是否显示
testparam.isAccuracyCircleShow = YES;
//定位图标名称
testparam.locationViewImgName = @"bnavi_icon_location_fixed@2x";
//定位图标偏移量
testparam.locationViewOffsetX = 0;
testparam.locationViewOffsetY = 0;
[self.mapView updateLocationViewWithParam:testparam];
}
-(void)leftButton
{
NSLog(@"进入普通定位态");
[self.locationService startUserLocationService];
_mapView.showsUserLocation = NO;//先关闭显示的定位图层
_mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
_mapView.showsUserLocation = YES;//显示定位图层
// 设置当前地图的显示范围,直接显示到用户位置
// BMKCoordinateRegion adjustRegion = [self.mapView regionThatFits:BMKCoordinateRegionMake(self.bmkLocationService.userLocation.location.coordinate, BMKCoordinateSpanMake(0.02f,0.02f))];
//设置地图当前的显示范围
// [self.mapView setRegion:adjustRegion animated:YES];
//表示一个点的annotation
//CLLocationCoordinate2D 表示经纬度
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
//此处暂写北京的经纬度
coor.latitude = 39.915;
coor.longitude = 116.404;
annotation.coordinate = coor;
annotation.title = @"北京天安门";
//向地图窗口添加标注
[_mapView addAnnotation:annotation];
// 添加一个PointAnnotation
BMKPointAnnotation *pointAnnotation2 = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor2;
coor2.latitude = 39.913;
coor2.longitude = 116.402;
pointAnnotation2.title = @"3号区";
pointAnnotation2.coordinate = coor2;
[_mapView addAnnotation:pointAnnotation2];
// [annotationArray addObject:pointAnnotation2];
}
- (void)initBMLocationService
{
// 初始化位置百度位置服务
self.bmkLocationService = [[BMKLocationService alloc] init];
// 设置距离过滤,表示每移动10更新一次位置
[BMKLocationService setLocationDistanceFilter:10];
// 设置定位精度
[BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyBest];
}
//注意必须使真机才可使用手机定位
- (void)setMapViewProperty
{
// 显示定位图层
self.mapView.showsUserLocation = YES;
// 设置定位模式
self.mapView.userTrackingMode = BMKUserTrackingModeNone;
// 允许旋转地图
self.mapView.rotateEnabled = YES;
// 显示比例尺
self.bmkMapView.showMapScaleBar = YES;
self.bmkMapView.mapScaleBarPosition = CGPointMake(self.view.frame.size.width - 50, self.view.frame.size.height - 50);
// 定位图层自定义样式参数
BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
displayParam.isRotateAngleValid = NO;//跟随态旋转角度是否生效
displayParam.isAccuracyCircleShow = NO;//精度圈是否显示
displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
displayParam.locationViewImgName = @"walk";
//动态指定我的位置样式
[self.mapView updateLocationViewWithParam:displayParam];
}
以上就是对于百度地图的基本使用,希望大家能有所收获,笔者初次撰写博客,有些地方可能不是很全面,希望大家见谅,笔者也会继续努力,与大家共同分享iOS相关知识,谢谢。