RKLBSManager.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. //
  2. // RKLBSManager.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/10/19.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKLBSManager.h"
  9. #import <MapKit/MapKit.h>
  10. @interface RKLBSManager()<CLLocationManagerDelegate>
  11. @property(nonatomic,strong)CLLocationManager *clManager;
  12. @property(nonatomic,copy)RKLbsBlock lbsEvent;
  13. @end
  14. static RKLBSManager *_lbsManager = nil;
  15. @implementation RKLBSManager
  16. +(instancetype)shareManager {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. _lbsManager = [[super allocWithZone:NULL]init];
  20. [_lbsManager createCL];
  21. });
  22. return _lbsManager;
  23. }
  24. -(void)createCL{
  25. if (!_clManager) {
  26. _clManager = [[CLLocationManager alloc] init];
  27. [_clManager setDesiredAccuracy:kCLLocationAccuracyBest];
  28. _clManager.delegate = self;
  29. }
  30. }
  31. -(void)startLocation {
  32. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  33. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  34. UIAlertController *alertContro = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"%@%@%@",YZMsg(@"打开“定位服务”来允许“"),[infoDictionary objectForKey:@"CFBundleDisplayName"],YZMsg(@"确定您的位置")] preferredStyle:UIAlertControllerStyleAlert];
  35. UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:YZMsg(@"设置") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  36. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  37. }];
  38. [alertContro addAction:cancleAction];
  39. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:YZMsg(@"取消") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  40. }];
  41. [alertContro addAction:sureAction];
  42. [[YBBaseAppDelegate sharedAppDelegate].topViewController presentViewController:alertContro animated:YES completion:nil];
  43. }else {
  44. SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
  45. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined && [_clManager respondsToSelector:requestSelector]) {
  46. [_clManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
  47. } else {
  48. [_clManager startUpdatingLocation];
  49. }
  50. }
  51. }
  52. -(void)locationComplete:(RKLbsBlock)complete; {
  53. self.lbsEvent = complete;
  54. }
  55. - (void)stopLocation {
  56. [_clManager stopUpdatingHeading];
  57. [_clManager stopUpdatingLocation];
  58. }
  59. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  60. if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
  61. [self stopLocation];
  62. } else {
  63. [_clManager startUpdatingLocation];
  64. }
  65. }
  66. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  67. [self stopLocation];
  68. }
  69. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
  70. CLLocation *newLocatioin = locations[0];
  71. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  72. NSString* locationLat = [NSString stringWithFormat:@"%f",newLocatioin.coordinate.latitude];
  73. NSString* locationLng = [NSString stringWithFormat:@"%f",newLocatioin.coordinate.longitude];
  74. YBWeakSelf;
  75. [geocoder reverseGeocodeLocation:newLocatioin completionHandler:^(NSArray *placemarks, NSError *error) {
  76. if (!error) {
  77. CLPlacemark *placeMark = placemarks[0];
  78. NSString *city = placeMark.locality;
  79. [cityDefault saveLocationLat:locationLat];
  80. [cityDefault saveLocationLng:locationLng];
  81. [cityDefault saveLocationCity:city];
  82. NSString *provinceStr = placeMark.administrativeArea?placeMark.administrativeArea:@"";
  83. NSString *areaStr = placeMark.subLocality?placeMark.subLocality:@"";
  84. /*
  85. if ([city hasSuffix:@"市"]) {
  86. city = [city substringToIndex:city.length-1];
  87. }
  88. if (_fromEdit) {
  89. }else{
  90. [PublicObj updataNewCity:city];
  91. }
  92. */
  93. [weakSelf updataTabbarWithCity:city];
  94. //
  95. if (exactCity) {
  96. [weakSelf getCityWithLat:locationLat andLng:locationLng andArray:placemarks cusArray:@[provinceStr,city,areaStr]];
  97. }else{
  98. if (weakSelf.lbsEvent) {
  99. self.lbsEvent(placemarks, @[provinceStr,city,areaStr]);
  100. }
  101. }
  102. }
  103. }];
  104. [self stopLocation];
  105. }
  106. #pragma mark - 打开导航软件开始
  107. -(void)showNavigationsWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName{
  108. UIApplication * app = [UIApplication sharedApplication];
  109. YBWeakSelf;
  110. RKActionSheet *sheet = [[RKActionSheet alloc]initWithTitle:YZMsg(@"")];
  111. if ([app canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
  112. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"高德地图") complete:^{
  113. [weakSelf openGdMapWithLat:lat lng:lng endName:endName];
  114. }];
  115. }
  116. if ([app canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
  117. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"百度地图") complete:^{
  118. [weakSelf openBdMapWithLat:lat lng:lng endName:endName];
  119. }];
  120. }
  121. if ([app canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
  122. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"腾讯地图") complete:^{
  123. [weakSelf openTxMapWithLat:lat lng:lng endName:endName];
  124. }];
  125. }
  126. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"Apple地图") complete:^{
  127. [weakSelf openAppleMapWithLat:lat lng:lng endName:endName];
  128. }];
  129. /*
  130. // 谷歌
  131. if ([app canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  132. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"谷歌地图") complete:^{
  133. [weakSelf openGoogleMapWithLat:lat lng:lng endName:endName];
  134. }];
  135. }
  136. */
  137. [sheet addActionWithType:RKSheet_Cancle andTitle:YZMsg(@"取消") complete:^{
  138. }];
  139. [sheet showSheet];
  140. }
  141. /// 高德
  142. -(void)openGdMapWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName{
  143. NSString *appName = [PublicObj getAppName];
  144. NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=&sname=%@&did=&dlat=%@&dlon=%@&dname=%@&dev=0&t=0",appName,@"我的位置",lat,lng,endName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  145. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
  146. }
  147. /// 百度
  148. -(void)openBdMapWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName{
  149. /// 百度需要坐标转换
  150. double old_lat = [lat doubleValue];
  151. double old_lng = [lng doubleValue];
  152. NSArray *newLocA = [self gcjToBd09llLat:old_lat lon:old_lng];
  153. NSNumber *new_lat = newLocA[0];
  154. NSNumber *new_lng = newLocA[1];
  155. NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name:%@&mode=driving&coord_type=bd09ll",new_lat,new_lng,endName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  156. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
  157. }
  158. /// 腾讯
  159. -(void)openTxMapWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName{
  160. NSString *appName = [PublicObj getAppName];
  161. NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=%@&tocoord=%@,%@&policy=1&referer=%@", endName, lat, lng, appName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  162. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  163. }
  164. /// 苹果地图
  165. -(void)openAppleMapWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName{
  166. float latVal = [NSString stringWithFormat:@"%@", lat].floatValue;
  167. float lngVal = [NSString stringWithFormat:@"%@", lng].floatValue;
  168. CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latVal, lngVal);
  169. MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
  170. MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
  171. toLocation.name = endName;
  172. NSArray *items = @[currentLoc,toLocation];
  173. NSDictionary *dic = @{
  174. MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
  175. MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
  176. MKLaunchOptionsShowsTrafficKey : @(YES)
  177. };
  178. [MKMapItem openMapsWithItems:items launchOptions:dic];
  179. }
  180. /// 谷歌地图【未启用】
  181. -(void)openGoogleMapWithLat:(NSNumber *)lat lng:(NSNumber *)lng endName:(NSString *)endName {
  182. NSString *appName = [PublicObj getAppName];
  183. NSString *urlScheme = [NSBundle mainBundle].bundleIdentifier;
  184. NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&q=%@,%@&saddr=&daddr=%@&directionsmode=driving",appName,urlScheme,lat, lng,endName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  185. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  186. }
  187. /// 百度坐标转高德坐标
  188. -(NSArray *)bd09llToGCJLat:(double)blat lon:(double)blon {
  189. double X_PI = M_PI * 3000.0 / 180.0;
  190. double x = blon - 0.0065;
  191. double y = blat - 0.006;
  192. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * X_PI);
  193. double theta = atan2(y, x) - 0.000003 * cos(x * X_PI);
  194. double lat = z * sin(theta);
  195. double lon = z * cos(theta);
  196. NSArray *latlon = @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
  197. return latlon;
  198. }
  199. /// 高德坐标转百度坐标
  200. -(NSArray *)gcjToBd09llLat:(double)glat lon:(double)glon{
  201. double X_PI = M_PI * 3000.0 / 180.0;
  202. double x = glon;
  203. double y = glat;
  204. double z = sqrt(x * x + y * y) + 0.00002 * sin(y * X_PI);
  205. double theta = atan2(y, x) + 0.000003 * cos(x * X_PI);
  206. double lat = z * sin(theta) + 0.006;
  207. double lon = z * cos(theta) + 0.0065;
  208. NSArray *latlon = @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
  209. return latlon;
  210. }
  211. /*
  212. //百度坐标转高德(传入经度、纬度)
  213. function bd_decrypt(bd_lng, bd_lat) {
  214. var X_PI = Math.PI * 3000.0 / 180.0;
  215. var x = bd_lng - 0.0065;
  216. var y = bd_lat - 0.006;
  217. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
  218. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
  219. var gg_lng = z * Math.cos(theta);
  220. var gg_lat = z * Math.sin(theta);
  221. return {lng: gg_lng, lat: gg_lat}
  222. }*/
  223. /*
  224. //高德坐标转百度(传入经度、纬度)
  225. function bd_encrypt(gg_lng, gg_lat) {
  226. var X_PI = Math.PI * 3000.0 / 180.0;
  227. var x = gg_lng, y = gg_lat;
  228. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
  229. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
  230. var bd_lng = z * Math.cos(theta) + 0.0065;
  231. var bd_lat = z * Math.sin(theta) + 0.006;
  232. return {
  233. bd_lat: bd_lat,
  234. bd_lng: bd_lng
  235. };
  236. }
  237. */
  238. #pragma mark - 打开导航软件结束
  239. #pragma mark - 针对海外获取精确定位城市、翻译【利用腾讯api】开始
  240. -(void)updataTabbarWithCity:(NSString *)city {
  241. if([PublicObj checkNull:city]){
  242. return;
  243. }
  244. if ([city hasSuffix:@"市"]) {
  245. city = [city substringToIndex:city.length-1];
  246. }
  247. if (_fromEdit) {
  248. }else{
  249. dispatch_async(dispatch_get_main_queue(), ^{
  250. [PublicObj updataNewCity:city];
  251. });
  252. }
  253. }
  254. -(void)getCountyCodeLat:(NSString *)lat andLng:(NSString *)lng {
  255. /**
  256. https://apis.map.qq.com/ws/geocoder/v1/?
  257. location=36.178019,117.086293
  258. &get_poi=0
  259. &poi_options=address_format=short;radius=1000;page_size=20;page_index=1;policy=5
  260. &key=TJSBZ-DMWE4-UM5US-DXW7F-RVZRJ-GFFFQ
  261. */
  262. NSString *gdy_lang = @"en";
  263. if ([lagType isEqual:ZH_CN]) {
  264. gdy_lang = @"cn";
  265. }
  266. NSString *baseUrl = @"https://apis.map.qq.com/ws/geocoder/v1/?";
  267. NSDictionary *pullDic = @{
  268. @"location":[NSString stringWithFormat:@"%@,%@",lat,lng],
  269. @"key":TencentKey,
  270. @"get_poi":@"0",
  271. @"poi_options":@"address_format=short;radius=1000;page_size=20;page_index=1;policy=5",
  272. @"language":gdy_lang,
  273. };
  274. baseUrl = [baseUrl stringByAppendingFormat:@"%@",[self dealWithParam:pullDic]];
  275. baseUrl = [baseUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  276. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
  277. [request setHTTPMethod:@"GET"];
  278. /*
  279. //把字典中的参数进行拼接
  280. NSString *body = @"";
  281. NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
  282. //设置请求体
  283. [request setHTTPBody:bodyData];
  284. //设置本次请求的数据请求格式
  285. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  286. // 设置本次请求请求体的长度(因为服务器会根据你这个设定的长度去解析你的请求体中的参数内容)
  287. [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)bodyData.length] forHTTPHeaderField:@"Content-Length"];
  288. */
  289. //设置请求最长时间
  290. request.timeoutInterval = 15;
  291. NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  292. if (data) {
  293. //利用iOS自带原生JSON解析data数据 保存为Dictionary
  294. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  295. int status = [minstr([dict valueForKey:@"status"]) intValue];
  296. if (status == 0) {
  297. NSDictionary *resDic = [dict valueForKey:@"result"];
  298. NSDictionary *ad_info = [resDic valueForKey:@"ad_info"];
  299. NSString *nation_code = minstr([ad_info valueForKey:@"nation_code"]);
  300. [cityDefault saveGdyCountry:nation_code];
  301. }else{
  302. [cityDefault saveGdyCountry:@""];
  303. NSLog(@"======出错:%@\%@",dict,baseUrl);
  304. }
  305. }else{
  306. [cityDefault saveGdyCountry:@""];
  307. NSLog(@"======出错:未拿到数据\n%@",baseUrl);
  308. }
  309. }];
  310. [task resume];
  311. }
  312. #pragma mark - 城市
  313. -(void)getCityWithLat:(NSString *)lat andLng:(NSString *)lng andArray:(NSArray *)placemarks cusArray:(NSArray *)cusArray{
  314. NSString *baseUrl = @"https://apis.map.qq.com/ws/geocoder/v1/?";
  315. NSString *gdy_lang = @"en";
  316. if ([lagType isEqual:ZH_CN]) {
  317. gdy_lang = @"cn";
  318. }
  319. NSDictionary *pullDic = @{
  320. @"location":[NSString stringWithFormat:@"%@,%@",lat,lng],
  321. @"key":TencentKey,
  322. @"get_poi":@"0",
  323. @"poi_options":@"address_format=short;radius=1000;page_size=20;page_index=1;policy=5",
  324. @"language":gdy_lang,
  325. };
  326. baseUrl = [baseUrl stringByAppendingFormat:@"%@",[self dealWithParam:pullDic]];
  327. baseUrl = [baseUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  328. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
  329. [request setHTTPMethod:@"GET"];
  330. //设置请求最长时间
  331. request.timeoutInterval = 15;
  332. YBWeakSelf;
  333. NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  334. if (data) {
  335. //利用iOS自带原生JSON解析data数据 保存为Dictionary
  336. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  337. int status = [minstr([dict valueForKey:@"status"]) intValue];
  338. if (status == 0) {
  339. NSDictionary *resDic = [dict valueForKey:@"result"];
  340. NSDictionary *ad_info = [resDic valueForKey:@"ad_info"];
  341. NSString *nation_code = minstr([ad_info valueForKey:@"nation_code"]);
  342. NSDictionary *gdy_address_component = [resDic valueForKey:@"address_component"];
  343. NSString *gdy_city = @"";
  344. if ([nation_code isEqual:@"156"]) {
  345. // 国内
  346. gdy_city = minstr([gdy_address_component valueForKey:@"city"]);
  347. }else{
  348. // 国外
  349. gdy_city = minstr([gdy_address_component valueForKey:@"ad_level_3"]);
  350. if ([PublicObj checkNull:gdy_city]) {
  351. gdy_city = minstr([gdy_address_component valueForKey:@"ad_level_2"]);
  352. }
  353. if ([PublicObj checkNull:gdy_city]) {
  354. gdy_city = minstr([gdy_address_component valueForKey:@"ad_level_1"]);
  355. }
  356. }
  357. if (![PublicObj checkNull:gdy_city]) {
  358. [cityDefault saveLocationCity:gdy_city];
  359. if (weakSelf.lbsEvent) {
  360. weakSelf.lbsEvent(placemarks, @[cusArray[0],gdy_city,cusArray[2]]);
  361. }
  362. [weakSelf updataTabbarWithCity:gdy_city];
  363. }else{
  364. if (weakSelf.lbsEvent) {
  365. weakSelf.lbsEvent(placemarks, cusArray);
  366. }
  367. }
  368. }else{
  369. if (weakSelf.lbsEvent) {
  370. weakSelf.lbsEvent(placemarks, cusArray);
  371. }
  372. NSLog(@"======city出错:%@\%@",dict,baseUrl);
  373. }
  374. }else{
  375. if (weakSelf.lbsEvent) {
  376. weakSelf.lbsEvent(placemarks, cusArray);
  377. }
  378. NSLog(@"======city出错:未拿到数据\n%@",baseUrl);
  379. }
  380. }];
  381. [task resume];
  382. }
  383. #pragma mark -- 拼接参数
  384. -(NSString *)dealWithParam:(NSDictionary *)param {
  385. NSArray *allkeys = [param allKeys];
  386. NSMutableString *result = [NSMutableString string];
  387. for (NSString *key in allkeys) {
  388. NSString *string = [NSString stringWithFormat:@"%@=%@&", key, param[key]];
  389. [result appendString:string];
  390. }
  391. NSString *newStr = [result substringToIndex:(result.length-1)];
  392. return newStr;
  393. }
  394. #pragma mark - 周边
  395. -(void)txMapPoisWithCoordinate:(CLLocationCoordinate2D)coordinate andPage:(NSInteger)page complete:(TencentListBlock)locList;{
  396. NSString *lat = [NSString stringWithFormat:@"%f",coordinate.latitude];
  397. NSString *lng = [NSString stringWithFormat:@"%f",coordinate.longitude];
  398. NSString *baseUrl = @"https://apis.map.qq.com/ws/geocoder/v1/?";
  399. NSString *gdy_lang = @"en";
  400. if ([lagType isEqual:ZH_CN]) {
  401. gdy_lang = @"cn";
  402. }
  403. NSDictionary *pullDic = @{
  404. @"location":[NSString stringWithFormat:@"%@,%@",lat,lng],
  405. @"key":TencentKey,
  406. @"get_poi":@"1",
  407. @"poi_options":[NSString stringWithFormat:@"address_format=short;radius=1000;page_size=20;page_index=%ld;policy=5",(long)page],
  408. @"language":gdy_lang,
  409. };
  410. baseUrl = [baseUrl stringByAppendingFormat:@"%@",[self dealWithParam:pullDic]];
  411. baseUrl = [baseUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  412. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
  413. [request setHTTPMethod:@"GET"];
  414. //设置请求最长时间
  415. request.timeoutInterval = 15;
  416. NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  417. if (data) {
  418. //利用iOS自带原生JSON解析data数据 保存为Dictionary
  419. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  420. int status = [minstr([dict valueForKey:@"status"]) intValue];
  421. if (status == 0) {
  422. NSDictionary *resDic = [dict valueForKey:@"result"];
  423. NSLog(@"====:%@",resDic);
  424. NSArray *resArray = [resDic valueForKey:@"pois"];
  425. if (locList) {
  426. locList(resArray);
  427. }
  428. }else{
  429. if (locList) {
  430. locList(@[]);
  431. }
  432. NSLog(@"======周边出错:%@\%@",dict,baseUrl);
  433. }
  434. }else{
  435. if (locList) {
  436. locList(@[]);
  437. }
  438. NSLog(@"======周边出错:未拿到数据\n%@",baseUrl);
  439. }
  440. }];
  441. [task resume];
  442. }
  443. #pragma mark - 搜索地区
  444. -(void)txSearch:(NSString *)keys coordinate:(CLLocationCoordinate2D)coordinate andPage:(NSInteger)page complete:(TencentListBlock)locList;{
  445. /**
  446. https://apis.map.qq.com/ws/place/v1/search?
  447. keyword=万达
  448. &boundary=nearby(36.178032,117.086304,1000)
  449. &orderby=_distance
  450. &page_size=20
  451. &page_index=1
  452. &key=TJSBZ-DMWE4-UM5US-DXW7F-RVZRJ-GFFFQ
  453. &sig=c77652d4bcd47a14418511a4f45a829d
  454. &language=en
  455. */
  456. NSString *lat = [NSString stringWithFormat:@"%f",coordinate.latitude];
  457. NSString *lng = [NSString stringWithFormat:@"%f",coordinate.longitude];
  458. NSString *baseUrl = @"https://apis.map.qq.com/ws/place/v1/search?";
  459. NSString *gdy_lang = @"en";
  460. if ([lagType isEqual:ZH_CN]) {
  461. gdy_lang = @"cn";
  462. }
  463. NSDictionary *pullDic = @{
  464. @"keyword":keys,
  465. @"boundary":[NSString stringWithFormat:@"nearby(%@,%@,1000)",lat,lng],
  466. @"orderby":@"_distance",
  467. @"page_size":@"20",
  468. @"page_index":@(page),
  469. @"key":TencentKey,
  470. @"language":gdy_lang,
  471. };
  472. baseUrl = [baseUrl stringByAppendingFormat:@"%@",[self dealWithParam:pullDic]];
  473. baseUrl = [baseUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  474. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
  475. [request setHTTPMethod:@"GET"];
  476. //设置请求最长时间
  477. request.timeoutInterval = 15;
  478. NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  479. if (data) {
  480. //利用iOS自带原生JSON解析data数据 保存为Dictionary
  481. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  482. int status = [minstr([dict valueForKey:@"status"]) intValue];
  483. if (status == 0) {
  484. NSArray *resArray = [dict valueForKey:@"data"];
  485. if (locList) {
  486. locList(resArray);
  487. }
  488. }else{
  489. if (locList) {
  490. locList(@[]);
  491. }
  492. NSLog(@"======搜索出错:%@\%@",dict,baseUrl);
  493. }
  494. }else{
  495. if (locList) {
  496. locList(@[]);
  497. }
  498. NSLog(@"======搜索出错:未拿到数据\n%@",baseUrl);
  499. }
  500. }];
  501. [task resume];
  502. }
  503. #pragma mark - 针对海外获取精确定位城市、翻译【利用腾讯api】结束
  504. @end