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

2017-07-25  本文已影响2849人  翻滚的炒勺2013

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
上一篇 下一篇

猜你喜欢

热点阅读