JXLocationVC.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. //
  2. // JXLocationVC.m
  3. // CustomMKAnnotationView
  4. //
  5. // Created by Jian-Ye on 12-11-22.
  6. // Copyright (c) 2012年 Jian-Ye. All rights reserved.
  7. //
  8. #import "JXLocationVC.h"
  9. #import "JXMapData.h"
  10. #import "admobViewController.h"
  11. #import "JXPlaceMarkModel.h"
  12. #import "JXNearMarkCell.h"
  13. #import "SPAlertController.h"
  14. @interface JXLocationVC()<JXActionSheetVCDelegate> {
  15. }
  16. @property (nonatomic, strong) JXPlaceMarkModel*model;
  17. @property (nonatomic, strong) JXNearMarkCell *lastCell;
  18. @end
  19. @implementation JXLocationVC
  20. - (id)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. self.title = Localized(@"JXUserInfoVC_Loation");
  25. self.heightHeader = _locationType ==JXLocationTypeShowStaticLocation ? 0 : JX_SCREEN_TOP;
  26. self.heightFooter = 0;
  27. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  28. self.isGotoBack = YES;
  29. // 定位回调或反编码回调前,self被销毁会造成闪退
  30. self.isFreeOnClose = NO;
  31. if (!_locations)
  32. _locations = [[NSMutableArray alloc]init];
  33. _address = g_server.address;
  34. _nearMarkArray = [[NSMutableArray alloc] init];
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. [self createHeadAndFoot];
  41. [self initBaiduMapView];
  42. [self customView];
  43. self.tableBody.contentSize = CGSizeMake(0, JX_SCREEN_HEIGHT - JX_SCREEN_TOP);
  44. // _baiduMapView.showsUserLocation = YES;//显示定位图层
  45. // [_baiduMapView updateLocationData:userLocation];
  46. }
  47. - (void)viewWillAppear:(BOOL)animated
  48. {
  49. [super viewWillAppear:animated];
  50. [_baiduMapView viewWillAppear];
  51. _baiduMapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  52. }
  53. -(void)viewWillDisappear:(BOOL)animated
  54. {
  55. [super viewWillDisappear:animated];
  56. [_baiduMapView viewWillDisappear];
  57. _baiduMapView.delegate = nil; // 不用时,置nil
  58. }
  59. -(void)viewDidAppear:(BOOL)animated{
  60. [super viewDidAppear:animated];
  61. if (_locationType == JXLocationTypeShowStaticLocation) {
  62. JXMapData * staticData = [_locations firstObject];
  63. [self makeMapCenter:staticData];
  64. [self addPointAnnotations:_locations];
  65. self.title = staticData.title;
  66. }else {
  67. //定位服务
  68. if (_locService == nil) {
  69. _locService = [[BMKLocationService alloc] init];
  70. _locService.delegate = self;
  71. [_locService setDesiredAccuracy:kCLLocationAccuracyBest];
  72. }
  73. [_locService startUserLocationService];
  74. }
  75. }
  76. - (void)customView{
  77. if (_locationType == JXLocationTypeShowStaticLocation) {
  78. UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, JX_SCREEN_TOP - 38, 31, 31)];
  79. [backBtn setBackgroundImage:[UIImage imageNamed:@"map_back"] forState:UIControlStateNormal];
  80. [backBtn addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
  81. [self.view addSubview:backBtn];
  82. UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-100, JX_SCREEN_WIDTH, 100)];
  83. baseView.backgroundColor = [UIColor whiteColor];
  84. [self.view addSubview:baseView];
  85. UILabel *adrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, JX_SCREEN_WIDTH-20*3-60, 20)];
  86. adrLabel.text = self.placeNames;
  87. [baseView addSubview:adrLabel];
  88. UIButton* btn = [UIFactory createButtonWithImage:@"adress_navigation" highlight:nil target:self selector:@selector(moreBtnAction)];
  89. btn.frame = CGRectMake(CGRectGetMaxX(adrLabel.frame)+20, 20, 60, 60);
  90. [baseView addSubview:btn];
  91. //复位
  92. UIButton * resetLoca = [[UIButton alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH -80+30-15, JX_SCREEN_HEIGHT -100-50, 30, 30)];
  93. [resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
  94. [resetLoca addTarget:self action:@selector(resetMapCenter) forControlEvents:UIControlEventTouchUpInside];
  95. [self.view addSubview:resetLoca];
  96. }else{
  97. //发送
  98. if(self.isSend){
  99. _sendButton = [UIFactory createCommonButton:self.isCircleCome ? Localized(@"JX_Confirm") : Localized(@"JX_Send") target:self action:@selector(onSelect)];
  100. _sendButton.frame = CGRectMake(JX_SCREEN_WIDTH - 15-32, JX_SCREEN_TOP - 30, 32, 15);
  101. [self.tableHeader addSubview:_sendButton];
  102. [_sendButton setBackgroundImage:nil forState:UIControlStateNormal];
  103. [_sendButton setBackgroundImage:nil forState:UIControlStateHighlighted];
  104. [_sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  105. _sendButton.titleLabel.font = SYSFONT(15);
  106. _sendButton.hidden = YES;
  107. // _sendButton.userInteractionEnabled = NO;
  108. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  109. // _sendButton.userInteractionEnabled = YES;
  110. // });
  111. }
  112. //复位
  113. UIButton * resetLoca = [[UIButton alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH -50, JX_SCREEN_HEIGHT -50, 30, 30)];
  114. [resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
  115. [resetLoca addTarget:self action:@selector(resetLocation) forControlEvents:UIControlEventTouchUpInside];
  116. [self.view addSubview:resetLoca];
  117. //当前位置图标
  118. UIImageView * pointImage = [[UIImageView alloc]initWithFrame:CGRectMake((JX_SCREEN_WIDTH - 30) /2, (JX_SCREEN_HEIGHT -64)/2 - 50 +10 +64, 30, 50)];
  119. pointImage.image = [UIImage imageNamed:@"position"];
  120. pointImage.userInteractionEnabled = NO;
  121. [self.view addSubview:pointImage];
  122. //周边地点列表
  123. _nearMarkTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT - 200, JX_SCREEN_WIDTH, 200) style:UITableViewStylePlain];
  124. _nearMarkTableView.dataSource = self;
  125. _nearMarkTableView.delegate = self;
  126. _nearMarkTableView.separatorStyle = UITableViewCellSelectionStyleNone;
  127. [self.view addSubview:_nearMarkTableView];
  128. }
  129. }
  130. //复位按钮响应事件
  131. -(void)resetLocation{
  132. if (_locService.userLocation.location == nil) {
  133. [g_App showAlert:Localized(@"JXLoc_StartLocNotice")];
  134. return;
  135. }
  136. BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(_locService.userLocation.location.coordinate, BMKCoordinateSpanMake(0.0001f,0.0001f));
  137. BMKCoordinateRegion adjustedRegion = [_baiduMapView regionThatFits:viewRegion];
  138. [_baiduMapView setRegion:adjustedRegion animated:YES];
  139. }
  140. -(void)resetMapCenter{
  141. [self makeMapCenter:[_locations firstObject]];
  142. }
  143. - (void)initBaiduMapView{
  144. _baiduMapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.tableBody.frame.size.width, self.tableBody.frame.size.height)];
  145. _baiduMapView.zoomLevel = 13;
  146. _baiduMapView.minZoomLevel = 6;
  147. _baiduMapView.showsUserLocation = YES;
  148. [self.tableBody addSubview:_baiduMapView];
  149. }
  150. -(void)addPointAnnotations:(NSArray *)locArray{
  151. // 在地图中添加PointAnnotation
  152. for (JXMapData * mapData in locArray) {
  153. CLLocationCoordinate2D coor = [mapData coordinate2D];
  154. BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc]init];
  155. annotation.title = mapData.title;
  156. annotation.subtitle = mapData.subtitle;
  157. annotation.coordinate = coor;
  158. [_baiduMapView addAnnotation:annotation];
  159. }
  160. }
  161. -(void)makeMapCenter:(JXMapData *)centerData{
  162. CLLocationCoordinate2D coor = [centerData coordinate2D];
  163. BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coor, BMKCoordinateSpanMake(0.0001f,0.0001f));
  164. BMKCoordinateRegion adjustedRegion = [_baiduMapView regionThatFits:viewRegion];
  165. [_baiduMapView setRegion:adjustedRegion animated:YES];
  166. }
  167. #pragma mark BMKLocationServiceDelegate
  168. - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
  169. {
  170. //定位到当前位置
  171. CLLocationCoordinate2D coor;
  172. if (self.latitude == 0 && self.longitude ==0) {
  173. if (userLocation.location == nil) {
  174. [g_App showAlert:Localized(@"JXLoc_StartLocNotice")];
  175. return;
  176. }
  177. self.latitude = userLocation.location.coordinate.latitude;
  178. self.longitude = userLocation.location.coordinate.longitude;
  179. coor = userLocation.location.coordinate;
  180. }else{
  181. coor = (CLLocationCoordinate2D){self.longitude,self.latitude};
  182. }
  183. if (userLocation) {
  184. [_locService stopUserLocationService];
  185. }
  186. // _annotation.coordinate = coor;
  187. ///表示一个经纬度区域
  188. BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coor, BMKCoordinateSpanMake(0.0001f,0.0001f));
  189. BMKCoordinateRegion adjustedRegion = [_baiduMapView regionThatFits:viewRegion];
  190. [_baiduMapView setRegion:adjustedRegion animated:YES];
  191. //设置地图中心为用户经纬度
  192. // [_mapView updateLocationData:userLocation];
  193. if (_reverseGeoCodeOption==nil) {
  194. _reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
  195. }
  196. if (_geoCodeSearch==nil) {
  197. //初始化地理编码,
  198. _geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
  199. _geoCodeSearch.delegate = self;
  200. }
  201. //返回当前位置附近的信息
  202. _reverseGeoCodeOption.reverseGeoPoint = coor;
  203. [_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];
  204. }
  205. #pragma mark BMKMapViewDelegate
  206. - (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
  207. {
  208. if (_locationType == JXLocationTypeShowStaticLocation) {
  209. }else{
  210. CLLocationCoordinate2D mapCoordinate= mapView.getMapStatus.targetGeoPt;
  211. self.latitude = mapCoordinate.latitude;
  212. self.longitude = mapCoordinate.longitude;
  213. NSLog(@"精度%f 维度%f %d",self.latitude,self.longitude,self.tableBody.userInteractionEnabled);
  214. // CLLocationCoordinate2D coor;
  215. // coor = (CLLocationCoordinate2D){self.locX,self.locY};
  216. // _annotation.coordinate = coor;
  217. // _reverseGeoCodeOption.reverseGeoPoint = (CLLocationCoordinate2D){114.060456,22.615227};
  218. _reverseGeoCodeOption.reverseGeoPoint = mapCoordinate;
  219. BOOL flag = [_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];
  220. if(flag)
  221. {
  222. NSLog(@"反geo检索发送成功");
  223. }else
  224. {
  225. NSLog(@"反geo检索发送失败");
  226. }
  227. }
  228. }
  229. //-(void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status{
  230. //// NSLog(@"%f,%f",status.targetScreenPt.x,status.targetScreenPt.y);
  231. // NSLog(@"targetGeoPt:%f,%f",status.targetGeoPt.latitude,status.targetGeoPt.longitude);
  232. // _annotation.coordinate = status.targetGeoPt;
  233. //}
  234. //返回反地理编码搜索结果
  235. - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
  236. // [_address release];
  237. _address = [[NSString alloc]initWithFormat:@"%@",result.address];
  238. NSLog(@"%@",result.addressDetail);
  239. //获取周边用户信息
  240. _sendButton.hidden = NO;
  241. if (error==BMK_SEARCH_NO_ERROR) {
  242. [_nearMarkArray removeAllObjects];
  243. for(BMKPoiInfo *poiInfo in result.poiList)
  244. {
  245. [_nearMarkArray addObject:[JXPlaceMarkModel modelByBMKPoiInfo:poiInfo]];
  246. }
  247. _selIndex = 0;
  248. if (!_nearMarkArray || _nearMarkArray.count <= 0) {
  249. return;
  250. }
  251. JXPlaceMarkModel*model = [_nearMarkArray objectAtIndex:0];
  252. NSLog(@"%@",model);
  253. model.address = result.address;
  254. if (result.sematicDescription.length > 0) {
  255. model.name = result.sematicDescription;
  256. }
  257. model.latitude = result.location.latitude;
  258. model.longitude = result.location.longitude;
  259. self.model = model;
  260. //
  261. [_nearMarkTableView reloadData];
  262. [_locService stopUserLocationService];
  263. }else{
  264. NSLog(@"BMKSearchErrorCode: %u",error);
  265. }
  266. }
  267. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
  268. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  269. BMKPinAnnotationView *myAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; //初始化一个大头针标注
  270. myAnnotationView.pinColor = BMKPinAnnotationColorGreen;
  271. myAnnotationView.animatesDrop = YES;
  272. // myAnnotationView.draggable = YES;
  273. myAnnotationView.image = [UIImage imageNamed:@"position"];
  274. return myAnnotationView;
  275. }
  276. return nil;
  277. }
  278. -(void)dealloc{
  279. NSLog(@"JXLocationVC dealloc");
  280. [_locations removeAllObjects];
  281. _locations = nil;
  282. _delegate = nil;
  283. }
  284. -(void)onSelect{
  285. NSString *imagePath = [self screenShotAction];
  286. JXMapData * data = [[JXMapData alloc]init];
  287. if (_model != nil) {
  288. data.latitude = [NSString stringWithFormat:@"%f",_model.latitude];
  289. data.longitude = [NSString stringWithFormat:@"%f",_model.longitude];
  290. data.subtitle = _model.name;
  291. }else{
  292. data.latitude = [NSString stringWithFormat:@"%f",self.latitude];
  293. data.longitude = [NSString stringWithFormat:@"%f",self.longitude];
  294. data.subtitle = _address;
  295. }
  296. if (!data.subtitle || data.subtitle.length <= 0 || [data.subtitle isEqualToString:@"null"] || [data.subtitle isEqualToString:@"(null)"]) {
  297. if (g_server.address.length > 0) {
  298. data.subtitle = g_server.address;
  299. }else {
  300. data.subtitle = @"";
  301. }
  302. }
  303. data.imageUrl = imagePath;
  304. if(self.delegate != nil && [self.delegate respondsToSelector:self.didSelect])
  305. [self.delegate performSelectorOnMainThread:self.didSelect withObject:data waitUntilDone:NO];
  306. [self actionQuit];
  307. _delegate = nil;
  308. [_baiduMapView removeFromSuperview];
  309. // _pSelf = nil;
  310. }
  311. -(NSString *)screenShotAction{
  312. // BOOL su = [_baiduMapView zoomOut];
  313. // UIImage *image = [_baiduMapView takeSnapshot:CGRectMake(0, (CGRectGetHeight(_baiduMapView.frame)-JX_SCREEN_WIDTH/2.2)/2, JX_SCREEN_WIDTH-100, JX_SCREEN_WIDTH/2.2-100)];
  314. // UIImage *logoImage = [self addImageLogo:image text:[UIImage imageNamed:@"position"]];
  315. // //写入文件
  316. // NSString* filePath = [FileInfo getUUIDFileName:@"jpg"];
  317. // [g_server saveImageToFile:logoImage file:filePath isOriginal:NO];
  318. UIImage *image = [self snapshotToImage:CGSizeMake(JX_SCREEN_WIDTH, JX_SCREEN_WIDTH/2.2)];
  319. UIImage *logoImage = [self addImageLogo:image text:[UIImage imageNamed:@"position"]];
  320. NSString* filePath = [FileInfo getUUIDFileName:@"jpg"];
  321. [g_server saveImageToFile:logoImage file:filePath isOriginal:NO];
  322. return filePath;
  323. }
  324. - (nullable UIImage *)snapshotToImage:(CGSize)size {
  325. CGPoint map_center = _baiduMapView.center;
  326. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
  327. [_baiduMapView drawViewHierarchyInRect:CGRectMake(0, -(map_center.y - size.height)-JX_SCREEN_TOP, _baiduMapView.bounds.size.width, _baiduMapView.bounds.size.height) afterScreenUpdates:YES];
  328. UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  329. UIGraphicsEndImageContext();
  330. return snapshotImage;
  331. }
  332. -(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
  333. {
  334. int w = img.size.width;
  335. int h = img.size.height;
  336. int logoWidth = 40;
  337. int logoHeight = 80;
  338. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  339. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  340. CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  341. CGContextDrawImage(context, CGRectMake((w-logoWidth)/2, h/2-10, logoWidth, logoHeight), [logo CGImage]);
  342. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  343. CGContextRelease(context);
  344. CGColorSpaceRelease(colorSpace);
  345. return [UIImage imageWithCGImage:imageMasked];
  346. }
  347. - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index {
  348. if (index == 0) {
  349. [self onDaoHangForIOSMap];
  350. }
  351. }
  352. -(void)moreBtnAction{
  353. JXActionSheetVC *actionVC = [[JXActionSheetVC alloc] initWithImages:nil names:@[Localized(@"JX_MapApple")]];
  354. actionVC.delegate = self;
  355. [g_navigation.subViews.lastObject presentViewController:actionVC animated:NO completion:nil];
  356. // SPAlertController *alertSheet = [SPAlertController alertControllerWithTitle:nil message:nil preferredStyle:SPAlertControllerStyleActionSheet animationType:SPAlertAnimationTypeDefault];
  357. //
  358. //// UIAlertController * alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  359. //// SPAlertAction *actionApple = [SPAlertAction actionWithTitle:Localized(@"JX_MapApple") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  360. //// NSLog(@"苹果");
  361. //// [self onDaoHangForIOSMap];
  362. //// }];
  363. //// [alertSheet addAction:actionApple];
  364. //
  365. // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
  366. // SPAlertAction * actionAMap = [SPAlertAction actionWithTitle:Localized(@"JX_MapGaode") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  367. // NSLog(@"高德");
  368. // [self onDaoHangForGaoDeMap];
  369. // }];
  370. // [alertSheet addAction:actionAMap];
  371. // }
  372. // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
  373. // SPAlertAction * actionBaidu = [SPAlertAction actionWithTitle:Localized(@"JX_MapBaidu") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  374. // NSLog(@"百度");
  375. // [self onDaoHangForBaiDuMap];
  376. // }];
  377. // [alertSheet addAction:actionBaidu];
  378. // }
  379. //
  380. // if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
  381. // SPAlertAction *actionGoogle = [SPAlertAction actionWithTitle:Localized(@"JX_MapGoogle") style:SPAlertActionStyleDefault handler:^(SPAlertAction *action) {
  382. // NSLog(@"谷歌");
  383. // [self onGoogleMap];
  384. // }];
  385. // [alertSheet addAction:actionGoogle];
  386. // }
  387. //
  388. //// if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
  389. //// UIAlertAction *actionQq = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  390. //// NSLog(@"腾讯地图");
  391. //// [self onQQMap];
  392. //// }];
  393. //// [alertSheet addAction:actionQq];
  394. //// }
  395. //
  396. // SPAlertAction * cancelAction = [SPAlertAction actionWithTitle:Localized(@"JX_Cencal") style:SPAlertActionStyleCancel handler:^(SPAlertAction * _Nonnull action) {
  397. // NSLog(@"取消");
  398. // }];
  399. // [alertSheet addAction:cancelAction];
  400. //
  401. //
  402. //// if ([alertSheet respondsToSelector:@selector(popoverPresentationController)]) {
  403. //// alertSheet.popoverPresentationController.sourceView = self.view; //必须加
  404. //// alertSheet.popoverPresentationController.sourceRect = CGRectMake(0, JX_SCREEN_HEIGHT, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);//可选,我这里加这句代码是为了调整到合适的位置
  405. //// }
  406. // [self presentViewController:alertSheet animated:YES completion:nil];
  407. }
  408. #pragma mark ------------------------------ 导航 - iosMap
  409. -(void) onDaoHangForIOSMap
  410. {
  411. //起点
  412. // CLLocation * location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
  413. // location = [location locationMarsFromBaidu];
  414. //
  415. // CLLocationCoordinate2D coor =location.coordinate;
  416. // MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor addressDictionary:nil]];
  417. // currentLocation.name =@"我的位置";
  418. JXMapData * staticData = [_locations firstObject];
  419. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  420. // self.title = staticData.title;
  421. //目的地的位置
  422. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  423. // location2 = [location2 locationMarsFromBaidu];
  424. CLLocationCoordinate2D coor2 =location2.coordinate;
  425. // CLLocationCoordinate2D coords = self.location;
  426. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];
  427. toLocation.name = staticData.title;
  428. NSArray *items = [NSArray arrayWithObjects:toLocation, nil];
  429. NSString * mode = MKLaunchOptionsDirectionsModeDriving;
  430. // switch (_seleIndex) {
  431. // case 1:
  432. // {
  433. // mode = MKLaunchOptionsDirectionsModeTransit;
  434. // }
  435. // break;
  436. // case 2:
  437. // {
  438. // mode = MKLaunchOptionsDirectionsModeDriving;
  439. // }
  440. // break;
  441. // case 3:
  442. // {
  443. // mode = MKLaunchOptionsDirectionsModeWalking;
  444. // }
  445. // break;
  446. //
  447. // default:
  448. // break;
  449. // }
  450. NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:mode, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
  451. //打开苹果自身地图应用,并呈现特定的item
  452. [MKMapItem openMapsWithItems:items launchOptions:options];
  453. }
  454. #pragma mark ------------------------------ 导航 - 高德
  455. -(void) onDaoHangForGaoDeMap
  456. {
  457. // m 驾车:0:速度最快,1:费用最少,2:距离最短,3:不走高速,4:躲避拥堵,5:不走高速且避免收费,6:不走高速且躲避拥堵,7:躲避收费和拥堵,8:不走高速躲避收费和拥堵 公交:0:最快捷,2:最少换乘,3:最少步行,5:不乘地铁 ,7:只坐地铁 ,8:时间短 是
  458. // t = 0:驾车 =1:公交 =2:步行
  459. // NSString * t = @"0";
  460. // switch (_seleIndex) {
  461. // case 1:
  462. // {
  463. // t = @"1";
  464. // }
  465. // break;
  466. // case 2:
  467. // {
  468. // t = @"0";
  469. // }
  470. // break;
  471. // case 3:
  472. // {
  473. // t = @"2";
  474. // }
  475. // break;
  476. //
  477. // default:
  478. // break;
  479. // }
  480. //起点
  481. // CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
  482. // location = [location locationMarsFromBaidu];
  483. //
  484. // CLLocationCoordinate2D coor =location.coordinate;
  485. //目的地的位置
  486. // CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
  487. // location2 = [location2 locationMarsFromBaidu];
  488. //
  489. // CLLocationCoordinate2D coor2 =location2.coordinate;
  490. JXMapData * staticData = [_locations firstObject];
  491. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  492. // self.title = staticData.title;
  493. //目的地的位置
  494. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  495. // location2 = [location2 locationMarsFromBaidu];
  496. CLLocationCoordinate2D coor2 =location2.coordinate;
  497. // 导航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"
  498. // 路径规划 URL:iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0
  499. // -- 不能直接让用户进入导航,应该给用户更多的选择,所以先进行路径规划
  500. // NSURL *myLocationScheme = [NSURL URLWithString:@"iosamap://path?sourceApplication=shikuim"];
  501. NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=shikuim&sid=BGVIS1&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0",coor2.latitude,coor2.longitude,staticData.title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  502. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 iosamap
  503. {
  504. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  505. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  506. NSLog(@"scheme调用结束"); }];
  507. }else { //iOS10以前,使用旧API
  508. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  509. }
  510. }else{
  511. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  512. }
  513. }
  514. #pragma mark ------------------------------ 导航 - 百度
  515. -(void) onDaoHangForBaiDuMap
  516. {
  517. // 百度地图如何调起APP进行导航
  518. // mode 导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行
  519. NSString * modeBaiDu = @"driving";
  520. // switch (_seleIndex) {
  521. // case 1:
  522. // {
  523. // modeBaiDu = @"transit";
  524. // }
  525. // break;
  526. // case 2:
  527. // {
  528. // modeBaiDu = @"driving";
  529. // }
  530. // break;
  531. // case 3:
  532. // {
  533. // modeBaiDu = @"walking";
  534. // }
  535. // break;
  536. //
  537. // default:
  538. // break;
  539. // }
  540. JXMapData * staticData = [_locations firstObject];
  541. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  542. // self.title = staticData.title;
  543. //目的地的位置
  544. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  545. // location2 = [location2 locationMarsFromBaidu];
  546. CLLocationCoordinate2D coor2 =location2.coordinate;
  547. NSString *url = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%lf,%lf&destination=%f,%f&mode=%@&src=公司|APP",coor2.latitude,coor2.longitude,coor2.latitude,coor2.longitude,modeBaiDu] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
  548. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  549. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。
  550. {
  551. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  552. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  553. NSLog(@"scheme调用结束");
  554. }];
  555. }else { //iOS10以前,使用旧API
  556. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  557. }
  558. }else{
  559. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  560. }
  561. }
  562. #pragma mark --- google
  563. -(void)onGoogleMap{
  564. JXMapData * staticData = [_locations firstObject];
  565. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  566. // self.title = staticData.title;
  567. //目的地的位置
  568. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  569. // location2 = [location2 locationMarsFromBaidu];
  570. CLLocationCoordinate2D coor2 =location2.coordinate;
  571. NSString *url = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"shikuim",coor2.latitude, coor2.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  572. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。
  573. {
  574. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  575. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  576. NSLog(@"scheme调用结束"); }];
  577. }else { //iOS10以前,使用旧API
  578. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  579. }
  580. }else{
  581. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  582. }
  583. }
  584. //#pragma mark ---qq
  585. //-(void)onQQMap{
  586. // JXMapData * staticData = [_locations firstObject];
  587. // CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  588. // // self.title = staticData.title;
  589. // //目的地的位置
  590. // CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  591. // // location2 = [location2 locationMarsFromBaidu];
  592. //
  593. // CLLocationCoordinate2D coor2 =location2.coordinate;
  594. //
  595. // NSString *url = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=%@&coord_type=1&policy=0",coor2.latitude, coor2.longitude,staticData.title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  596. // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]])
  597. // {
  598. // if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  599. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  600. // NSLog(@"scheme调用结束"); }];
  601. // }else { //iOS10以前,使用旧API
  602. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  603. // }
  604. // }else{
  605. // [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  606. // }
  607. //}
  608. #pragma mark tableViewDelegate
  609. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  610. {
  611. return _nearMarkArray.count;
  612. }
  613. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  614. {
  615. JXNearMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JXNearMarkCell class])];
  616. if (cell == nil) {
  617. cell = [[JXNearMarkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JXNearMarkCell class])];
  618. }
  619. [cell refreshWithModel:[_nearMarkArray objectAtIndex:indexPath.row]];
  620. return cell;
  621. }
  622. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  623. [_locService stopUserLocationService];
  624. _selIndex = indexPath.row;
  625. JXPlaceMarkModel*model = [_nearMarkArray objectAtIndex:indexPath.row];
  626. // _locationTF.text = model.address;
  627. //
  628. self.model = model;
  629. JXNearMarkCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  630. cell.selFlag.hidden = NO;
  631. if (self.lastCell != cell) {
  632. self.lastCell.selFlag.hidden = YES;
  633. }
  634. // self.textString = model.address;
  635. if (self.lastCell != cell) {
  636. self.lastCell = cell;
  637. }
  638. }
  639. #pragma mark 设置cell分割线做对齐
  640. -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
  641. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  642. [cell setLayoutMargins:UIEdgeInsetsZero];
  643. }
  644. if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
  645. [cell setSeparatorInset:UIEdgeInsetsZero];
  646. }
  647. }
  648. -(void)viewDidLayoutSubviews {
  649. if ([_nearMarkTableView respondsToSelector:@selector(setSeparatorInset:)]) {
  650. [_nearMarkTableView setSeparatorInset:UIEdgeInsetsZero];
  651. }
  652. if ([_nearMarkTableView respondsToSelector:@selector(setLayoutMargins:)]) {
  653. [_nearMarkTableView setLayoutMargins:UIEdgeInsetsZero];
  654. }
  655. }
  656. @end