导入包文件
/*** 高德地图 ***/
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
实现代理
<AMapLocationManagerDelegate,MAMapViewDelegate,AMapSearchDelegate>
定义全局变量
{
/*** 高德地图 ***/
MAMapView * _mapView;
UIButton * _locationButton;
CLLocation * _currentLocation;
//主动定位
AMapSearchAPI * _search;
}
@property(nonatomic,retain)NSString * currentCity;
定义全局宏
//当前城市
#define localTitle @"localTitle"
//当前街道
#define detailTitle @"detailTitle"
//城市id
#define cityId @"cityId"
//获取纬度
#define latitudeLocation @"latitudeLocation"
//获取精度
#define longitudeLocation @"longitudeLocation"
/*** 获取城市城市ID和区域ID ***/
#define getCityUrl @""baseUrl""
定义方法
** 高德地图 **
[self initMapView];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self initSearch];
});
实现方法
#pragma mark - 高德地图定位
-(void)initMapView
{
[AMapServices sharedServices].apiKey = APIKey;
_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
_mapView.delegate = self;
[_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:NO];
// [self.view addSubview:_mapView];
/*** 开启定位功能 ***/
_mapView.showsUserLocation = YES;
}
//初始化Search
-(void)initSearch
{
_search = [[AMapSearchAPI alloc]init];
_search.delegate = self;
}
//搜索请求
-(void)reGeoAction
{
if (_currentLocation) {
AMapReGeocodeSearchRequest * request = [[AMapReGeocodeSearchRequest alloc]init];
request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
[_search AMapReGoecodeSearch:request];
}
}
//搜索
-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
// NSLog(@"11");
#pragma mark - 输出
_currentCity = [[NSString alloc]init];
//城市
NSString * title = response.regeocode.addressComponent.city;
//城市不存在就显示省份
if (title.length == 0) {
title = response.regeocode.addressComponent.province;
}
//获取城市
NSString * cityCode = response.regeocode.addressComponent.citycode;
NSString * areaCode = response.regeocode.addressComponent.adcode;
// NSLog(@"cityCode = %@",cityCode);
// NSLog(@"areaCode = %@",areaCode);
/*** 乡镇街道 ***/
NSString * cityString = response.regeocode.addressComponent.city; //城市
NSString * areaString = response.regeocode.addressComponent.district;
NSString * townshipS = response.regeocode.addressComponent.streetNumber.street;
NSString * townshipString = @"";
if (cityString.length && areaString.length && townshipS.length) {
townshipString = [NSString stringWithFormat:@"当前位置: %@%@%@",response.regeocode.addressComponent.city,response.regeocode.addressComponent.district,response.regeocode.addressComponent.streetNumber.street];
}else if (cityString.length && areaString.length){
townshipString = [NSString stringWithFormat:@"当前位置: %@%@",response.regeocode.addressComponent.city,response.regeocode.addressComponent.district];
}else{
townshipString = [NSString stringWithFormat:@"当前位置: %@",response.regeocode.addressComponent.city];
}
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]init];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSMutableDictionary * parameter = [NSMutableDictionary dictionary];
parameter[@"cityCode"] = cityCode;
parameter[@"areaCode"] = areaCode;
// parameter[@"cityCode"] = @"0851";
// parameter[@"areaCode"] = @"520100";
[manager POST:getCityUrl parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// NSLog(@"%@",responseObject);
NSDictionary * dataDic = responseObject[@"data"];
if (dataDic.count > 0) {
/*** 定位成功 ***/
/*** 城市ID ***/
NSString * cityID = [NSString stringWithFormat:@"%@",dataDic[@"id"]];
// NSLog(@"-----------%@",cityID);
/*** 当前城市 ***/
NSString * localTitleString = dataDic[@"name"];
// NSLog(@"%@",localTitleString);
/*** 偏好设置 ***/
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
/*** 判断是否已经定位 ***/
NSString * cityIdString = [userDefaults objectForKey:cityId];
/*** 存储当前城市 ***/
[userDefaults setValue:localTitleString forKey:localTitle];
/*** 是否切换城市 ***/
[userDefaults setValue:localTitleString forKey:@"changeCity"];
/*** 存储当前街道 ***/
[userDefaults setValue:townshipString forKey:detailTitle];
[userDefaults synchronize];
if (cityIdString) {
return;
}else{
//不存在城市Id
/*** 存储城市ID ***/
[userDefaults setValue:cityID forKey:cityId];
/*** 存储系统定位ID ***/
[userDefaults setValue:cityID forKey:@"LocationCity"];
}
}else{
/*** 无此城市 ***/
/*** 取上次定位城市 ***/
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSString * cityName = [userDefaults objectForKey:localTitle];
if (!(cityName.length > 0)) {
/*** 无上次定位城市 ***/
//当前城市
[userDefaults setValue:cityString forKey:localTitle];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"您所在的城市不再服务范围内!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
[alert show];
//当前街道
[userDefaults setValue:townshipString forKey:detailTitle];
/*** 存储当前城市 ***/
[userDefaults setValue:@"苏州" forKey:localTitle];
[userDefaults setValue:townshipString forKey:detailTitle];
[userDefaults setValue:@"2" forKey:cityId];
[userDefaults synchronize];
});
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
//获得经纬度坐标
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
// NSLog(@"1122");
//获取经纬度
NSString * latitudeString = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
NSString * longitudeString = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
_currentLocation = [userLocation.location copy];
if (_currentLocation) {
[self reGeoAction]; //反地理编码
/*** 存储经纬度 ***/
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:latitudeString forKey:latitudeLocation];
[userDefaults setValue:longitudeString forKey:longitudeLocation];
[userDefaults synchronize];
}
}
#pragma mark - 代理 定位失败后调用此方法
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSString * cityName = [userDefaults objectForKey:@"selectCity"];
if (!(cityName.length > 0)) {
/*** 无上次定位城市 ***/
//当前城市
[userDefaults setValue:@"苏州" forKey:@"localTitle"];
//当前街道
[userDefaults setValue:@"当前位置: o(╯□╰)o暂未获取您的具体位置" forKey:@"detailTitle"];
[userDefaults setValue:@"2" forKey:@"cityId"];
//定位失败
[userDefaults setObject:@"NoLocation" forKey:@"locationFailuer"];
[userDefaults synchronize];
/*** 弹出对话框, 定位失败 ***/
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"定位失败, 请打开定位!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
// [alert show];
});
NSLog(@"%@",error);
}
}