(一)自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