/* 使用高德地圖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對象,配置查詢參數(shù)(中心點坐標)
request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
// 進行逆地理編碼查詢
[_search AMapReGoecodeSearch:request];
}
}
要實時獲得用戶的經(jīng)緯度:則需要添加下面這個代理方法
定位回調(diào)
// 定位回調(diào)
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
NSLog(@"userLocation:%@",userLocation.location);
_currentLocation = [userLocation.location copy];
}
定位回調(diào)發(fā)起逆地理編碼
- (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];
// 發(fā)起逆地理編碼
[self reGeoAction];
_maMapView.showsUserLocation = NO;
}
}
逆地理編碼查詢回調(diào)函數(shù)
// 逆地理編碼查詢回調(diào)函數(shù)
- (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;
// 添加大頭針,根據(jù)給定經(jīng)緯度進行定位并添加標注
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,就會調(diào)用此方法
// 顯示Annotation,每在MapView中加入一個Annotation,就會調(diào)用此方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
// 內(nèi)存優(yōu)化后寫法,重用機制
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;
}
搜索失敗回調(diào)方法
// 搜索失敗回調(diào)方法
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error;
{
NSLog(@"request :%@,error :%@",request,error);
}
點擊Annotation回調(diào)
// 點擊Annoation回調(diào)
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
// 若點擊的是定位標注,則執(zhí)行逆地理編碼
if ([view.annotation isKindOfClass:[MAUserLocation class]])
{
[self reGeoAction];
}
}
修改定位狀態(tài)
- (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
{
// 修改定位按鈕狀態(tài)
if (mode == MAUserTrackingModeNone)
{
[_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
}
else
{
[_locationButton setImage:[UIImage imageNamed:@"location_yes"] forState:UIControlStateNormal];
}
}
在工程中導入
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
@interface CheckinViewController ()<MAMapViewDelegate,AMapSearchDelegate>
{
MAMapView *_maMapView;
AMapSearchAPI *_search;
CLLocation *_currentLocation;
NSString *lat;
NSString *lon;
NSString *name;
}