地图导入 - GeekBand/GBMoran_iOS GitHub Wiki

蓦然Lab Manual----地图导入

以高德地图为例.

高德地图开发环境配置

自动配置和手动配置

申请key

参考文章: http://lbs.amap.com/api/ios-sdk/guide/buildproject/

定位

我们要获取到的主要是经纬度和详细地址

首先, 引入头文件

#import <AMapSearchKit/AMapSearchKit.h>

#import <MAMapKit/MAMapKit.h>

签订协议, 必须要签订, 否则不会执行

AMapSearchDelegate

MAMapViewDelegate

在viewDidLoad中配置

[MAMapServices sharedServices].apiKey = @"69b035e62c17ae7f98898392e2b17376";
[AMapSearchServices sharedServices].apiKey = @"69b035e62c17ae7f98898392e2b17376";
self.mapView = [[MAMapView alloc] init];
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapSearchAPI = [[AMapSearchAPI alloc] init];
self.mapSearchAPI.delegate = self;

协议方法

获取经纬度

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

获取地址

/**
 *  逆地理编码查询回调函数
 *
 *  @param request  发起的请求,具体字段参考 AMapReGeocodeSearchRequest 。
 *  @param response 响应结果,具体字段参考 AMapReGeocodeSearchResponse 。
 */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response; 
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    [self.locationDic setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.longitude] forKey:@"longitude"];
    [self.locationDic setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.latitude] forKey:@"latitude"];

    self.currentLocation = userLocation;

    CLLocation * location = userLocation.location;

    AMapReGeocodeSearchRequest * request = [[AMapReGeocodeSearchRequest alloc] init];
    request.requireExtension = YES;
    request.radius = 10000;
    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
{
    if(response.regeocode != nil)
    {
        NSString *result = [NSString stringWithFormat:@"%@", response.regeocode.formattedAddress];
    
        [self.locationDic setObject:result forKey:@"location"];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"observeLocationValue" object:nil userInfo:self.locationDic];
    }
}

我们定义了一个字典self.locationDic来存放获取到的数据, 拿到全部的数据后, 用通知把数据传到发布页面. 至此我们就完成了经纬度和地址的获取.