Location, Destination Display, Path Planning, Navigation

Posted by consolestrat on Fri, 05 Jul 2019 00:27:13 +0200

1. First, how to access Baidu Map

1) Open Baidu Map and Open Platform Website first. Find the relevant download module and download the SDK packages as required. I chose "Download All" here.

2) Next, it is necessary to apply for the key on the website, and the application procedure can see the official documents. Application key

3) Access Baidu Map in the project. Statement of steps: Baidu Map Access

Use Cocoa Pods. In the folder where the current project file (. xcodeproj) is located, open terminal and create Podfile touch Podfile Edit Podfile as follows: Pod'Baidu Map Kit'#Baidu Map SDK Enter the command in the folder where Podfile is located: pod install (this may be slower, please wait patiently...)

Manual configuration. framework form development kit Introduce the required packages under "New Group" in the 3D part directory of the project Click the "+" button in TARGETS - > Build Phases - > Link Binary With Libaries, click the "Add Other" button in the pop-up window, and select Baidu Map API **. framework to add to the project. Note: Static libraries are developed in the form of objective-c++, so there should be at least one. mm file in the project, or modify Project-> Edit Active Target-> Build Setting to find Compile Sources As and set it to "Objective-C++". Introduce the required system library. CoreLocation.framework and Quartz Core. framework, OpenGLES.framework, System Configuration. framework, CoreGraphics.framework, Security.framework, libsqlite3.0.tbd (libsqlite3.0.dylib before Xcode 7), CoreTelephony.framework, libstdc+.6.0.9.tbd (libstdc+.6.0.9.dylib before Xcode 7). In Xcode Projects - > Active Target - > Build Phases - > Link Binary With Libraries, add these system libraries. If the project needs the basic map function, the mapapi.bundle resource file needs to be introduced.

4) Add in info.plist <key>LSApplicationQueriesSchemes</key> <array> <string>baidumap</string> <string>iosamap</string> </array>

2. Add maps to the required controllers to display the user's current location, display the target address (marked with a pin), path planning and navigation (Baidu Map, Gaode Map, Systematic Map). See Code Notes for details.


//
// HotelLocationMapVC.m
// Where to live
//
// Created by geek on 2016/12/25.
// Copyright 2016 geek. All rights reserved.
//

#import "HotelLocationMapVC.h"
#Import & lt; Baidu Map API_Map/BMKMapComponent.h> //Baidu Map Basic Header File
#Import & lt; Baidu Map API_Location/BMKLocation Service.h> //Baidu Map Location Header File
#Import & lt; Baidu Map API_Search/BMKPoiSearch.h> //Baidu Map Search Header File
#Import & lt; MapKit / MapKit. h> // Open the header file needed for the system map

@interface HotelLocationMapVC ()&lt;BMKMapViewDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate&gt;
@property (nonatomic, strong) UIButton *myLodactionButton; //My Location Button
@property (nonatomic, strong) UIButton *hotelLocationButton; //Hotel Location Button
@property (nonatomic, strong) UIButton *navigationButton; //Start Navigation Button
@property (nonatomic, strong) BMKMapView *mapView; //Map Basics
@property (nonatomic, strong) BMKLocationService *locService; //Location Service
@property (nonatomic, strong) BMKPoiSearch *poiSearch; //Search Service
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIAlertController *alertController;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate; // Coordinates to navigate
@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 = @"Hotel location";
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(@"Start Navigation");
[self presentViewController:self.alertController animated:YES completion:nil];
}

-(void)myPosition{
NSLog(@"My position");
}

-(void)hotelPosition{
NSLog(@"Hotel location");
}

#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;
//Peripheral Cloud Retrieval Parameter Information Class
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(@"Successful search");
[self.locService stopUserLocationService];
}else{
NSLog(@"search failed");
}
}

#pragma mark - BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode{
if (errorCode == BMK_SEARCH_NO_ERROR) {
//Show only the first search point to the interface
BMKPoiInfo *info = poiResult.poiInfoList.firstObject;
//Comments to initialize a point
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];
/*
*Display all the search results
for (BMKPoiInfo *info in poiResult.poiInfoList) {
[self.dataArray addObject:info];
//Comments to initialize a point
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(@"details%@",poiDetailResult.name);
}

#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id &lt;BMKAnnotation&gt;)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 Details Retrieval Information Class
BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init];
BMKPoiInfo *info = self.dataArray.firstObject;
//uid of poi, retrieved from the BMKPoiResult structure returned from POI retrieval
option.poiUid = info.uid;
BOOL flag = [self.poiSearch poiDetailSearch:option];
if (flag) {
NSLog(@"Successful search");
}
else {
NSLog(@"Retrieval failure");
}
}

#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:@"Start Navigation" 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:@"My position" 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:@"Hotel location" 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:@"Please select the map" message:nil
preferredStyle:UIAlertControllerStyleActionSheet ];
UIAlertAction *appleAction = [UIAlertAction actionWithTitle:@"Apple's own map" 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:@"Golden Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://Navi? SourceApplication=%@& backScheme=%@& poiname=%@& lat=%f& lon=%f& dev=0& style=2",@"Where do you live",@"YGche", @ self.destination, self.coordinate.latitude, self.coordinate.longitude] string ByAdding Percentage Escaping: Encoding: Encoding F8 String];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showInfoWithStatus:@"Golden Map is not installed on your mobile phone"];
}
}];
UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"Baidu Maps" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin = {my location} & amp; destination = latlng:% f,% f | name:%@ & amp; mode = driving & amp; coord_type = gcj02 ", self. coordinate. latitude, self. coordinate. longitude, self. destination] string ByAdding Percents UsingEncoding: NSUTF8 String Encoding];
if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showInfoWithStatus:@"Baidu Map is not installed on your mobile phone"];
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Sign out" style:UIAlertActionStyleCancel handler:nil];
[_alertController addAction:appleAction];
[_alertController addAction:tecentAction];
[_alertController addAction:baiduAction];
[_alertController addAction:cancelAction];
}
return _alertController;
}

@end

If you want to see the full Demo, portal: Where does App live?

Topics: encoding xcode SDK Mobile