騰訊地圖獲取附近位置(逆向解析)+單次定位

最近公司要求添加定位功能,我們的需求是在全球都可以定位综慎,經(jīng)過了解騰訊地圖API,發(fā)現(xiàn)定位不是太準(zhǔn),但是還是可以用到的兔簇。
下面跟大家分享一下騰訊地圖單次定位:

集成騰訊地圖SDK。在集成時需要注意:
76D5ABA1-D4A1-4DDD-AB91-52D1C29BE609.png

要用以上路徑進行導(dǎo)入硬耍,我之前直接拖入的話會報錯垄琐。但是只有我這樣,我身邊的狼友沒有遇到经柴。
集成報錯的坑呢就遇到這一個狸窘。我在項目中主要用到的的逆向解析和單次定位。

單次定位代碼:

/*****初始化*****/
- (void)configLocationManager
{
    self.locationManager = [[TencentLBSLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    [self.locationManager setApiKey:@"自己申請的騰訊地圖Key"];
    
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
    // 需要后臺定位的話坯认,可以設(shè)置此屬性為YES翻擒。
    [self.locationManager setAllowsBackgroundLocationUpdates:NO];
    
    // 如果需要POI信息的話,根據(jù)所需要的級別來設(shè)定牛哺,定位結(jié)果將會根據(jù)設(shè)定的POI級別來返回陋气,如:
    [self.locationManager setRequestLevel:TencentLBSRequestLevelName];
    
    // 申請的定位權(quán)限,得和在info.list申請的權(quán)限對應(yīng)才有效
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    if (authorizationStatus == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }
}

// 單次定位
- (void)startSingleLocation {
    [self.locationManager requestLocationWithCompletionBlock:
     ^(TencentLBSLocation *location, NSError *error) {
//         _theNameLab.text = [NSString stringWithFormat:@"國家:%@,省份:%@,城市:%@,地點:%@,街道:%@,名稱:%@,區(qū)縣:%@",location.nation,location.province,location.city,location.address,location.street,location.name,location.district];
         
         _theAddressArr = [[NSArray alloc]init];
         _theAddressArr =location.poiList;
         _AddressStr =[NSString stringWithFormat:@"%@%@            ",location.city,location.name];
         if ([_AddressStr isEqualToString:@"<null>            "]||_AddressStr == nil||[_AddressStr isEqualToString:@"(null)            "]) {
             _AddressStr = @"";
             _DiZhiBut.hidden = YES;
             theStr = @"";
         }else{
             _DiZhiBut.hidden = NO;
             [_DiZhiBut setTitle:_AddressStr forState:UIControlStateNormal];
             theStr = [NSString stringWithFormat:@"%f,%f",location.poiList[0].latitude,location.poiList[0].longitude];

         }
         
        theNameArr = [[NSMutableArray alloc]init];
        theAddressArr = [[NSMutableArray alloc]init];
         for (int yu =0; yu < _theAddressArr.count; yu++) {
             NSString *theNameStr = [NSString stringWithFormat:@"%@            ",location.poiList[yu].name];
             if ([theNameStr isEqualToString:@"<null>            "]||theNameStr == nil||[theNameStr isEqualToString:@"(null)            "]) {
                theNameStr = @"";
             }
             [theNameArr addObject:theNameStr];
             NSString *theOneAddressStr =[NSString stringWithFormat:@"%@            ",location.poiList[yu].address];
             if ([theOneAddressStr isEqualToString:@"<null>            "]||theOneAddressStr == nil||[theOneAddressStr isEqualToString:@"(null)            "]) {
                 theOneAddressStr = @"";
             }
             [theAddressArr addObject:theOneAddressStr];
         }
     }];
}
里面的代碼可以忽略引润,根據(jù)自己的需求寫巩趁。

//定位代理方法——————判斷是否開啟定位。我們需要在plist文件中添加字段淳附。
- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
                 didFailWithError:(NSError *)error {
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    if (authorizationStatus == kCLAuthorizationStatusDenied ||
        authorizationStatus == kCLAuthorizationStatusRestricted) {
//        [self.theNameLab setText:@"定位權(quán)限沒開啟议慰!"];
        self.themessSwitch.on = NO;

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                       message:@"定位權(quán)限未開啟,是否開啟奴曙?"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"是"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
                                                    if( [[UIApplication sharedApplication]canOpenURL:
                                                         [NSURL URLWithString:UIApplicationOpenSettingsURLString]] ) {
                                                        [[UIApplication sharedApplication] openURL:
                                                         [NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                                                    }
                                                }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"否"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
                                                }]];
        
        [self presentViewController:alert animated:true completion:nil];
        
    } else {
        self.themessSwitch.on = YES;

//        [self.theNameLab setText:[NSString stringWithFormat:@"%@", error]];
    }
}

