iOS高德地图使用-搜索,路径规划

项目中想加入地图功能,使用高德地图第三方,想要实现确定一个位置,搜索路线并且显示的方法。耗了一番功夫,总算实现了。

效果

WeChat_1462507820.jpeg

一、配置工作

1.申请key

访问 http://lbs.amap.com/dev/key/ 在高度地图第三方开发平台申请一个key,注册账户,新建应用,这个没什么门槛。
得到这个key

屏幕快照 2016-05-06 上午10.34.15.png

提示一下,这个key对应的bundle ID 要和工程里面的bundle ID 相同,不然每次打开地图都会报一个Invalid_user_scode的提示。

2.导入第三方

方便起见 pod导入

pod ‘AMap3DMap‘ #3D地图SDK
#pod ‘AMap2DMap‘ #2D地图SDK(2D地图和3D地图不能同时使用,2选1)
pod ‘AMapSearch‘ #搜索服务SDK
3.打开工程的后台定位功能 更改info.plist

增加两条,一条请求位置时提示,一条https

屏幕快照 2016-05-06 上午11.02.06.png

屏幕快照 2016-05-06 上午10.48.19.png

二、地图基本显示

把地图添加到view即可显示

- (void)viewDidLoad {
    [super viewDidLoad];

    //配置用户Key
    [MAMapServices sharedServices].apiKey = @"76bb9bc3718375ad03acba7c333694c4";

    //把地图放在底层
    [self.view insertSubview:self.mapView atIndex:0];

}
//地图懒加载
- (MAMapView *)mapView
{
    if (!_mapView) {
        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        _mapView.delegate = self;
        _mapView.showsUserLocation = YES;    //YES 为打开定位,NO为关闭定位

        [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO]; //地图跟着位置移动

        //自定义定位经度圈样式
        _mapView.customizeUserLocationAccuracyCircleRepresentation = NO;
        //地图跟踪模式
        _mapView.userTrackingMode = MAUserTrackingModeFollow;

        //后台定位
        _mapView.pausesLocationUpdatesAutomatically = NO;

        _mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系统必须配置

    }
    return _mapView;
}

三、地图搜索功能

遵守协议 AMapSearchDelegate

高德提供了多种搜索方式,POI搜索(关键字查询、周边搜索、多边形查询),检索现实中真实存在的地物。

提示搜索,就是在还没有输入完全时,根据已有字符进行的搜索

//搜索框激活时,使用提示搜索
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    //发起输入提示搜索
    AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
   //关键字
    tipsRequest.keywords = _searchController.searchBar.text;
   //城市
    tipsRequest.city = _currentCity;

    //执行搜索
    [_search AMapInputTipsSearch: tipsRequest];

}

//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
    if(response.tips.count == 0)
    {
        return;
    }
    //通过AMapInputTipsSearchResponse对象处理搜索结果
    //先清空数组
    [self.searchList removeAllObjects];
    for (AMapTip *p in response.tips) {
        //把搜索结果存在数组
        [self.searchList addObject:p];
    }
    _isSelected = NO;
    //刷新表视图
    [self.tableView reloadData];
}

点击进行poi搜索

//周边搜索
- (IBAction)searchAction:(id)sender {
    //初始化检索对象
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;

    //构造AMapPOIAroundSearchRequest对象,设置周边请求参数
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];

    //当前位置
    request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];

    //关键字
    request.keywords = _searchController.searchBar.text;
    NSLog(@"%@",_searchController.searchBar.text);
    // types属性表示限定搜索POI的类别,默认为:餐饮服务|商务住宅|生活服务
    // POI的类型共分为20种大类别,分别为:
    // 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
    // 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
    // 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
    //    request.types = @"餐饮服务|生活服务";
    request.radius =  5000;//<! 查询半径,范围:0-50000,单位:米 [default = 3000]
    request.sortrule = 0;
    request.requireExtension = YES;

    //发起周边搜索
    [_search AMapPOIAroundSearch:request];
}

//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if(response.pois.count == 0)
    {
        return;
    }

    //通过 AMapPOISearchResponse 对象处理搜索结果

    [self.dataList removeAllObjects];
    for (AMapPOI *p in response.pois) {
        NSLog(@"%@",[NSString stringWithFormat:@"%@\nPOI: %@,%@", p.description,p.name,p.address]);

        //搜索结果存在数组
        [self.dataList addObject:p];
    }

    _isSelected = YES;
    [self.tableView reloadData];

}

四、路径规划

规划路径查询(驾车路线搜索、公交换成方案查询、步行路径检索),提前知道出行路线

//规划线路查询
- (IBAction)findWayAction:(id)sender {
    //构造AMapDrivingRouteSearchRequest对象,设置驾车路径规划请求参数
    AMapWalkingRouteSearchRequest *request = [[AMapWalkingRouteSearchRequest alloc] init];
    //设置起点,我选择了当前位置,mapView有这个属性
    request.origin = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
    //设置终点,可以选择手点
    request.destination = [AMapGeoPoint locationWithLatitude:_destinationPoint.coordinate.latitude longitude:_destinationPoint.coordinate.longitude];

//    request.strategy = 2;//距离优先
//    request.requireExtension = YES;

    //发起路径搜索,发起后会执行代理方法
    //这里使用的是步行路径
    [_search AMapWalkingRouteSearch: request];
}

