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

O 需求

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

一 准备

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

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

二 编码

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

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

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

在ViewController.m中添加如下代码

 1 - (void)viewDidLoad
 2 {
 3     [superviewDidLoad];
 4      // Do any additional setup after loading the view, typically from a nib.
 5    
 6     //启动BMKMapManager
 7     _mapManager = [[BMKMapManageralloc]init];
 8     BOOL ret = [_mapManagerstart:@"2772BD5CAFF652491F65707D6D5E9ABEBF3639CC"generalDelegate:self];
 9     if (!ret) {
10         NSLog(@"manager start failed!");
11     }
12    
13     //创建一张百度地图
14     _mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
15     [_mapViewsetShowsUserLocation:YES];               //开启定位功能
16     _mapView.delegate = self;
17     [self.viewaddSubview:_mapView];
18    
19     // 在地图中添加一个PointAnnotation
20     _annotation = [[BMKPointAnnotationalloc]init];
21     _annotation.title = @"test";
22     _annotation.subtitle = @"this is a test!";
23     [_mapViewaddAnnotation:_annotation];      //个人猜测,当执行此句代码时,将会调用- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation方法
24 }
25  
26 #pragma mark -  实现 BMKMapViewDelegate 中的方法
27  
28 /**
29  在地图View将要启动定位时,会调用此函数
30  @param mapView 地图View
31  下面的这个方法,貌似并没有被启动啊?是否是可有可无的?
32  */
33 - (void)mapViewWillStartLocatingUser:(BMKMapView *)mapView
34 {
35      NSLog(@"start locate");
36 }
37  
38 /**
39      用户位置更新后,会调用此函数
40      @param mapView 地图View
41      @param userLocation 新的用户位置
42     在实际使用中,只需要    [mapView setShowsUserLocation:YES];    mapView.delegate = self;   两句代码就可以启动下面的方法。疑问,为什么我的位置没有移动的情况下,这个方法循环被调用呢?
43  */
44 - (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
45 {
46      if (userLocation != nil) {
47        NSLog(@"%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
48  
49         //将地图移动到当前位置
50         float zoomLevel = 0.02; 
51         BMKCoordinateRegion region = BMKCoordinateRegionMake(userLocation.location.coordinate,BMKCoordinateSpanMake(zoomLevel, zoomLevel)); 
52         [_mapViewsetRegion:[_mapViewregionThatFits:region] animated:YES];
53        
54         //大头针摆放的坐标,必须从这里进行赋值,否则取不到值,这里可能涉及到委托方法执行顺序的问题
55         CLLocationCoordinate2D coor;
56         coor.latitude = userLocation.location.coordinate.latitude;
57         coor.longitude = userLocation.location.coordinate.longitude;
58         _annotation.coordinate = coor;
59      }
60 }
61  
62 /**
63      定位失败后,会调用此函数
64      @param mapView 地图View
65      @param error  错误号,参考CLError.h中定义的错误号
66  */
67 - (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
68  
69 {
70      if (error != nil)
71        NSLog(@"locate failed: %@", [error localizedDescription]);
72      else {
73        NSLog(@"locate failed");
74      }
75 }
76  
77 - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
78 {
79      if ([annotation isKindOfClass:[BMKPointAnnotationclass]]) {
80        BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationViewalloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];                 //初始化一个大头针标注
81         NSLog(@"I'm coming!");
82        newAnnotation.pinColor = BMKPinAnnotationColorPurple;  
83        newAnnotation.animatesDrop = YES;
84        newAnnotation.draggable = YES;
85        return newAnnotation;  
86      }
87      returnnil;
88 }

执行后,效果如下:

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

五 思路

六 分析

用到的类:

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

原文地址:https://www.cnblogs.com/ygm900/p/2783147.html