以上就是單次定位褒脯。

逆向解析:
逆向解析首先需要倆個值,1.經(jīng)度2.緯度缆毁,通過這倆個值用get請求接口:
http://apis.map.qq.com/ws/geocoder/v1/?location=%@&get_poi=1&key=ke y&poi_options=address_format=short;radius=2000;page_size=20;page_index=1;policy=1
//里面的參數(shù)自己添加更改番川,根據(jù)騰訊地圖文檔。

1.通過值獲取地址
 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
  
    NSString *theStrOne = [NSString stringWithFormat:@"http://apis.map.qq.com/ws/geocoder/v1/?location=%@&get_poi=1&key=key&poi_options=address_format=short;radius=2000;page_size=20;page_index=1;policy=1",_theStr];
    [manager GET:theStrOne parameters:nil progress:nil success:
     ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         _theAddressArr = [[NSArray alloc]init];
         NSString *theStr = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"message"]];
         if ([theStr isEqualToString:@"query ok"]) {
             _theAddressArr =[[responseObject objectForKey:@"result"]objectForKey:@"pois"];
             [_theAddressTable reloadData];
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
     }];
    
請求到的就是附近位置脊框。

//PS:如有侵權(quán)颁督,請聯(lián)系刪除,謝謝浇雹。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沉御,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子昭灵,更是在濱河造成了極大的恐慌吠裆,老刑警劉巖伐谈,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異试疙,居然都是意外死亡诵棵,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門祝旷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來履澳,“玉大人,你說我怎么就攤上這事怀跛【啻” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵吻谋,是天一觀的道長忠蝗。 經(jīng)常有香客問我,道長漓拾,這世上最難降的妖魔是什么什湘? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮晦攒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘得哆。我一直安慰自己脯颜,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布贩据。 她就那樣靜靜地躺著栋操,像睡著了一般。 火紅的嫁衣襯著肌膚如雪饱亮。 梳的紋絲不亂的頭發(fā)上矾芙,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天,我揣著相機與錄音近上,去河邊找鬼剔宪。 笑死,一個胖子當(dāng)著我的面吹牛壹无,可吹牛的內(nèi)容都是我干的葱绒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼斗锭,長吁一口氣:“原來是場噩夢啊……” “哼地淀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岖是,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤帮毁,失蹤者是張志新(化名)和其女友劉穎实苞,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烈疚,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡黔牵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胞得。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荧止。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖阶剑,靈堂內(nèi)的尸體忽然破棺而出跃巡,到底是詐尸還是另有隱情,我是刑警寧澤牧愁,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布素邪,位于F島的核電站,受9級特大地震影響猪半,放射性物質(zhì)發(fā)生泄漏兔朦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一磨确、第九天 我趴在偏房一處隱蔽的房頂上張望沽甥。 院中可真熱鬧,春花似錦乏奥、人聲如沸摆舟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恨诱。三九已至,卻和暖如春骗炉,著一層夾襖步出監(jiān)牢的瞬間照宝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工句葵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留厕鹃,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓乍丈,卻偏偏與公主長得像熊响,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子诗赌,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354