最近公司要求添加定位功能,我們的需求是在全球都可以定位综慎,經(jīng)過了解騰訊地圖API,發(fā)現(xiàn)定位不是太準(zhǔn),但是還是可以用到的兔簇。
下面跟大家分享一下騰訊地圖單次定位:
要用以上路徑進行導(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)系刪除,謝謝浇雹。