應(yīng)該場景描述:后臺返回給我的地址,我需要將地址轉(zhuǎn)成 經(jīng)緯度拍冠,進行導(dǎo)航簇抵。
記錄原因:網(wǎng)上相關(guān)文檔正压,因為版本原因,更新不全面拓劝,無法快速實現(xiàn)功能
使用步驟:
- 項目中導(dǎo)入 高德地圖的 SDK 郑临,我用的是pod, 在pod文件中加入下面代碼,并執(zhí)行 pod install .
#高德地圖
pod 'AMapSearch'
pod 'AMapLocation'
pod 'AMapNavi'
- 在需要使用的 XXX controller.h 導(dǎo)入頭文件
#import <AMapNaviKit/MAMapKit.h> // 創(chuàng)建地圖需要
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
- XXX controller.m 中遵循 代理
@interface XXXMapController ()
<MAMapViewDelegate,
AMapSearchDelegate>
@end
- 功能代碼
- (void)viewDidLoad {
[super viewDidLoad];
// 隱藏navBar
[self navBarNeedHidden:YES andAnimation:NO];
[self configSubViews];
}
-(void)configSubViews {
[AMapServices sharedServices].enableHTTPS = YES;
// 配置用戶APP Key --> 自己在高德平臺去申請
[[AMapServices sharedServices] setApiKey:@"xxxxxxxxxxxxxxxxxxx"];
///初始化地圖
self.maMapView = [[MAMapView alloc] initWithFrame:self.mapBGView.bounds];
self.maMapView.zoomLevel = 14.5f;
self.search = [[AMapSearchAPI alloc] init];
// self.maMapView.delegate = self;
self.search.delegate = self;
//發(fā)起正向地理編碼
AMapGeocodeSearchRequest *searchRequest = [[AMapGeocodeSearchRequest alloc] init];
// 將字符串的地址 通過正向地理編碼轉(zhuǎn)換為 經(jīng)緯度坐標(biāo)點
searchRequest.address = self.addressStr;
[self.search AMapGeocodeSearch: searchRequest];
// 發(fā)起逆向地理編碼
// AMapReGeocodeSearchRequest *searchRequest = [[AMapReGeocodeSearchRequest alloc] init];
// searchRequest.location.latitude = xx.xxxxxx;
// searchRequest.location.longitude = xx.xxxxxx;
// [self.search AMapReGoecodeSearch:searchRequest];
///把地圖添加至view
[self.mapBGView addSubview:self.maMapView];
}
#pragma mark -- MAMapViewDelegate
// 自定義標(biāo)記點圖標(biāo)躺翻,暫時不需要自定義
/*
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
}
annotationView.image = [UIImage imageNamed:@"warehouseMap"];
//設(shè)置中心點偏移公你,使得標(biāo)注底部中間點成為經(jīng)緯度對應(yīng)點
annotationView.centerOffset = CGPointMake(0, -18);
return annotationView;
}
return nil;
}
*/
#pragma mark -- AMapSearchDelegate
// 正向地理編碼(將文字地址轉(zhuǎn)換為坐標(biāo)點的經(jīng)緯度)
-(void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response {
if (response.geocodes.count == 0) {
return;
}
AMapGeoPoint *point = response.geocodes[0].location;
self.latitude = point.latitude;
self.longitude = point.longitude;
self.maMapView.centerCoordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude);
[self.maMapView addAnnotation:pointAnnotation];
}
// 逆向地理編碼(將文字地址轉(zhuǎn)換為坐標(biāo)點的經(jīng)緯度)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (![DDUtilsisNullOrNil:response.regeocode]) {
AMapReGeocode *regeocode = response.regeocode;
CLLocationDegrees latitude = request.location.latitude;
CLLocationDegrees longitude = request.location.longitude;
NSString *address = regeocode.formattedAddress; // 獲得檢索位置
}
}