定位、目的地顯示、路徑規劃、導航

一、首先說說如何接入百度地圖php

1)首先打開百度地圖開開放平臺網站,。找到相關下載模塊,根據需求下載所需的SDK包。我這裏選擇的是「所有下載」ios

2)其次須要在網站上申請密鑰,申請步驟可查看官方文檔。 申請密鑰c++

3)在工程中接入百度地圖。步驟說明:百度地圖接入git

使用CocoaPods。 在當前工程文件(.xcodeproj)所在文件夾下,打開terminal,建立Podfile touch Podfile 編輯Podfile內容以下: pod 'BaiduMapKit' #百度地圖SDK 在Podfile所在的文件夾下輸入命令: pod install (這個可能比較慢,請耐心等待……)github

手動配置.framework形式開發包 在工程的3dparty目錄下「New Group」,將所需的包引入 在 TARGETS->Build Phases-> Link Binary With Libaries中點擊「+」按鈕,在彈出的窗口中點擊「Add Other」按鈕,選擇BaiduMapAPI_**.framework添加到工程中。 注意:靜態庫採用objective-c++的形式開發,因此工程中至少要有一個.mm文件,或者修改Project -> Edit Active Target -> Build Setting 中找到 Compile Sources As,並將其設置爲"Objective-C++" 引入所需的系統庫。CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework、libsqlite3.0.tbd(xcode7之前爲 libsqlite3.0.dylib)、CoreTelephony.framework 、libstdc++.6.0.9.tbd(xcode7之前爲libstdc++.6.0.9.dylib)。在Xcode的Project -> Active Target ->Build Phases ->Link Binary With Libraries,添加這幾個系統庫便可。 若是項目須要基礎地圖功能則需引入mapapi.bundle資源文件。objective-c

4)在info.plist中加入 <key>LSApplicationQueriesSchemes</key> <array> <string>baidumap</string> <string>iosamap</string> </array>sql

二、在所須要的控制器裏面加入地圖顯示用戶當前定位、顯示目標地址(用大頭針標示出來)、路徑規劃導航(百度地圖、高德地圖、系統地圖),詳細說明見代碼註釋api

//
// HotelLocationMapVC.m
// 住哪兒
//
// Created by geek on 2016/12/25.
// Copyright © 2016年 geek. All rights reserved.
//

#import "HotelLocationMapVC.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h> //百度地圖基本頭文件
#import <BaiduMapAPI_Location/BMKLocationService.h> //百度地圖定位頭文件
#import <BaiduMapAPI_Search/BMKPoiSearch.h> //百度地圖搜索頭文件
#import <MapKit/MapKit.h> //打開系統地圖所需的頭文件

@interface HotelLocationMapVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate>
@property (nonatomic, strong) UIButton *myLodactionButton; //個人位置按鈕
@property (nonatomic, strong) UIButton *hotelLocationButton; //酒店位置按鈕
@property (nonatomic, strong) UIButton *navigationButton; //開始導航按鈕
@property (nonatomic, strong) BMKMapView *mapView; //地圖基本
@property (nonatomic, strong) BMKLocationService *locService; //定位服務
@property (nonatomic, strong) BMKPoiSearch *poiSearch; //搜索服務
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIAlertController *alertController;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate; // 要導航的座標
@end

@implementation HotelLocationMapVC

#pragma mark - lice cyele
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[SVProgressHUD dismiss];
self.mapView.delegate = nil;
}

#pragma mark - private method
-(void)setupUI{
self.title = @"酒店位置";
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.mapView];
[self.locService startUserLocationService];
[self.view addSubview:self.navigationButton];
[self.view addSubview:self.myLodactionButton];
[self.view addSubview:self.hotelLocationButton];
}

-(void)startNavigation{
NSLog(@"開始導航");
[self presentViewController:self.alertController animated:YES completion:nil];
}

-(void)myPosition{
NSLog(@"個人位置");
}

-(void)hotelPosition{
NSLog(@"酒店位置");
}

#pragma mark - BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
self.mapView.showsUserLocation = YES;
[self.mapView updateLocationData:userLocation];
self.mapView.centerCoordinate = userLocation.location.coordinate;
self.mapView.zoomLevel = 18;
//周邊雲檢索參數信息類
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init];;
option.pageIndex = 0;
option.pageCapacity = 50;
option.location = userLocation.location.coordinate;
option.keyword = self.destination;
BOOL flag = [self.poiSearch poiSearchNearBy:option];
if (flag) {
NSLog(@"搜索成功");
[self.locService stopUserLocationService];
}else{
NSLog(@"搜索失敗");
}
}

#pragma mark - BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode{
if (errorCode == BMK_SEARCH_NO_ERROR) {
//只將搜索到的第一個點顯示到界面上
BMKPoiInfo *info = poiResult.poiInfoList.firstObject;
//初始化一個點的註釋
BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init];
self.coordinate = CLLocationCoordinate2DMake(info.pt.latitude, info.pt.longitude);
annotoation.coordinate = info.pt;
annotoation.title = info.name;
annotoation.subtitle = self.destination;
annotoation.subtitle = info.address;
[self.mapView addAnnotation:annotoation];
[self.mapView selectAnnotation:annotoation animated:YES];
/*
*將搜索到的全部結果顯示出來
for (BMKPoiInfo *info in poiResult.poiInfoList) {
[self.dataArray addObject:info];
//初始化一個點的註釋
BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init];
annotoation.coordinate = info.pt;
annotoation.title = info.name;
annotoation.subtitle = self.destination;
annotoation.subtitle = info.address;
[self.mapView addAnnotation:annotoation];
[self.mapView selectAnnotation:annotoation animated:YES];
}
*/
}
}

