IOS高德地圖逆地理編碼定位+網絡判斷

本人開發的開發者技術變現資源彙集地,你們支持下,下面是網址android

https://www.baiydu.comgit

 

 

先說下這功能的流程,  流程:判斷用戶是否聯網--->獲取用戶地理位置經緯度--->經過經緯度去查詢地理位置名稱網絡

//高德地圖
@property (nonatomic, strong) MAMapView *mapView;//高德地圖
@property (nonatomic, strong) AMapSearchAPI *search;
@property(nonatomic,strong)NSString *longitude;
@property(nonatomic,strong)NSString *latitude;
@property (nonatomic, strong) CLLocationManager  *locationManager;

//本地信息存儲類
@property(nonatomic,strong) AMapAddressComponent *compantAddress;
@property(nonatomic,strong)  NSMutableArray *CellContentArray;
@property(nonatomic,strong)NSString  *currentCityName;


@property(nonatomic,strong)NSString *tempRecordIsOrNoSelect;//是不是首次加載定位

@end
 
@implementation HomeController
//通知設置值  根據選中的地區加載相應的數據
- (void)setCurrentLocation:(NSNotification *)text{
  
  //移除通知
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"citySelectLocationReciveceMethod" object:nil];
   _currentCityName= [NSString stringWithFormat:@"%@",text.object];
    
    if (_currentCityName.length>3) {
        _currentCityName=[_currentCityName substringToIndex:3];
       
    
    }
   else
   {
       
       
   }
    _tempRecordIsOrNoSelect=_currentCityName;
  
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];//開啓網絡監聽
    if (self.navigationController.navigationBar.hidden) {
        self.navigationController.navigationBarHidden=NO;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setCurrentLocation:) name:@"citySelectLocationReciveceMethod" object:nil];
        //地圖定位
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       // if (_tempRecordIsOrNoSelect.length==0) {
            if(status==AFNetworkReachabilityStatusNotReachable)
            {
                showMessage(@"請檢查網絡是否暢通!");
                _tempRecordIsOrNoSelect=nil;
                [SVProgressHUD dismiss];
                return ;
            }
            else
            {
if (_tempRecordIsOrNoSelect.length==0) { [SVProgressHUD showWithStatus:@"定位中......" maskType:SVProgressHUDMaskTypeBlack]; _longitude=[[NSString alloc]init]; _latitude=[[NSString alloc]init]; self.mapView=[[MAMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 500)]; self.mapView.userTrackingMode = 1, _mapView.showsUserLocation = YES; self.mapView.userTrackingMode = MAUserTrackingModeNone; self.mapView.delegate=self; } else { CGRect tempLeftButtonFrame=self.LeftButton.frame; _tempRecordIsOrNoSelect=_currentCityName; [self setLeftButtonTitle:_currentCityName setFontColor:[UIColor whiteColor] setFontSize:16 setFrme:&tempLeftButtonFrame]; } } }]; self.title=@"首頁"; //高德地圖 } //高德地圖代理 - (void)mapViewWillStartLocatingUser:(MAMapView *)mapView { if(![CLLocationManager locationServicesEnabled]){ UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失敗" message:@"請在手機設置中開啓定位功能\n開啓步驟:設置 > 隱私 > 位置 > 定位服務" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alertView show]; self.mapView = nil; self.mapView.delegate = nil; return; }else{ if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) { UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失敗" message:@"請在手機設置中開啓定位功能\n開啓步驟:設置 > 隱私 > 位置 > 定位服務下《***》應用" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alertView show]; self.mapView = nil; self.mapView.delegate = nil; return; } } } - (void)mapViewDidStopLocatingUser:(MAMapView *)mapView { _longitude=[NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.longitude];//@"106.546128";// _latitude= [NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.latitude]; //@"29.559153"; _search = [[AMapSearchAPI alloc] initWithSearchKey:@"e4bb6f15bba9fde10b1cfc13b298370e" Delegate:self]; AMapReGeocodeSearchRequest *regeoRequest = [[AMapReGeocodeSearchRequest alloc] init]; regeoRequest.searchType = AMapSearchType_ReGeocode; regeoRequest.location =[AMapGeoPoint locationWithLatitude:[_latitude floatValue] longitude:[_longitude floatValue]]; regeoRequest.radius = 10000; regeoRequest.requireExtension = YES; //發起逆地理編碼 [_search AMapReGoecodeSearch: regeoRequest]; } //逆編碼查詢代理 - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response { if(response.regeocode != nil) { _compantAddress=response.regeocode.addressComponent; _currentCityName=_compantAddress.province; if (_currentCityName.length>3) { _currentCityName=[_currentCityName substringToIndex:3]; } _tempRecordIsOrNoSelect=_currentCityName;//定位後也要給這個臨時判斷是否選擇的變量賦值 [self setLeftButtonTitle:_currentCityName setFontColor:[UIColor whiteColor] setFontSize:16 setFrme:&computeFrame]; UIImageView *pointView=[[UIImageView alloc]initWithFrame:CGRectMake(computeFrame.size.width-2,10, 13, 8 )]; [pointView setImage:[UIImage imageNamed:@"fanhuijiantou.png"]]; [pointView setBackgroundColor:[UIColor clearColor]]; [self.LeftButton addSubview:pointView]; [self.LeftButton addTarget:self action:@selector(changeLocationClick:) forControlEvents:UIControlEventTouchUpInside]; self.LeftButton.hidden=NO; } else{ showMessage(@"請檢查網是否暢通!"); } [SVProgressHUD dismiss]; } - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation { mapView.showsUserLocation = NO; }

本人作的一款androidApp,積分牆下載可兌支付寶紅包,無廣告看最新國內外大片,各類當前熱門H5頁遊,淘寶天貓高額購物券!!:掃描下載,多謝支持!如有須要作此類產品的能夠聯繫我:2819936788,各種數據API接口!(手機/流量/QB/遊戲/淘寶客高額優惠券/超便宜的代理IP API/產品推廣工具API)ide

 

相關文章
相關標籤/搜索