/* 使用高德地图API,请注册Key,注册地址:http://lbs.amap.com/console/key */
const static NSString *APIKey = @"";
initMapView
- (void)initMapView
{
// 配置用户Key
[MAMapServices sharedServices].apiKey = APIKey;
_maMapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
_maMapView.delegate = self;
// 设置指南针和比例尺的位置
_maMapView.compassOrigin = CGPointMake(_maMapView.compassOrigin.x, 21);
_maMapView.scaleOrigin = CGPointMake(_maMapView.scaleOrigin.x, 21);
// 开启定位
// 取得定位权限,有两个方法,取决于你的定位使用情况
// 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
// iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法
// 在Info.plist文件中添加如下配置:
// NSLocationAlwaysUsageDescription Always
// NSLocationWhenInUseUsageDescription InUse
_maMapView.showsUserLocation = YES;
// 设置跟随定位模式,将定位点设置成地图中心点
_maMapView.userTrackingMode = MAUserTrackingModeFollow;
[self.view addSubview:_maMapView];
}
初始化 AMapSearchAPI
// 初始化 AMapSearchAPI
- (void)initSearch
{
// 初始化检索对象
// 初始化之前请设置 AMapSearchServices 中的APIKey,否则将无法正常使用搜索服务.
[AMapSearchServices sharedServices].apiKey = APIKey;
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
}
逆地理编码
// 逆地理编码
- (void)reGeoAction
{
if (_currentLocation)
{
AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
// 构造AMapReGeocodeSearchRequest对象,配置查询参数(中心点坐标)
request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
// 进行逆地理编码查询
[_search AMapReGoecodeSearch:request];
}
}
要实时获得用户的经纬度:则需要添加下面这个代理方法
定位回调
// 定位回调
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
NSLog(@"userLocation:%@",userLocation.location);
_currentLocation = [userLocation.location copy];
}
定位回调发起逆地理编码
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
if (updatingLocation)
{
_currentLocation = [userLocation.location copy];
lat = [NSString stringWithFormat:@"%f",_currentLocation.coordinate.latitude];
lon = [NSString stringWithFormat:@"%f",_currentLocation.coordinate.longitude];
// 发起逆地理编码
[self reGeoAction];
_maMapView.showsUserLocation = NO;
}
}
逆地理编码查询回调函数
// 逆地理编码查询回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response;
{
// NSLog(@"response :%@",response);
if (response.regeocode != nil)
{
NSString *title = response.regeocode.addressComponent.city;
if (title.length == 0)
{
title = response.regeocode.addressComponent.province;
}
// 给定位标注的title和subtitle赋值,在气泡中显示定位点的地址信息
_maMapView.userLocation.title = title;
_maMapView.userLocation.subtitle = response.regeocode.formattedAddress;
}
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response;
{
if (response.regeocode != nil)
{
NSString *title = response.regeocode.addressComponent.city;
if (title.length == 0)
{
title = response.regeocode.addressComponent.province;
}
name = response.regeocode.formattedAddress;
NSString *result = [NSString stringWithFormat:@"当前位置:%@",name];
self.resultLocation.text = result;
// 添加大头针,根据给定经纬度进行定位并添加标注
MAPointAnnotation *reGeocodeAnnotation = [[MAPointAnnotation alloc] init];
[reGeocodeAnnotation setCoordinate:_currentLocation.coordinate];
reGeocodeAnnotation.title = title;
reGeocodeAnnotation.subtitle = name;
// 添加标注
[_maMapView addAnnotation:reGeocodeAnnotation];
// 标注是否有动画效果
[_maMapView selectAnnotation:reGeocodeAnnotation animated:YES];
}
}
显示Annotation,每在MapView中加入一个Annotation,就会调用此方法
// 显示Annotation,每在MapView中加入一个Annotation,就会调用此方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
// 内存优化后写法,重用机制
static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
// 判断是否存在
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
}
// 显示注释的标题和子标题
annotationView.canShowCallout = YES;
// 设置大头针动画
annotationView.animatesDrop = YES;
// 是否支持拖动
annotationView.draggable = NO;
// 设置大头针颜色
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
}
return nil;
}
搜索失败回调方法
// 搜索失败回调方法
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error;
{
NSLog(@"request :%@,error :%@",request,error);
}
点击Annotation回调
// 点击Annoation回调
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
// 若点击的是定位标注,则执行逆地理编码
if ([view.annotation isKindOfClass:[MAUserLocation class]])
{
[self reGeoAction];
}
}
修改定位状态
- (void)initControls
{
_locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
_locationButton.frame = CGRectMake(20, CGRectGetHeight(_maMapView.bounds) - 80, 40, 40);
_locationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
_locationButton.backgroundColor = [UIColor whiteColor];
_locationButton.layer.cornerRadius = 5;
[_locationButton addTarget:self action:@selector(locateAction) forControlEvents:UIControlEventTouchUpInside];
[_locationButton setImage:[UIImage imageNamed:@"localion_no"] forState:UIControlStateNormal];
[_maMapView addSubview:_locationButton];
}
- (void)locateAction
{
if (_maMapView.userTrackingMode != MAUserTrackingModeFollow)
{
[_maMapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
}
}
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
{
// 修改定位按钮状态
if (mode == MAUserTrackingModeNone)
{
[_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
}
else
{
[_locationButton setImage:[UIImage imageNamed:@"location_yes"] forState:UIControlStateNormal];
}
}
在工程中导入
#import
#import
@interface CheckinViewController ()
{
MAMapView *_maMapView;
AMapSearchAPI *_search;
CLLocation *_currentLocation;
NSString *lat;
NSString *lon;
NSString *name;
}