- (void)onGetPoiDetailResult:(BMKPoiSearch*)searcher result:(BMKPoiDetailResult*)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode{
NSLog(@"詳情%@",poiDetailResult.name);
}

#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"];
newAnnotation.pinColor = BMKPinAnnotationColorRed;
newAnnotation.animatesDrop = YES;
return newAnnotation;
}
return nil;
}

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view {
//poi詳情檢索信息類
BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init];
BMKPoiInfo *info = self.dataArray.firstObject;
//poi的uid,從poi檢索返回的BMKPoiResult結構中獲取
option.poiUid = info.uid;
BOOL flag = [self.poiSearch poiDetailSearch:option];
if (flag) {
NSLog(@"檢索成功");
}
else {
NSLog(@"檢索失敗");
}
}

#pragma mark - lazy load
-(BMKMapView *)mapView{
if (!_mapView) {
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, BoundWidth, BoundHeight)];
_mapView.showsUserLocation = YES;
_mapView.showIndoorMapPoi = YES;
_mapView.zoomLevel = 21;
_mapView.rotateEnabled = NO;
_mapView.delegate = self;
}
return _mapView;
}

-(BMKLocationService *)locService{
if (!_locService) {
_locService = [[BMKLocationService alloc] init];
_locService.delegate = self;
}
return _locService;
}

-(BMKPoiSearch *)poiSearch{
if (!_poiSearch) {
_poiSearch = [[BMKPoiSearch alloc] init];
_poiSearch.delegate = self;
}
return _poiSearch;
}

- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}

-(UIButton *)navigationButton{
if (!_navigationButton) {
_navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
_navigationButton.frame = CGRectMake(BoundWidth-120, BoundHeight-64-140, 100, 30);
[_navigationButton setTitle:@"開始導航" forState:UIControlStateNormal];
;_navigationButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_navigationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_navigationButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
_navigationButton.layer.cornerRadius = 5;
[_navigationButton clipsToBounds];
[_navigationButton addTarget:self action:@selector(startNavigation) forControlEvents:UIControlEventTouchUpInside];
}
return _navigationButton;
}

-(UIButton *)myLodactionButton{
if (!_myLodactionButton) {
_myLodactionButton = [UIButton buttonWithType:UIButtonTypeCustom];
_myLodactionButton.frame = CGRectMake(BoundWidth-120, BoundHeight-64-100, 100, 30);
[_myLodactionButton setTitle:@"個人位置" forState:UIControlStateNormal];
_myLodactionButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_myLodactionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_myLodactionButton.backgroundColor = [UIColor whiteColor];
_myLodactionButton.layer.cornerRadius = 5;
[_myLodactionButton clipsToBounds];
[_myLodactionButton addTarget:self action:@selector(myPosition) forControlEvents:UIControlEventTouchUpInside];
}
return _myLodactionButton;
}

-(UIButton *)hotelLocationButton{
if (!_hotelLocationButton) {
_hotelLocationButton = [UIButton buttonWithType:UIButtonTypeCustom];
_hotelLocationButton.frame = CGRectMake(BoundWidth-120, BoundHeight-64-60, 100, 30);
[_hotelLocationButton setTitle:@"酒店位置" forState:UIControlStateNormal];
_hotelLocationButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_hotelLocationButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_hotelLocationButton.backgroundColor = [UIColor whiteColor];
_hotelLocationButton.layer.cornerRadius = 5;
[_hotelLocationButton clipsToBounds];
[_hotelLocationButton addTarget:self action:@selector(hotelPosition) forControlEvents:UIControlEventTouchUpInside];
}
return _hotelLocationButton;
}

-(UIAlertController *)alertController{
if (!_alertController) {
_alertController = [UIAlertController alertControllerWithTitle:@"請選擇地圖" message:nil
preferredStyle:UIAlertControllerStyleActionSheet ];
UIAlertAction *appleAction = [UIAlertAction actionWithTitle:@"蘋果自帶地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]];
toLocation.name = self.destination;
[MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}];
UIAlertAction *tecentAction = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=0&style=2",@"住哪兒",@"YGche",self.destination,self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showInfoWithStatus:@"您的手機未安裝高德地圖"];
}
}];
UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{個人位置}}&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude,self.destination] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showInfoWithStatus:@"您的手機未安裝百度地圖"];
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleCancel handler:nil];
[_alertController addAction:appleAction];
[_alertController addAction:tecentAction];
[_alertController addAction:baiduAction];
[_alertController addAction:cancelAction];
}
return _alertController;
}

@end

若是要看完整Demo的,傳送門:住哪兒Appxcode

相關文章
相關標籤/搜索