百度地图API 地理位置获取和大头针显示当前位置

1.导入系统库 添加方式:在Xcode的Project -> Active Target ->Build Phases ->Link Binary With Libraries,添加这几个系统库即可。

CoreLocation.framework、

QuartzCore.framework、

OpenGLES.framework、

SystemConfiguration.framework、

CoreGraphics.framework、

Security.framework、

libsqlite3.0.tbd(xcode7以前为 libsqlite3.0.dylib)、

CoreTelephony.framework 、

libstdc++.6.0.9.tbd(xcode7以前为libstdc++.6.0.9.dylib)


导入百度框架

BaiduMapAPI_Base.framework

BaiduMapAPI_Location.framework

BaiduMapAPI_Map.framework


第四步、引入mapapi.bundle资源文件

如果使用了基础地图功能,需要添加该资源,否则地图不能正常显示mapapi.bundle中存储了定位、默认大头针标注View及路线关键点的资源图片,还存储了矢量地图绘制必需的资源文件。如果您不需要使用内置的图片显示功能,则可以删除bundle文件中的image文件夹。您也可以根据具体需求任意替换或删除该bundle中image文件夹的图片文件。

方法:选中工程名,在右键菜单中选择Add Files to “工程名”…,从BaiduMapAPI_Map.framework||Resources文件中选择mapapi.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。



2.在TARGETS->Build Settings->Other Linker Flags 中添加-Objc。

3.百度地图框架拖进工程里面

3.静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即在Xcode的Project -> Edit Active Target -> Build Setting 中找到 Compile Sources As,并将其设置为"Objective-C++"


4.Info.plist文件里添加

Bundle display name   项目名

NSLocationAlwaysUsageDescription  空

NSLocationWhenInUseUsageDescription 空


代码:

1.

#import <UIKit/UIKit.h>

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件



@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

//    UIWindow *window;

    UINavigationController *navigationController;

    BMKMapManager* _mapManager;

}

@property (strong, nonatomic) UIWindow *window;



@end



2.

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       // 要使用百度地图,请先启动BaiduMapManager

    _mapManager = [[BMKMapManager alloc]init];

    // 如果要关注网络及授权验证事件,请设定     generalDelegate参数

    BOOL ret = [_mapManager start:@"SYEQevXO4Mt538X6O8iCLSEyPdEi5OrV"  generalDelegate:nil];

    if (!ret) {

        NSLog(@"manager start failed!");

    }

    // Add the navigation controller's view to the window and display.

    [self.window addSubview:navigationController.view];

    [self.window makeKeyAndVisible];

    

    

    return YES;

}


3.

#import <UIKit/UIKit.h>

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>

//#import <BaiduMapAPI_Search/BMKPoiSearch.h>

//#import <BaiduMapAPI_Search/BMKPoiSearch.h>


@interface ViewController : UIViewController<BMKGeneralDelegate,BMKMapViewDelegate,BMKLocationServiceDelegate>

{

    BMKMapManager *_mapManager;                         //声明一个地图管理

    BMKMapView *_mapView;                                      //声明一张地图

    BMKPointAnnotation *_annotation;                  //声明一个标注

    BMKLocationService *_locService;

}


@end


4.


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    self.view = _mapView;

    

    //初始化BMKLocationService

    _locService = [[BMKLocationService alloc]init];

    _locService.delegate = self;

    //启动LocationService

    [_locService startUserLocationService];

    

    // 在地图中添加一个PointAnnotation

    _annotation = [[BMKPointAnnotation alloc]init];

    _annotation.title = @"test";

    _annotation.subtitle = @"this is a test!";

    [_mapView addAnnotation:_annotation];      //个人猜测,当执行此句代码时,将会调用- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation方法

    

    

    

}


-(void)viewWillAppear:(BOOL)animated

{

    [_mapView viewWillAppear];

    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

}

-(void)viewWillDisappear:(BOOL)animated

{

    [_mapView viewWillDisappear];

    _mapView.delegate = nil; // 不用时,置nil

}



//实现相关delegate 处理位置信息更新

//处理方向变更信息

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{

    NSLog(@"heading is %@",userLocation.heading);

    

    if (userLocation != nil) {

        NSLog(@"%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);

        

//        //将地图移动到当前位置

//        float zoomLevel = 0.02;

//        BMKCoordinateRegion region = BMKCoordinateRegionMake(userLocation.location.coordinate,BMKCoordinateSpanMake(zoomLevel, zoomLevel));

//        [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];

        

        //大头针摆放的坐标,必须从这里进行赋值,否则取不到值 ,这里可能涉及到委托方法执行顺序的问题

        CLLocationCoordinate2D coor;

        coor.latitude = userLocation.location.coordinate.latitude;

        coor.longitude = userLocation.location.coordinate.longitude;

        _annotation.coordinate = coor;

    }


    

}

处理位置坐标更新

//- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

//{

//    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

//}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

        BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];                 //初始化一个大头针标注

        newAnnotation.pinColor = BMKPinAnnotationColorPurple;

        newAnnotation.animatesDrop = YES;

        newAnnotation.draggable = YES;

        return newAnnotation;

    }

    return nil;

}





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值