本文介绍: 文章目录前言需要用到的库显示基础地图地图定位位置搜索路线规划前言关于如何调用高德地图的SDK,项目前需要下载哪些库,还有正式开始写项目前的一些注意事项可见这篇博客【iOS】调用百度、高德地图SDK需要用到的库PodFile中写入以下:platform :ios, ‘7.0’target ‘你的工程名字‘ dopod ‘AMap3DMap‘ pod ‘AMapLocation‘pod ‘AMapSearch‘endAMap3DMap是用来显示地图等有关操作的库AMapLocatio
前言
关于如何调用高德地图的SDK,项目前需要下载哪些库,还有正式开始写项目前的一些注意事项可见这篇博客【iOS】调用百度、高德地图SDK
需要用到的库
platform :ios, '7.0'
target '你的工程名字' do
pod 'AMap3DMap'
pod 'AMapLocation'
pod 'AMapSearch'
end
显示基础地图
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AMapServices sharedServices].apiKey = @"你的KEY值";
return YES;
}
@property (strong, nonatomic) MAMapView* mapView;
//把地图添加至view
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height * 0.12, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.88)];
_mapView.showsIndoorMap = YES; //设置显示室内地图
_mapView.zoomLevel = 18; //设置缩放比例
_mapView.zoomEnabled = YES; //NO表示禁用缩放手势,YES表示开启
_mapView.rotateEnabled = NO; //NO表示禁用旋转手势,YES表示开启
_mapView.delegate = self; //设置代理
[self.view addSubview:_mapView];
开启定位
//是否显示用户的位置
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;
地图定位
@property (strong, nonatomic) AMapLocationManager* locationManager;
//初始化定位
- (void)initLocation {
//持续定位
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 10;
[self.locationManager setLocatingWithReGeocode:YES];
//开启持续定位
[self.locationManager startUpdatingLocation];
}
//在回调函数中,获取定位坐标,进行业务处理。
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
NSLog(@"location:{纬度:%f; 经度:%f;}", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
}
位置搜索
- 当我在输入框里每打出一个字,相关位置信息都会实时更改,并由
tableView
展示出来 - 我新创建了一个视图控制器,专门用来呈现搜索的页面
- 这里用到了新接触的一个控件
UISearchController
,专门用于搜索的一个控件 - 在新视图提前声明头文件
#import <AMapSearchKit/AMapSearchKit.h>
,在.h
中用到了如下属性
#import "ViewController.h"
#import <AMapSearchKit/AMapSearchKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SearchViewController : UIViewController <UITextFieldDelegate, UISearchBarDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource, AMapSearchDelegate>
@property (strong, nonatomic) NSString* searchString;
@property (strong, nonatomic) UISearchController* searchController;
@property (strong, nonatomic) UITableView* searchTableView;
@property (strong, nonatomic) NSMutableArray* listArray;
@property (strong, nonatomic) NSMutableArray* listNameArray;
@property (strong, nonatomic) AMapSearchAPI* searchAPI;
@end
NS_ASSUME_NONNULL_END
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchTableView.tableHeaderView = _searchController.searchBar; //这句代码意义是将searchController添加在视图上
_searchController.searchResultsUpdater = self;
_searchController.searchBar.delegate = self;
_searchController.searchBar.placeholder = @"搜索";
_searchController.searchBar.barTintColor = [UIColor colorWithWhite:0.93 alpha:1];
//搜索框激活时,使用提示搜索
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//发起输入提示搜索
AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
//关键字
tipsRequest.keywords = _searchController.searchBar.text;
//城市
tipsRequest.city = @"西安";
//执行搜索
[_searchAPI AMapInputTipsSearch: tipsRequest];
}
//实现输入提示的回调函数
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response {
if(response.tips.count == 0) {
return;
}
//通过AMapInputTipsSearchResponse对象处理搜索结果
//先清空数组
[_listArray removeAllObjects];
[_listNameArray removeAllObjects];
for (AMapTip *obj in response.tips) {
//把搜索结果存在数组
[_listArray addObject:obj];
[_listNameArray addObject:obj.name];
}
//_isSelected = NO;
//刷新表视图
[_searchTableView reloadData];
}
路线规划
//路径规划
- (void)pathPlan {
AMapWalkingRouteSearchRequest *navi = [[AMapWalkingRouteSearchRequest alloc] init];
/* 出发点. */
navi.origin = [AMapGeoPoint locationWithLatitude:_mapView.userLocation.coordinate.latitude longitude:_mapView.userLocation.coordinate.longitude];
/* 目的地. */
navi.destination = [AMapGeoPoint locationWithLatitude:_tipTemp.location.latitude longitude:_tipTemp.location.longitude];
//发起路线规划
[_searchAPI AMapWalkingRouteSearch:navi];
}
//实现路径搜索的回调函数
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response {
if (response.route == nil) {
return;
}
//通过AMapNavigationSearchResponse对象处理搜索结果
NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];
NSLog(@"%@", route);
AMapPath *path = response.route.paths[0]; //选择一条路径
AMapStep *step = path.steps[0]; //这个路径上的导航路段数组
NSLog(@"%@",step.polyline); //此路段坐标点字符串
NSLog(@"%@",response.route.paths[0]);
if (response.count > 0) {
//移除地图原本的遮盖
[_mapView removeOverlays:_pathPolylines];
_pathPolylines = nil;
// 只显示第⼀条 规划的路径
_pathPolylines = [self polylinesForPath:response.route.paths[0]];
NSLog(@"%@",response.route.paths[0]);
//添加新的遮盖,然后会触发代理方法进行绘制
[_mapView addOverlays:_pathPolylines];
}
}
- 绘制遮盖时执行的代理方法
//绘制遮盖时执行的代理方法
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
/* 自定义定位精度对应的MACircleView. */
//画路线
if ([overlay isKindOfClass:[MAPolyline class]]) {
//初始化一个路线类型的view
MAPolylineRenderer *polygonView = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
//设置线宽颜色等
polygonView.lineWidth = 8.f;
polygonView.strokeColor = [UIColor colorWithRed:0.015 green:0.658 blue:0.986 alpha:1.000];
polygonView.fillColor = [UIColor colorWithRed:0.940 green:0.771 blue:0.143 alpha:0.800];
polygonView.lineJoinType = kMALineJoinRound;//连接类型
//返回view,就进行了添加
return polygonView;
}
return nil;
}
//路线解析
- (NSArray *)polylinesForPath:(AMapPath *)path {
if (path == nil || path.steps.count == 0) {
return nil;
}
NSMutableArray *polylines = [NSMutableArray array];
[path.steps enumerateObjectsUsingBlock:^(AMapStep *step, NSUInteger idx, BOOL *stop) {
NSUInteger count = 0;
CLLocationCoordinate2D *coordinates = [self coordinatesForString:step.polyline
coordinateCount:&count
parseToken:@";"];
MAPolyline *polyline = [MAPolyline polylineWithCoordinates:coordinates count:count];
[polylines addObject:polyline];
(void)((free(coordinates))), coordinates = NULL;
}];
return polylines;
}
- 解析经纬度
- (CLLocationCoordinate2D *)coordinatesForString:(NSString *)string
coordinateCount:(NSUInteger *)coordinateCount
parseToken:(NSString *)token {
if (string == nil) {
return NULL;
}
if (token == nil) {
token = @",";
}
NSString *str = @"";
if (![token isEqualToString:@","]) {
str = [string stringByReplacingOccurrencesOfString:token withString:@","];
}
else {
str = [NSString stringWithString:string];
}
NSArray *components = [str componentsSeparatedByString:@","];
NSUInteger count = [components count] / 2;
if (coordinateCount != NULL) {
*coordinateCount = count;
}
CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D*)malloc(count * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < count; i++) {
coordinates[i].longitude = [[components objectAtIndex:2 * i] doubleValue];
coordinates[i].latitude = [[components objectAtIndex:2 * i + 1] doubleValue];
}
return coordinates;
}
原文地址:https://blog.csdn.net/kochunk1t/article/details/121771507
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_46998.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。