//长按手势响应方法,选择路径规划的终点,手势自己加
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
       //在地图上长按的位置
        CGPoint p = [gesture locationInView:_mapView];
        NSLog(@"press on (%f, %f)", p.x, p.y);
    }
     //转换成经纬度
    CLLocationCoordinate2D coordinate = [_mapView convertPoint:[gesture locationInView:_mapView] toCoordinateFromView:_mapView];

   //赋值给目标点
    _destinationPoint = [[MAPointAnnotation alloc] init];
    _destinationPoint.coordinate = coordinate;
}

执行路径搜索后会执行代理方法

//实现路径搜索的回调函数
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    if(response.route == nil)
    {
        return;
    }

    //通过AMapNavigationSearchResponse对象处理搜索结果
    NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];

    AMapPath *path = response.route.paths[0]; //选择一条路径
    AMapStep *step = path.steps[0]; //这个路径上的导航路段数组
    NSLog(@"%@",step.polyline);   //此路段坐标点字符串

    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;

}
时间: 2024-08-26 22:02:49

iOS高德地图使用-搜索,路径规划的相关文章

高德地图便民搜索_源码下载

高德地图便民搜索 支持平台:Android   运行环境:Eclipse   开发语言:Java 下载地址:http://t.cn/R7YjOXT 源码简介 通过调用高德地图api,根据输入地名,搜索具体位置的功能. 源码运行截图  

高德地图关键字搜索

EditText startTextView=(EditText) findViewById(R.id.car_search_et); startTextView.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int count, int after) { String newText = s.toString().trim();

iOS 高德地图API不能定位及INVALID_USER_SCODE

iOS 高德地图API不能定位及INVALID_USER_SCODE问题,有需要的朋友可以参考下. 一.在使用高德地图的API的时候,没有办法实现定位,在这里说一下在真机测试的时候出现没法定位应该注意的几点问题和解决方法. 1.将mapView添加到self.view上,[self.view addSubView:self.mapView]; 2.在plist文件中添加一个属性NSLocationAlwaysUsageDescription. 3.设置mapView的一个属性,self.mapV

IOS高德地图开发

博客链接:www.goofyy.com/blog 或者百度搜索 goofyy 玩了苹果原生地图,觉得IOS8的原生还是差了那么一点点,对比了一下腾讯的SDK和高德的SDK,还是觉得高德更碉些,第三方地图就先拿高德地图开刀了. 使用高德SDK,首先到高德官网注册一个开发者账号,获取开发者KEY.这些高德LBS开放平台都是有详细教程.小编编就不在这里赘余啦.首先是导入库和开发前简单设置. 高德官网下载高德开发的SDK导入.具体导入的库如下 1.引入地图库&搜索库 左侧目录中选中工程名,在 TARGE

高德地图兴趣点搜索(PoiSearch)小例子

目前我们项目上在做一个兴趣点搜索的小功能(搜索附近的电影院),用的是高德地图,为了便于记忆,就写下来.功能及页面都很简单,就是在输入框中输入内容,然后就会搜索出附近相关的位置,然后在ListView中展示出来.项目中使用的是分页加载,为了写文章方便,就把加载去掉了,直接用ListView展示出来. ----------------------------------界面布局--------------------------------------- <?xml version="1.0&

android 和 ios 读取lua 的搜索路径(只是拷贝他人的,没有测试过)

在lua语言中,require语句搜寻模块有一个内置的顺序,并且可以通过package.path来维护模块的搜索策略.但是在cocos2d-x中,不是这样! cocos2d-x重载了原本的lua的require加载方式.(见Cocos2dxLuaLoader.cpp )Cocos2dxLuaLoader逻辑的生效是在package.path之前,并且package.path在安卓上则不能很好的处理加载pkg包内部文件的问题.所以在实际使用中,我们只使用cocos2d-x重载的方法就可以了. 怎么

iOS 高德地图API不能定位及INVALID_USER_SCODE问题

一.在使用高德地图的API的时候,没有办法实现定位,在这里说一下在真机测试的时候出现没法定位应该注意的几点问题和解决方法. 1.将mapView添加到self.view上,[self.view addSubView:self.mapView]; 2.在plist文件中添加一个属性NSLocationAlwaysUsageDescription. 3.设置mapView的一个属性,self.mapView.showUserLocation = YES,这个属性一定要设置为YES. 4.设置mapV

iOS高德地图让指定区域或者点显示在屏幕中间

对于高德地图也是一个新手,很多功能看文档,问技术 或者高德群里讨论  群号:204668425 在我们需求中绘制的有 圆 折线 不规则图形 方式,打开地图指定的绘制图形置于屏幕中间 1.首先创建一个数组--  arraySpace 圆: 需要根据圆的半径 中心点计算 垂直的四个 //加入所有圆的点 //设置位置的点 CLLocationCoordinate2D destinationCoordinated =CLLocationCoordinate2DMake(latitude,longitud

高德地图关键字搜索删除上一次搜索的Marker

方法:Marker类的  setMap(null);方法 高德是通过循环调用addmarker(i,d)方法  创建marker标记,所以我们需要 把创建的marker标记压入到一个数组,再第二次搜索时清空数组 var mar = new AMap.Marker(markerOption); search_markers.push(mar); marker.push(new AMap.LngLat(lngX, latY)); 第二次调用清空marker对象 if( search_markers!