04 将当前位置用大头针标注到百度地图上

O 需求

将当前位置用大头针标注到百度地图上

一 准备

详见《01 如何将百度地图加入IOS应用程序?》

注意,此应用程序运行环境是IOS DEVICE 。所以,相应的开发包一定要做好调整。

二 编码

(New标示本次新添加的代码;Delete表示本次需要删除的代码;Modify表示本次被修改的代码)

1、在ViewController.h中修改代码如下

#import <UIKit/UIKit.h>
#import "BMapKit.h"
@interface ViewController : UIViewController<BMKGeneralDelegate,BMKMapViewDelegate>
{
        BMKMapManager *_mapManager;                          //声明一个地图管理
        BMKMapView *_mapView;                                 //声明一张地图
        BMKPointAnnotation *_annotation;                         //声明一个标注
}
@end

在ViewController.m中添加如下代码

- (void)viewDidLoad
{
    [superviewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
   
    //启动BMKMapManager
    _mapManager = [[BMKMapManageralloc]init];
    BOOL ret = [_mapManagerstart:@"2772BD5CAFF652491F65707D6D5E9ABEBF3639CC"generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
   
    //创建一张百度地图
    _mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    [_mapViewsetShowsUserLocation:YES];               //开启定位功能
    _mapView.delegate = self;
    [self.viewaddSubview:_mapView];
   
    // 在地图中添加一个PointAnnotation
    _annotation = [[BMKPointAnnotationalloc]init];
    _annotation.title = @"test";
    _annotation.subtitle = @"this is a test!";
    [_mapViewaddAnnotation:_annotation];      //个人猜测,当执行此句代码时,将会调用- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation方法
}
 
#pragma mark -  实现 BMKMapViewDelegate 中的方法
 
/**
 在地图View将要启动定位时,会调用此函数
 @param mapView 地图View
 下面的这个方法,貌似并没有被启动啊?是否是可有可无的?
 */
- (void)mapViewWillStartLocatingUser:(BMKMapView *)mapView
{
     NSLog(@"start locate");
}
 
/**
     用户位置更新后,会调用此函数
     @param mapView 地图View
     @param userLocation 新的用户位置
    在实际使用中,只需要    [mapView setShowsUserLocation:YES];    mapView.delegate = self;   两句代码就可以启动下面的方法。疑问,为什么我的位置没有移动的情况下,这个方法循环被调用呢?
 */
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
     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)); 
        [_mapViewsetRegion:[_mapViewregionThatFits:region] animated:YES];
       
        //大头针摆放的坐标,必须从这里进行赋值,否则取不到值,这里可能涉及到委托方法执行顺序的问题
        CLLocationCoordinate2D coor;
        coor.latitude = userLocation.location.coordinate.latitude;
        coor.longitude = userLocation.location.coordinate.longitude;
        _annotation.coordinate = coor;
     }
}
 
/**
     定位失败后,会调用此函数
     @param mapView 地图View
     @param error  错误号,参考CLError.h中定义的错误号
 */
- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
 
{
     if (error != nil)
       NSLog(@"locate failed: %@", [error localizedDescription]);
     else {
       NSLog(@"locate failed");
     }
}
 
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
     if ([annotation isKindOfClass:[BMKPointAnnotationclass]]) {
       BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationViewalloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];                 //初始化一个大头针标注
        NSLog(@"I'm coming!");
       newAnnotation.pinColor = BMKPinAnnotationColorPurple;  
       newAnnotation.animatesDrop = YES;
       newAnnotation.draggable = YES;
       return newAnnotation;  
     }
     returnnil;
}

执行后,效果如下:


四 下载          ......去下载源码咯 ......

五 思路

六 分析

用到的类:

BMKMapManager(配合BMKMapView

使用,用于启动一张地图

BMKMapView

BMKCoordinateRegion (配合BMKMapView

使用,用于设置地图显示的范围和边界)

BMKCoordinateRegionMake(配合BMKCoordinateRegion进行使用,用于创建一个BMKCoordinateRegion对象)

 

BMKPointAnnotation

CLLocationCoordinate2D

配合BMKPointAnnotation使用,用于设置大头针显示的位置)

 

用到的方法:

用户位置更新后,会调用此函数

- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation

//根据anntation生成对应的View

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

 

七 疑问

1)        以上委托方法的触发时机和执行顺序是什么?(这个问题必须解决,这个问题关系到大头针是否显示,何时显示的问题。)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值