百度地圖集成 ,劃線定位 ,修改地圖圖標(biāo)時時劃線 害幅,源碼分享 話不多說直接源碼刨秆。
注意:導(dǎo)入 sdk,如有方法報(bào)錯 注意inforplist 和 viewcontroller.mm
1.AppDelegate 添加頭文件 定義屬性
@interface AppDelegate (){
BMKMapManager* _mapManager;
}
2.didFinishLaunchingWithOptions 方法里添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_mapManager = [[BMKMapManager alloc]init];
// 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗(yàn)證事件,請?jiān)O(shè)定 generalDelegate參數(shù)
BOOL ret = [_mapManager start:@"imlNRyuFnypuLPFe7WCAjnSPSLjC2EE1" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
// Add the navigation controller's view to the window and display.
[self.window makeKeyAndVisible];
return YES;
}
viewController里添加
// ViewController.m
// baidumap
// Created by 陳賀 on 16/8/26.
// Copyright ? 2016年 itcast. All rights reserved.
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "CoorModel.h"
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關(guān)所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云檢索功能所有的頭文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計(jì)算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達(dá)功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件
#import "CoorModel.h"
@interface ViewController ()<BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate ,BMKMapViewDelegate,BMKLocationServiceDelegate,CLLocationManagerDelegate>
{
BMKMapView *_mapView;
BMKPoiSearch *_searcher;
BMKLocationService *_locService;
NSMutableArray *dataArr;
}
@property(nonatomic,strong)CLLocationManager *locationManager;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil酌毡,否則影響內(nèi)存的釋放
_locService.delegate = self;
dataArr = [NSMutableArray array];
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; //不用時,置nil
_locService.delegate = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建地圖
_mapView = [BMKMapView new];
// 主動請求權(quán)限
// 重新設(shè)置百度圖片的圖標(biāo)
BMKLocationViewDisplayParam *displayParam = [BMKLocationViewDisplayParam new];
displayParam.isRotateAngleValid =YES;
displayParam.isAccuracyCircleShow =NO;
displayParam.locationViewImgName = @"touxiang";
displayParam.locationViewOffsetX =0;
displayParam.locationViewOffsetY =0;
[_mapView updateLocationViewWithParam:displayParam];// BMKCoordinateRegion region ;//表示范圍的結(jié)構(gòu)體
// region.center = coordinate;//中心點(diǎn)
// region.span.latitudeDelta = 0.3;//經(jīng)度范圍(設(shè)置為0.1表示顯示范圍為0.2的緯度范圍)
// region.span.longitudeDelta = 0.3;//緯度范圍
// [_mapView setRegion:region animated:YES];
//設(shè)置約束
_mapView.frame=CGRectMake(0, 0, W, H);
//切換為普通地圖樣式
// _mapView.zoomLevel=11.0;
_mapView.showMapScaleBar=YES;
// _mapView.userTrackingMode = BMKUserTrackingModeFollow;
_mapView.userTrackingMode=BMKUserTrackingModeFollow;
[_mapView setMapType:BMKMapTypeStandard];
_mapView.delegate=self;
//顯示定位圖層
_mapView.showsUserLocation = YES;
//打開實(shí)時路況圖層
[_mapView setTrafficEnabled:YES];
///設(shè)定地圖是否現(xiàn)顯示3D樓塊效果
_mapView.buildingsEnabled=YES;
//6. 設(shè)置地圖顯示的層級
[_mapView setZoomLevel:12];
//添加
// self.view =_mapView;
[self.view addSubview:_mapView];
//設(shè)置線路位置經(jīng)緯度
CLLocationCoordinate2D coords[5] = {0};
// 39.864523,long 116.451585
coords[0].latitude = 39.864523;
coords[0].longitude = 116.451585;
coords[1].latitude = 39.925;
coords[1].longitude = 116.454;
coords[2].latitude = 39.955;
coords[2].longitude = 116.494;
coords[3].latitude = 39.905;
coords[3].longitude = 116.554;
coords[4].latitude = 39.965;
coords[4].longitude = 116.604;
//構(gòu)建分段顏色索引數(shù)組
NSArray *colorIndexs = [NSArray arrayWithObjects:
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2], nil];
//構(gòu)建BMKPolyline,使用分段顏色索引,其對應(yīng)的BMKPolylineView必須設(shè)置colors屬性
BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:colorIndexs];
[_mapView addOverlay:colorfulPolyline];
//初始化BMKLocationService
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
_locationManager = [[CLLocationManager alloc] init];
//獲取授權(quán)驗(yàn)證
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
}
_locService = [[BMKLocationService alloc] init];
_locService.delegate = self;
[_locService startUserLocationService];
_mapView.showsUserLocation = NO;
_mapView.userTrackingMode = BMKUserTrackingModeFollow;
_mapView.showsUserLocation = YES;
// _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
//添加大頭針經(jīng)緯度
CLLocationCoordinate2D coordss[5] = {0};
//循環(huán)添加大頭針 // 39.864523,long 116.451585
coordss[0].latitude = 39.864523;
coordss[0].longitude = 116.451585;
coordss[1].latitude = 39.925;
coordss[1].longitude = 116.454;
coordss[2].latitude = 39.955;
coordss[2].longitude = 116.494;
coordss[3].latitude = 39.905;
coordss[3].longitude = 116.554;
coordss[4].latitude = 39.965;
coordss[4].longitude = 116.604;
for (int i=0; i<5; i++) {
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
annotation.coordinate = coordss[i];
annotation.title = @"這里是北京";
[_mapView addAnnotation:annotation];
}
//定位
_locService = [[BMKLocationService alloc]init];
//設(shè)置代理
_locService.delegate = self;
//啟動LocationService
[_locService startUserLocationService];
}
//自定義大頭針
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{ BMKAnnotationView *annotationView=[[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
annotationView.image =[UIImage imageNamed:@"canyin"];
//自定義內(nèi)容氣泡
UIView *areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
areaPaoView.layer.cornerRadius=8;
areaPaoView.layer.masksToBounds=YES;
areaPaoView.layer.contents =(id)[UIImage imageNamed:@" "].CGImage;//這張圖片是做好的透明
areaPaoView.backgroundColor=[UIColor whiteColor];
// if ([annotation.title isEqualToString:@"1"]) { //假設(shè)title的標(biāo)題為1告材,那么就把添加上這個自定義氣泡內(nèi)容
UILabel * labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
labelNo.text =[NSString stringWithFormat:@"站點(diǎn)編號:%@",@"faf"];
labelNo.textColor = [UIColor blackColor];
labelNo.backgroundColor = [UIColor clearColor];
[areaPaoView addSubview:labelNo];
UILabel * labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)];
labelStationName.text = [NSString stringWithFormat:@"站點(diǎn)名稱:昆山中學(xué)"];
labelStationName.textColor = [UIColor blackColor];
labelStationName.backgroundColor = [UIColor clearColor];
[areaPaoView addSubview:labelStationName];
UILabel * labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)];
labelSumNum.text = [NSString stringWithFormat:@"總樁數(shù):30"];
labelSumNum.textColor = [UIColor blackColor];
labelSumNum.backgroundColor = [UIColor clearColor];
[areaPaoView addSubview:labelSumNum];
UILabel * labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
labelBicycleNum.text = [NSString stringWithFormat:@"可借車:20"];
labelBicycleNum.textColor = [UIColor blackColor];
labelBicycleNum.backgroundColor = [UIColor clearColor];
[areaPaoView addSubview:labelBicycleNum];
// }
BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:areaPaoView];
annotationView.paopaoView=paopao;
return annotationView;
}
//根據(jù)overlay生成對應(yīng)的View
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
BMKPolylineView * polyLineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polyLineView.lineWidth = 5;
/// 使用分段顏色繪制時,必須設(shè)置(內(nèi)容必須為UIColor)
polyLineView.colors = [NSArray arrayWithObjects:[UIColor colorWithRed:0.42 green:0.42 blue:0.42 alpha:1.00], nil];
if (dataArr.count>2) {
polyLineView.strokeColor = [ [UIColor colorWithRed:0.32 green:0.69 blue:0.75 alpha:1.00] colorWithAlphaComponent:1];
polyLineView.lineWidth = 5.0;
}
return polyLineView;
return nil;
}
//處理位置坐標(biāo)更新
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
}
//處理位置坐標(biāo)更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
//獲取中心位置古劲,設(shè)置居中
CLLocationCoordinate2D coordss;
coordss.latitude = 39.905;
coordss.longitude = 116.554;
// _mapView.centerCoordinate = coordss;//移動到中心點(diǎn)
CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// _mapView.centerCoordinate = pt;//移動到中心點(diǎn)
CoorModel *model = [[CoorModel alloc] init];
model.latitude = pt.latitude;
model.longitude = pt.longitude;
[dataArr addObject:model];
[self makeline];
}
//地圖劃線的方法
-(void)makeline{
if ( dataArr.count < 2 ) {
return;
}
BMKMapPoint *pointarr = new BMKMapPoint[dataArr.count];
for (int i = 0; i < dataArr.count; i++) {
CoorModel *model = dataArr[i];
CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(model.latitude, model.longitude);
BMKMapPoint point = BMKMapPointForCoordinate(pt);
pointarr[i] = point;
}
BMKPolyline *polyline;
if (polyline) {
[_mapView removeOverlay:polyline];
}
polyline = [BMKPolyline polylineWithPoints:pointarr count:dataArr.count];
if (nil != polyline) {
[_mapView addOverlay:polyline];
}
delete [] pointarr;
}
@end
個人總結(jié) 依據(jù)百度地圖官方文檔所做斥赋。