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

iOS 使用高德地图正确姿势(一)
iOS 使用高德地图正确姿势(二)

实现大头针始终在地图中心,拖动地图实时poi出中心地址


地图.png

头文件

#import 
#import 
#import 
#import 
//协议

@property (nonatomic, strong) AMapLocationManager *locationManager;//定位管理者
@property (nonatomic, strong) MAMapView * mapView; //地图
@property(nonatomic,strong)AMapSearchAPI *search;
@property (nonatomic, strong) CLLocation *currentLocation;//定位坐标

一、初始化设置

    //高德key 初始化
    [AMapServices sharedServices].apiKey =GDkey;
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    //初始化_search 用于逆编码
    self.search =[[AMapSearchAPI alloc] init];
    self.search.delegate=self;
    // 带逆地理信息的一次定位(返回坐标和地址信息)
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间,最低2s,此处设置为2s
    self.locationManager.locationTimeout =2;
    //   逆地理请求超时时间,最低2s,此处设置为2s
    self.locationManager.reGeocodeTimeout = 2;
    [self GetLococation];//定位返回结果

二、地图界面

//加载地图
- (void)setmap{
    TSMapView *MapView=[[TSMapView alloc]init];
    MapView.frame=CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeigth-SafeAreaBoHttomHeight-SafeAreaTopHeight-160);
    [self.view addSubview:MapView];
     NSString *str=@"提示:如果定位不准,请拖动地图,标记出准确的店铺位置,业务员和骑手才能更快找到";
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10,16)];
  MapView.TipsLab.attributedText=string;    
    ///地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)
    [AMapServices sharedServices].enableHTTPS = YES;
    ///初始化地图
    _mapView = [[MAMapView alloc] initWithFrame:MapView.bounds];
    _mapView.mapType = MKMapTypeStandard;
    ///把地图添加至view
    [MapView addSubview:_mapView];
    //设置地图缩放比例,即显示区域
    [_mapView setZoomLevel:15.1 animated:YES];
    _mapView.delegate = self;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;//追踪用户的location更新
    _mapView.showsUserLocation = NO;    //定位小蓝点
    //设置定位精度
    _mapView.desiredAccuracy = kCLLocationAccuracyBest;
    //设置定位距离
    _mapView.distanceFilter = 5.0f;
    //地图加载后调用 保证在地图上方
    [MapView bringSubviewToFront:MapView.DTZimag];
    [MapView bringSubviewToFront:MapView.TipsView];
}

三、进入地图的一次定位回调,把定位坐标设成地图中心点

- (void)GetLococation{
    // 带逆地理(返回坐标和地址信息)。将下面代码中的 YES 改成 NO ,则不会返回地址信息。
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        /**location 当前定位地理信息*/
        NSLog(@"location:%@", location);
        self.currentLocation=location;
       //把中心点设成自己的坐标
        self.mapView.centerCoordinate = self.currentLocation.coordinate;
        //当前位置逆地理信息
        if (regeocode)
        {
            NSLog(@"reGeocode:%@", regeocode);
        }
    }];
}

四、拖动地图完成后回调

#pragma mark - MAMapViewDelegate
//地图区域改变完成后调用的接口
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"%f",mapView.region.center.latitude); //拿到中心点的经纬度
    NSLog(@"%f/n",mapView.region.center.longitude);
    //根据中心坐标进行周边搜索 */
    [self searchAround:mapView.region.center.latitude getLong:mapView.region.center.longitude];
}

五、根据中心坐标进行周边搜索

- (void)searchAround:(CLLocationDegrees )location getLong:(CLLocationDegrees )Longitude{
    //构造AMapPOIAroundSearchRequest对象,设置周边请求参数
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:location longitude:Longitude];
    // types属性表示限定搜索POI的类别,默认为:餐饮服务|商务住宅|生活服务
    // POI的类型共分为20种大类别,分别为:
    // 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
    // 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
    // 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
    request.types = @"餐饮服务|生活服务|商务住宅|公司企业|地名地址信息";
    request.sortrule = 0;
    request.requireExtension = YES;    
    //发起周边搜索
    [self.search AMapPOIAroundSearch: request];
}

六、实现POI搜索对应的回调

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    NSLog(@"周边搜索回调");
    if(response.pois.count == 0)
    {
        return;
    }
//返回10组数据,想要什么自己拿
    self.dataArray = [NSMutableArray arrayWithArray:response.pois];
}

你可能感兴趣的:(iOS 使用高德地图正确姿势(三))