美文网首页
iOS 简单集成高德地图 一步步教你实现定位,插入大头针

iOS 简单集成高德地图 一步步教你实现定位,插入大头针

作者: 翻滚的炒勺2013 | 来源:发表于2017-07-25 17:30 被阅读2849次

0.先到高德官网创建你的应用高德地图官网

C5752CAB-9C10-4428-9031-B6D300E0008A.png

1.复制这里的Bundle ID

44C3021C-E4F8-4571-96FC-7B908C500939.png

2.填好信息,返回页面会生成一个key

DBC59451-74B9-4BB9-BDB1-5EBE4DA91E73.png

3.在podfile添加

 pod 'AMap2DMap' #2D地图SDK (2D和3D不能同时使⽤用)
  pod 'AMapSearch'
  pod 'AMapLocation'

4.打开终端cd 你项目的路径之后加一下代码

pod install

5.更新好之后打开工程,在appDelegate中

pod install

6.在appDelegate中添加你的key

   [AMapServices sharedServices].apiKey = @"你的key";

7.在pch中添加头文件

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

8.懒加载地图

- (MAMapView *)header {
    if (_header == nil) {
        _header = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - 200 - kNavHeight)];
        _header.delegate = self;
        /// 打开定位
        _header.showsUserLocation = YES;
        /*
         MAUserTrackingModeNone              = 0,    ///< 不追踪用户的location更新
         MAUserTrackingModeFollow            = 1,    ///< 追踪用户的location更新
         MAUserTrackingModeFollowWithHeading = 2     ///< 追踪用户的location与heading更新
         */
        _header.userTrackingMode = MAUserTrackingModeFollow;
        ///设定定位精度。默认为kCLLocationAccuracyBest
        _header.desiredAccuracy = kCLLocationAccuracyBest;
        /// 是否显示指南针
        _header.showsCompass = NO;
        /// 设定定位的最小更新距离。默认为kCLDistanceFilterNone,会提示任何移动

        _header.distanceFilter = 15.0f;
        /// 是否显示比例尺,默认为YES
        _header.showsScale = NO;
        /// 是否支持缩放,默认为YES

        _header.zoomEnabled = YES;
        /// 是否支持平移,默认为YES
        _header.scrollEnabled = YES;
        /// 缩放级别, [3, 20]
        _header.zoomLevel = 15;
        /// 去掉高德地图logo


        for (UIView *view in _header.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                [view removeFromSuperview];
            }
        }

    }
    return _header;
    
}

9.添加地图

    self.tableView.tableHeaderView = self.header;

650632E5-85AC-4099-95BD-C9F23C2EE323.png

10.获得当前的经纬度

位置或者设备方向更改变后会调用此方法

/**
 * @brief 位置或者设备方向更新后调用此接口
 * @param mapView 地图View
 * @param userLocation 用户定位信息(包括位置与设备方向等数据)
 * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
 */
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation;

在这里进行逆地理编码, 构造一个AMapSearchAPI对象,遵守代理方法AMapSearchDelegate

- (AMapSearchAPI *)search {
    if (_search == nil) {
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

设置逆地理编码查询参数

AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];

regeo.location                    = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
regeo.requireExtension            = YES;

发起逆地理编码查询

[self.search AMapReGoecodeSearch:regeo];

逆地理编码完整代码

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    
    if (updatingLocation ){
//        self.isFirstLocated = YES;
        AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
        regeo.location = [AMapGeoPoint locationWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
        regeo.requireExtension = YES;
        [self.search AMapReGoecodeSearch:regeo];
        
    }
  
}

逆地理编码查询回调函数

/**
 * @brief 逆地理编码查询回调函数
 * @param request  发起的请求,具体字段参考 AMapReGeocodeSearchRequest 。
 * @param response 响应结果,具体字段参考 AMapReGeocodeSearchResponse 。
 */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response;

在这个方法里拿到AMapReGeocodeSearchResponse类型的response,查看头文件里面有AMapReGeocode类型的regeocode,把它转化为AMapReGeocode,我们定义个模型来接受,我定义的是reGeocodeModel.在这个模型里面还有一个地址组成要素模型,再定义个AMapAddressComponent类型的addressComponentModel来接收,这样所有的信息就都可以拿到了.我们开始添加大头针,下面是完整代码

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
   AMapReGeocode *reGeocodeModel = response.regeocode;
   AMapAddressComponent *addressComponentModel = reGeocodeModel.addressComponent;
   if (response.regeocode != nil) {

       MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
       pointAnnotation.coordinate = self.header.userLocation.coordinate;
  
       pointAnnotation.title = [NSString stringWithFormat:@"%@%@%@",addressComponentModel.province,addressComponentModel.city,addressComponentModel.district];
//        pointAnnotation.subtitle = [NSString stringWithFormat:@"%@%@%@%@%@",addressComponentModel.township,addressComponentModel.neighborhood,addressComponentModel.building,addressComponentModel.streetNumber.street,addressComponentModel.streetNumber.number];
       pointAnnotation.subtitle = @"在你家";

       //将大头针添加到地图中
       [self.header addAnnotation:pointAnnotation];
       
       //默认选中气泡
       [self.header selectAnnotation:pointAnnotation animated:YES];
       
   }
   

   /// 坐标
   NSLog(@"%@",response.regeocode.formattedAddress);
   for (ZCDataModel *dataModel in self.dataArray) {
       if ([dataModel.title isEqualToString:@"我的位置"]) {
//            dataModel.details = response.regeocode.formattedAddress;
       }
   }
   /// 当位置刷新的时候,更新位置
   [self.tableView reloadData];
}

5D4DD18A-2695-4D01-B960-22D6DAEFC903.png

相关文章

  • iOS 使用高德地图正确姿势(三)

    iOS 使用高德地图正确姿势(一)iOS 使用高德地图正确姿势(二) 实现大头针始终在地图中心,拖动地图实时poi...

  • iOS 简单集成高德地图 一步步教你实现定位,插入大头针

    0.先到高德官网创建你的应用高德地图官网 1.复制这里的Bundle ID 2.填好信息,返回页面会生成一个key...

  • 高德地图集成总结

    iOS开发,第一次集成高德地图,实现了简单的定位,绘制气泡,导航。简单总结: 1.定位只是为了获取当前位置(如果需...

  • iOS 高德地图的使用

    iOS:高德地图的使用 本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示、地图的绘制、地图的定位、地...

  • mapView上添加大头针

    高德地图中,大头针是MAAnnotationView这是一个很简单的View,相当于高德简单做了一个显示大头针的V...

  • INVALID_USER_SCODE

    这几天做高德地图,发现INVALID_USER_SCODE这个问题,可以定位,导航,设置大头针,一切功能都可以实现...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

  • iOS 高德地图实现大头针展示,分级大头针,自定制大头针,在地图

    本项目主要是基于高德地图实现了大头针展示,分级大头针,自定制大头针,在地图上画线,线和点共存,路线规划(驾车路线规...

  • iOS集成高德地图SDK

    前言:关于这次集成高德地图,打算分几个内容定位 POI检索 导航 线路规划,现在只是简单地实现了前两个功能,先记录...

  • 获取SHA1

    今天遇到集成高德地图,不,是融云中的高德地图定位,填坑的。定位失败,一直提示定位失败,报错说key过期或者不正确。...

网友评论

      本文标题:iOS 简单集成高德地图 一步步教你实现定位,插入大头针

      本文链接:https://www.haomeiwen.com/subject/vzfgkxtx.html