百度地图定位 和添加地图围栏

(一)自iOS 地图SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,使用注意事项如下:

需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

1) NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述

2) NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述。



功能位于“基础地图(Map)”功能模块,开发者使用时请注意选择。

核心代码如下:(完整信息请参考Demo)

//以下_mapView为BMKMapView对象  _mapView.showsUserLocation = YES;//显示定位图层  _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态为普通定位模式

定位模式

目前为止,BMKMapView的定位模式(userTrackingMode)有4种分别是:

BMKUserTrackingModeNone:

普通定位模式,显示我的位置,我的位置图标和地图都不会旋转

BMKUserTrackingModeFollow :

定位跟随模式,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转

BMKUserTrackingModeFollowWithHeading :

定位罗盘模式,我的位置始终在地图中心,我的位置图标和地图都会跟着旋转

BMKUserTrackingModeHeading:

v4.1起支持,普通定位+定位罗盘模式,显示我的位置,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转。即在普通定位模式的基础上显示方向。



(二)以上为定位的分类,选择哪一种可以根据产品需求来确定,接下来要补充的则是自定义大头针的显示。和移动大头针的方法


首先声明成员变量

{

    BMKCircle* circle;

    BMKPointAnnotation* animatedAnnotation;

    BMKPointAnnotation* lockedScreenAnnotation;

}

1)创建大头针

- (void)addAnnotation {

    // 大头针  我的位置

    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(_userLoca.location.coordinate.latitude, _userLoca.location.coordinate.longitude);

    if (animatedAnnotation == nil) {

        animatedAnnotation = [[BMKPointAnnotation alloc]init];

        animatedAnnotation.coordinate = location1;

    }   

 [_mapView addAnnotation:animatedAnnotation];

}

-(void)addCompanyAnnotationWithLatitude:(double)latitudeNum WithLongitude:(double)longitudeNum{

    // 大头针 公司位置

    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(latitudeNum,longitudeNum);

     if (lockedScreenAnnotation == nil) {

        lockedScreenAnnotation = [[BMKPointAnnotation alloc]init];

        lockedScreenAnnotation.title = @"我是动画Annotation";

    }

      lockedScreenAnnotation.coordinate = location1;

    [_mapView addAnnotation:lockedScreenAnnotation];

}

//自定义百度地图的大头针   实现两种大头针区分显示的代理方法

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{

    //动画annotation

    if (annotation == animatedAnnotation) {

        NSString *AnnotationViewID = @"AnimatedAnnotation";                MyAnimatedAnnotationView *annotationView = nil;

        if (annotationView == nil) {

            annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];

        }

        NSMutableArray *images = [NSMutableArray array];

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"My_Local"]];

        [images addObject:image];

        annotationView.annotationImages = images;

        return annotationView;

    }

    //动画lockedScreenAnnotation

    if (annotation == lockedScreenAnnotation) {

        NSString *AnnotationViewID = @"lockedScreenAnnotation";

        MyAnimatedAnnotationView *annotationView = nil;

        if (annotationView == nil) {

            annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];

        }

        NSMutableArray *images = [NSMutableArray array];

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Cump_Local"]];

        [images addObject:image];

        annotationView.annotationImages = images;

        return annotationView;

    }

    return nil;

}

移动大头针

MyAnimatedAnnotationView *annotationView =(MyAnimatedAnnotationView*) [_mapView viewForAnnotation:animatedAnnotation];

CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(_userLoca.location.coordinate.latitude, _userLoca.location.coordinate.longitude);

    animatedAnnotation.coordinate = location1;

通过。viewForAnnotation 这个函数,传你要移动的大头针类型,等待这个的大头针的View ,然后重新赋值其经纬度。MyAnimatedAnnotationView 为自定义的大头针View ,会在文章最后附上自定义代码。


(三)获得地图上两点之间的距离。

1.获得第一个坐标点

BMKMapPoint userLocalpoint1 = BMKMapPointForCoordinate(userLocation.location.coordinate);

2.获得第二个坐标点 (这个坐标点在我的项目中是由后台返回的经纬度,在这里是使用字段取值的方式获得)

    CLLocationCoordinate2D location2 =CLLocationCoordinate2DMake([[_dataDic objectForKey:@"address_lat"] doubleValue],[[_dataDic objectForKey:@"address_lng"] doubleValue]);

    BMKMapPoint point2 = BMKMapPointForCoordinate(location2);

3.通过下面的函数 获取两者之间的距离

    CLLocationDistance distance = BMKMetersBetweenMapPoints(userLocalpoint1, point2);



(四)添加圆形地图围栏

百度地图的圆形围栏 类:BMKCircle

1)BMKCircle* circle;  //声明变量

2)mapView遵循BMKMapViewDelegate代理,并实现添加覆盖物的代理方法

#pragma mark implement BMKMapViewDelegate//根据overlay生成对应的View- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay

{

    if ([overlay isKindOfClass:[BMKCircle class]])

    { BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];

        circleView.fillColor = [SHDMapCircleColor colorWithAlphaComponent:0.2];

        circleView.strokeColor = [SHDMapCircleColor colorWithAlphaComponent:1.0];

        circleView.lineWidth = 1.0;

        return circleView;

    }

    return nil;

}

参数说明

circleView.fillColor :          为设置外边框的颜色

circleView.lineWidth = 1.0;  为设置的边框宽度

circleView.strokeColor :   为设置圆行覆盖物内部填充颜色



(五)附加自定义大头针View的完整代码。

1. 头文件

//// MyAnimatedAnnotationView.h// IphoneMapSdkDemo//// Created by wzy on 14-11-27.// Copyright (c) 2014年 Baidu. All rights reserved.//#import@interface MyAnimatedAnnotationView : BMKAnnotationView

@property (nonatomic, strong) NSMutableArray *annotationImages;

@property (nonatomic, strong) UIImageView *annotationImageView;

@end


2.实现文件

//// MyAnimatedAnnotationView.m// IphoneMapSdkDemo//// Created by wzy on 14-11-27.// Copyright (c) 2014年 Baidu. All rights reserved.//#import "MyAnimatedAnnotationView.h"@implementation MyAnimatedAnnotationView@synthesize annotationImageView = _annotationImageView;@synthesize annotationImages = _annotationImages;- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self) {

//        [self setBounds:CGRectMake(0.f, 0.f, 30.f, 30.f)];

        [self setBounds:CGRectMake(0.f, 0.f, 32.f, 32.f)];

        [self setBackgroundColor:[UIColor clearColor]];


        _annotationImageView = [[UIImageView alloc] initWithFrame:self.bounds];

        _annotationImageView.contentMode = UIViewContentModeCenter;

        [self addSubview:_annotationImageView];

    }

    return self;

}

- (void)setAnnotationImages:(NSMutableArray *)images {

    _annotationImages = images;

    [self updateImageView];

}

- (void)updateImageView {

    if ([_annotationImageView isAnimating]) {

        [_annotationImageView stopAnimating];

    }


    _annotationImageView.animationImages = _annotationImages;

    _annotationImageView.animationDuration = 0.5 * [_annotationImages count];

    _annotationImageView.animationRepeatCount = 0;

    [_annotationImageView startAnimating];

}

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容