在使用高德地圖之前需要現(xiàn)在高德地圖開放平臺創(chuàng)建一個APP趋翻,會自動生成一個appKey碍讯,在工程中使用高德地圖sdk時是需要這個key的,然后在自己的工程導(dǎo)入高德地圖sdk奕筐,高德地圖開放平臺上有詳細(xì)的步驟工扎。
1徘钥、引入需要的地圖框架
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
2、創(chuàng)建地圖以及需要的屬性
//地圖
self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
self.mapView.delegate = self; //遵循代理<MAMapViewDelegate>
[self.view addSubview:self.mapView];
//定位
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self; //遵循代理<AMapLocationManagerDelegate>
[self.locationManager startUpdatingLocation];//開始定位
//逆地理編碼回調(diào)
self.regeo = [[AMapReGeocodeSearchRequest alloc] init];
self.searchPoi = [[AMapSearchAPI alloc] init];
self.searchPoi.delegate = self; //遵循代理<AMapSearchDelegate>
//設(shè)置地圖中間的圖片
if (self.centerImgView == nil)
{
self.centerImgView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-20)/2, (kScreenHeight-37)/2, 20, 37)];
self.centerImgView.image = [UIImage imageNamed:@"anno"];
}
[self.mapView addSubview:self.centerImgView];
//設(shè)置button肢娘,顯示當(dāng)前定位的地址(也可以用Label呈础,因為別的功能需要它的點擊事件)
self.poiButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, kScreenWidth-40, 40)];
self.poiButton.backgroundColor = [UIColor colorWithRed:248/255.0 green:252/255.0 blue:255/255.0 alpha:1];
self.poiButton.layer.masksToBounds = YES;
self.poiButton.layer.cornerRadius = 20.0;
[self.poiButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
[self.mapView addSubview:self.poiButton];
3、定位代理方法
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
//定位結(jié)果
NSLog(@"當(dāng)前經(jīng)緯度:location:{lat:%f; lon:%f}", location.coordinate.latitude, location.coordinate.longitude);
//將當(dāng)前經(jīng)緯度設(shè)置為地圖的中心經(jīng)緯度
self.mapView.centerCoordinate = location.coordinate;
//因為這個是后臺持續(xù)定位的代理方法蔬浙,所以必須停止定位猪落,否則地圖只要一移動就會回到當(dāng)前所在位置贞远。
//因為一進(jìn)入地圖就會定位畴博,延遲1s停止定位,是為了能夠精準(zhǔn)的獲取到當(dāng)前位置蓝仲,否則可能會出現(xiàn)你定位在當(dāng)前位置了俱病,但是button上顯示的卻是別的地方的位置
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//執(zhí)行事件
[self.locationManager stopUpdatingLocation];//停止定位
});
}
4官疲、地圖區(qū)域改變完成后會調(diào)用此接口(挪動地圖改變經(jīng)緯度)
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
{
self.centerCoor = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
//開始逆地理編碼,把當(dāng)前經(jīng)緯度轉(zhuǎn)成中文地址
self.regeo.location = [AMapGeoPoint locationWithLatitude:self.centerCoor.latitude longitude:self.centerCoor.longitude];
[self.searchPoi AMapReGoecodeSearch:self.regeo];
}
5亮隙、逆地理編碼回調(diào)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil)
{
//解析response獲取地址描述途凫,可以自行選取需要的信息
// self.nowStr 是由當(dāng)前經(jīng)緯度轉(zhuǎn)化的中文地址
self.nowStr = [NSString stringWithFormat:@"%@%@%@%@", response.regeocode.addressComponent.province, response.regeocode.addressComponent.city, response.regeocode.addressComponent.district, response.regeocode.addressComponent.streetNumber.street];
;
NSLog(@"??%@", self.nowStr);
[self.poiButton setTitle:self.nowStr forState:(UIControlStateNormal)];
}
}
上面用到的屬性(忘記寫在上面了,就補在這里)
@property (strong, nonatomic) MAMapView *mapView;
@property (strong, nonatomic) AMapLocationManager *locationManager;
@property (strong, nonatomic) AMapSearchAPI *searchPoi;
@property (strong, nonatomic) AMapReGeocodeSearchRequest *regeo;
//顯示中心點經(jīng)緯度的地址
@property (strong, nonatomic) UIButton *poiButton;
//當(dāng)前位置地址
@property (strong, nonatomic) NSString *nowStr;
//中心點大頭針
@property (strong, nonatomic) UIImageView *centerImgView;
//中心點經(jīng)緯度結(jié)構(gòu)體
//這個就是當(dāng)前地圖中心店的經(jīng)緯度溢吻,如果需要使用這個經(jīng)緯度维费,可以直接用這個屬性
@property (assign, nonatomic) CLLocationCoordinate2D centerCoor;