ios高德地图定位及定位后数据如何回调

我有一个地方需要用到当前的位置的经纬度和省市区(不用显示地图),因此我想封装一个地图类,以后我只要调用这个类就会返回当前的位置的经纬度和省市区。但是操作的时候,一直不调用我的地图,我就不知道该如何做了,望各位指点一二

.h文件
#import <UIKit/UIKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>



@interface MyUserLocation : UIViewController


{
    void (^Location)(AMapReGeocode *reGeocode);
}

- (void)location:(void (^)(AMapReGeocode * reGeocode))block;

@end


.m文件
#import "MyUserLocation.h"
#import "Method.h"
#define SPAN  MACoordinateSpanMake(0.025, 0.025)

@interface MengUserLocation()<MAMapViewDelegate,AMapSearchDelegate>

@property (nonatomic ,strong) MAMapView * mapView;
@property (nonatomic ,strong) AMapSearchAPI * mapSearchAPI;
@property (nonatomic ,strong) MAUserLocation * currentLocation;

@property (nonatomic ,strong) NSMutableDictionary * userLocationDict;

@end

@implementation MyUserLocation

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.mapView = [[MAMapView alloc] init];
        self.mapView.showsUserLocation = YES;
        self.mapView.delegate = self;
        
        self.mapSearchAPI = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
        
        self.userLocationDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView
{
    if(![CLLocationManager locationServicesEnabled]){
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        self.mapView = nil;
        self.mapView.delegate = nil;
        return;
    }else{
        if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务下《***》应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            self.mapView = nil;
            self.mapView.delegate = nil;
            return;
        }
    }
}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (!isEqualCoordinate(self.currentLocation.coordinate, userLocation.coordinate, 0.001)) {
        
        
        [self.userLocationDict setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.longitude] forKey:@"longitude"];
        [self.userLocationDict setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.latitude] forKey:@"latitude"];
        
        self.currentLocation = userLocation;
        [self.mapView setRegion:MACoordinateRegionMake(self.currentLocation.coordinate, SPAN) animated:YES];
        
        CLLocation * location = userLocation.location;
        AMapReGeocodeSearchRequest * request = [[AMapReGeocodeSearchRequest alloc] init];
        request.searchType = AMapSearchType_ReGeocode;
        request.requireExtension = YES;
        AMapGeoPoint * point = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
        request.location = point;
        [self.mapSearchAPI AMapReGoecodeSearch:request];
    }
}

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    NSLog(@"response = %@",response);
    if (Location) {
        Location(response.regeocode);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值