JXGoogleMapVC.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. //
  2. // JXGoogleMapVC.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2018/8/20.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXGoogleMapVC.h"
  9. #import <GoogleMaps/GoogleMaps.h>
  10. #import <GoogleMaps/GoogleMaps.h>
  11. #import <CoreLocation/CoreLocation.h>
  12. #import "JXNearMarkCell.h"
  13. #import "JXMapData.h"
  14. #import "SPAlertController.h"
  15. @interface JXGoogleMapVC () <GMSMapViewDelegate,UIScrollViewDelegate,CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource,JXActionSheetVCDelegate>
  16. @property (nonatomic, strong) GMSMapView *gooMapView;
  17. @property (nonatomic, strong) CLLocationManager *locationManager;
  18. @property (nonatomic, strong) GMSMarker *marker;
  19. @property (nonatomic, strong) GMSGeocoder *geocoder;
  20. @property (nonatomic, strong) GMSMutableCameraPosition *camera;
  21. @property (nonatomic, strong) NSMutableArray *nearMarkArray;
  22. @property (nonatomic, strong) JXPlaceMarkModel *model;
  23. @property (nonatomic, assign) CGRect frame;
  24. @property (nonatomic, strong) JXNearMarkCell *lastCell;
  25. @end
  26. @implementation JXGoogleMapVC
  27. - (instancetype)init {
  28. if (self = [super init]) {
  29. self.title = Localized(@"JXUserInfoVC_Loation");
  30. self.heightHeader = _locationType ==JXGooLocationTypeShowStaticLocation ? 0 : JX_SCREEN_TOP;
  31. self.heightFooter = 0;
  32. self.isGotoBack = YES;
  33. [self createHeadAndFoot];
  34. _nearMarkArray = [[NSMutableArray alloc] init];
  35. if (_locationType ==JXGooLocationTypeShowStaticLocation) {
  36. UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, JX_SCREEN_TOP - 38, 31, 31)];
  37. [backBtn setBackgroundImage:[UIImage imageNamed:@"map_back"] forState:UIControlStateNormal];
  38. [backBtn addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
  39. [self.view addSubview:backBtn];
  40. // UIButton* btn = [UIFactory createButtonWithImage:@"title_more" highlight:nil target:self selector:@selector(moreBtnAction)];
  41. // btn.frame = CGRectMake(JX_SCREEN_WIDTH-24-8, JX_SCREEN_TOP - 34, 24, 24);
  42. // [self.tableHeader addSubview:btn];
  43. UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-100, JX_SCREEN_WIDTH, 100)];
  44. baseView.backgroundColor = [UIColor whiteColor];
  45. [self.view addSubview:baseView];
  46. UILabel *adrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, JX_SCREEN_WIDTH-20*3-60, 20)];
  47. adrLabel.text = self.placeNames;
  48. [baseView addSubview:adrLabel];
  49. UIButton* btn = [UIFactory createButtonWithImage:@"adress_navigation" highlight:nil target:self selector:@selector(moreBtnAction)];
  50. btn.frame = CGRectMake(CGRectGetMaxX(adrLabel.frame)+20, 20, 60, 60);
  51. [baseView addSubview:btn];
  52. //复位
  53. UIButton * resetLoca = [[UIButton alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH -80+30-15, JX_SCREEN_HEIGHT -100-50, 30, 30)];
  54. [resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
  55. [resetLoca addTarget:self action:@selector(resetMapCenter) forControlEvents:UIControlEventTouchUpInside];
  56. [self.view addSubview:resetLoca];
  57. } else {
  58. //发送
  59. if(self.isSend){
  60. _sendButton = [UIFactory createCommonButton:Localized(@"JX_Send") target:self action:@selector(onSelect)];
  61. _sendButton.frame = CGRectMake(JX_SCREEN_WIDTH-60, JX_SCREEN_TOP - 34, 60, 24);
  62. [_sendButton setBackgroundImage:nil forState:UIControlStateNormal];
  63. [_sendButton setBackgroundImage:nil forState:UIControlStateHighlighted];
  64. [_sendButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
  65. [self.tableHeader addSubview:_sendButton];
  66. }
  67. }
  68. if (!_locations)
  69. _locations = [[NSMutableArray alloc]init];
  70. }
  71. return self;
  72. }
  73. - (void)viewDidLoad {
  74. [super viewDidLoad];
  75. [self createGoogleMap];
  76. [self customView];
  77. [self startLocation];
  78. self.tableBody.contentSize = CGSizeMake(0, JX_SCREEN_HEIGHT - JX_SCREEN_TOP);
  79. }
  80. -(void)startLocation{
  81. if (_locationType == JXGooLocationTypeShowStaticLocation) {
  82. JXMapData * staticData = [_locations firstObject];
  83. self.latitude = [staticData.latitude doubleValue];
  84. self.longitude = [staticData.longitude doubleValue];
  85. // [self makeMapCenter:staticData];
  86. // [self addPointAnnotations:_locations];
  87. self.title = staticData.title;
  88. GMSMarker *marker = [[GMSMarker alloc] init];
  89. marker.position = CLLocationCoordinate2DMake(self.latitude, self.longitude);
  90. marker.title = self.title;
  91. // marker.snippet = @"Hong Kong";
  92. marker.icon = [UIImage imageNamed:@"position"];
  93. marker.map = _gooMapView;
  94. [self resetMapCenter]; // 移动镜头
  95. }else {
  96. //定位服务
  97. _locationManager=[[CLLocationManager alloc]init];
  98. if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
  99. [_locationManager requestWhenInUseAuthorization];
  100. }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
  101. _locationManager.delegate=self;
  102. _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
  103. [_locationManager startUpdatingLocation];
  104. }
  105. }
  106. }
  107. - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index {
  108. if (index == 0) {
  109. [self onDaoHangForIOSMap];
  110. }
  111. }
  112. -(void)moreBtnAction{
  113. JXActionSheetVC *actionVC = [[JXActionSheetVC alloc] initWithImages:nil names:@[Localized(@"JX_MapApple")]];
  114. actionVC.delegate = self;
  115. [g_navigation.subViews.lastObject presentViewController:actionVC animated:NO completion:nil];
  116. // SPAlertController *alertSheet = [SPAlertController alertControllerWithTitle:nil message:nil preferredStyle:SPAlertControllerStyleActionSheet animationType:SPAlertAnimationTypeDefault];
  117. //
  118. // // UIAlertController * alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  119. //// SPAlertAction *actionApple = [SPAlertAction actionWithTitle:Localized(@"JX_MapApple") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  120. //// NSLog(@"苹果");
  121. //// [self onDaoHangForIOSMap];
  122. //// }];
  123. //// [alertSheet addAction:actionApple];
  124. //
  125. // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
  126. // SPAlertAction * actionAMap = [SPAlertAction actionWithTitle:Localized(@"JX_MapGaode") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  127. // NSLog(@"高德");
  128. // [self onDaoHangForGaoDeMap];
  129. // }];
  130. // [alertSheet addAction:actionAMap];
  131. // }
  132. // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
  133. // SPAlertAction * actionBaidu = [SPAlertAction actionWithTitle:Localized(@"JX_MapBaidu") style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  134. // NSLog(@"百度");
  135. // [self onDaoHangForBaiDuMap];
  136. // }];
  137. // [alertSheet addAction:actionBaidu];
  138. // }
  139. //
  140. // if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
  141. // SPAlertAction *actionGoogle = [SPAlertAction actionWithTitle:Localized(@"JX_MapGoogle") style:SPAlertActionStyleDefault handler:^(SPAlertAction *action) {
  142. // NSLog(@"谷歌");
  143. // [self onGoogleMap];
  144. // }];
  145. // [alertSheet addAction:actionGoogle];
  146. // }
  147. //
  148. // // if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
  149. // // UIAlertAction *actionQq = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  150. // // NSLog(@"腾讯地图");
  151. // // [self onQQMap];
  152. // // }];
  153. // // [alertSheet addAction:actionQq];
  154. // // }
  155. //
  156. // SPAlertAction * cancelAction = [SPAlertAction actionWithTitle:Localized(@"JX_Cencal") style:SPAlertActionStyleCancel handler:^(SPAlertAction * _Nonnull action) {
  157. // NSLog(@"取消");
  158. // }];
  159. // [alertSheet addAction:cancelAction];
  160. //
  161. //
  162. // // if ([alertSheet respondsToSelector:@selector(popoverPresentationController)]) {
  163. // // alertSheet.popoverPresentationController.sourceView = self.view; //必须加
  164. // // alertSheet.popoverPresentationController.sourceRect = CGRectMake(0, JX_SCREEN_HEIGHT, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);//可选,我这里加这句代码是为了调整到合适的位置
  165. // // }
  166. // [self presentViewController:alertSheet animated:YES completion:nil];
  167. }
  168. #pragma mark ------------------------------ 导航 - iosMap
  169. -(void) onDaoHangForIOSMap
  170. {
  171. //起点
  172. // CLLocation * location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
  173. // location = [location locationMarsFromBaidu];
  174. //
  175. // CLLocationCoordinate2D coor =location.coordinate;
  176. // MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor addressDictionary:nil]];
  177. // currentLocation.name =@"我的位置";
  178. JXMapData * staticData = [_locations firstObject];
  179. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  180. // self.title = staticData.title;
  181. //目的地的位置
  182. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  183. // location2 = [location2 locationMarsFromBaidu];
  184. CLLocationCoordinate2D coor2 =location2.coordinate;
  185. // CLLocationCoordinate2D coords = self.location;
  186. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];
  187. toLocation.name = staticData.title;
  188. NSArray *items = [NSArray arrayWithObjects:toLocation, nil];
  189. NSString * mode = MKLaunchOptionsDirectionsModeDriving;
  190. // switch (_seleIndex) {
  191. // case 1:
  192. // {
  193. // mode = MKLaunchOptionsDirectionsModeTransit;
  194. // }
  195. // break;
  196. // case 2:
  197. // {
  198. // mode = MKLaunchOptionsDirectionsModeDriving;
  199. // }
  200. // break;
  201. // case 3:
  202. // {
  203. // mode = MKLaunchOptionsDirectionsModeWalking;
  204. // }
  205. // break;
  206. //
  207. // default:
  208. // break;
  209. // }
  210. NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:mode, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
  211. //打开苹果自身地图应用,并呈现特定的item
  212. [MKMapItem openMapsWithItems:items launchOptions:options];
  213. }
  214. #pragma mark ------------------------------ 导航 - 高德
  215. -(void) onDaoHangForGaoDeMap
  216. {
  217. // m 驾车:0:速度最快,1:费用最少,2:距离最短,3:不走高速,4:躲避拥堵,5:不走高速且避免收费,6:不走高速且躲避拥堵,7:躲避收费和拥堵,8:不走高速躲避收费和拥堵 公交:0:最快捷,2:最少换乘,3:最少步行,5:不乘地铁 ,7:只坐地铁 ,8:时间短 是
  218. // t = 0:驾车 =1:公交 =2:步行
  219. // NSString * t = @"0";
  220. // switch (_seleIndex) {
  221. // case 1:
  222. // {
  223. // t = @"1";
  224. // }
  225. // break;
  226. // case 2:
  227. // {
  228. // t = @"0";
  229. // }
  230. // break;
  231. // case 3:
  232. // {
  233. // t = @"2";
  234. // }
  235. // break;
  236. //
  237. // default:
  238. // break;
  239. // }
  240. //起点
  241. // CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
  242. // location = [location locationMarsFromBaidu];
  243. //
  244. // CLLocationCoordinate2D coor =location.coordinate;
  245. //目的地的位置
  246. // CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
  247. // location2 = [location2 locationMarsFromBaidu];
  248. //
  249. // CLLocationCoordinate2D coor2 =location2.coordinate;
  250. JXMapData * staticData = [_locations firstObject];
  251. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  252. // self.title = staticData.title;
  253. //目的地的位置
  254. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  255. // location2 = [location2 locationMarsFromBaidu];
  256. CLLocationCoordinate2D coor2 =location2.coordinate;
  257. // 导航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"
  258. // 路径规划 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
  259. // -- 不能直接让用户进入导航,应该给用户更多的选择,所以先进行路径规划
  260. // NSURL *myLocationScheme = [NSURL URLWithString:@"iosamap://path?sourceApplication=shikuim"];
  261. 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];
  262. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 iosamap
  263. {
  264. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  265. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  266. NSLog(@"scheme调用结束"); }];
  267. }else { //iOS10以前,使用旧API
  268. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  269. }
  270. }else{
  271. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  272. }
  273. }
  274. #pragma mark ------------------------------ 导航 - 百度
  275. -(void) onDaoHangForBaiDuMap
  276. {
  277. // 百度地图如何调起APP进行导航
  278. // mode 导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行
  279. NSString * modeBaiDu = @"driving";
  280. // switch (_seleIndex) {
  281. // case 1:
  282. // {
  283. // modeBaiDu = @"transit";
  284. // }
  285. // break;
  286. // case 2:
  287. // {
  288. // modeBaiDu = @"driving";
  289. // }
  290. // break;
  291. // case 3:
  292. // {
  293. // modeBaiDu = @"walking";
  294. // }
  295. // break;
  296. //
  297. // default:
  298. // break;
  299. // }
  300. JXMapData * staticData = [_locations firstObject];
  301. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  302. // self.title = staticData.title;
  303. //目的地的位置
  304. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  305. // location2 = [location2 locationMarsFromBaidu];
  306. CLLocationCoordinate2D coor2 =location2.coordinate;
  307. 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] ;
  308. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  309. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。
  310. {
  311. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  312. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  313. NSLog(@"scheme调用结束");
  314. }];
  315. }else { //iOS10以前,使用旧API
  316. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  317. }
  318. }else{
  319. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  320. }
  321. }
  322. #pragma mark --- google
  323. -(void)onGoogleMap{
  324. JXMapData * staticData = [_locations firstObject];
  325. CLLocationCoordinate2D toCoor = [staticData coordinate2D];
  326. // self.title = staticData.title;
  327. //目的地的位置
  328. CLLocation * location2 = [[CLLocation alloc]initWithLatitude:toCoor.latitude longitude:toCoor.longitude];
  329. // location2 = [location2 locationMarsFromBaidu];
  330. CLLocationCoordinate2D coor2 =location2.coordinate;
  331. NSString *url = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"shikuim",coor2.latitude, coor2.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  332. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。
  333. {
  334. if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
  335. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
  336. NSLog(@"scheme调用结束"); }];
  337. }else { //iOS10以前,使用旧API
  338. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  339. }
  340. }else{
  341. [g_App showAlert:Localized(@"JX_MapSelNotInstall")];
  342. }
  343. }
  344. - (void)createGoogleMap {
  345. GMSCameraPosition *camera = nil;
  346. if (self.latitude == 0) {
  347. camera = [GMSCameraPosition cameraWithLatitude:22.290664
  348. longitude:114.195304
  349. zoom:14];
  350. }else{
  351. camera = [GMSCameraPosition cameraWithLatitude:self.latitude
  352. longitude:self.longitude
  353. zoom:14];
  354. }
  355. _gooMapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  356. _gooMapView.delegate = self;
  357. self.view = _gooMapView;
  358. }
  359. - (void)locationManager:(CLLocationManager *)manager
  360. didUpdateLocations:(NSArray *)locations
  361. {
  362. CLLocation *curLocation = [locations lastObject];
  363. CLLocationCoordinate2D curCoordinate2D=curLocation.coordinate;
  364. [_locationManager stopUpdatingLocation];
  365. dispatch_async(dispatch_get_main_queue(), ^{
  366. // [_gooMapView clear];
  367. // _gooMapView.camera=[GMSCameraPosition cameraWithLatitude:curCoordinate2D.latitude longitude:curCoordinate2D.longitude zoom:14];
  368. self.longitude = curCoordinate2D.longitude;
  369. self.latitude = curCoordinate2D.latitude;
  370. });
  371. }
  372. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
  373. }
  374. -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
  375. /**
  376. * 拿到授权发起定位请求
  377. */
  378. if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
  379. [_locationManager startUpdatingLocation];
  380. }
  381. }
  382. #pragma mark - 获取地理位置名称(地点名)
  383. - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{
  384. //反向地理编码
  385. [[GMSGeocoder geocoder]reverseGeocodeCoordinate:position.target completionHandler:^(GMSReverseGeocodeResponse * response, NSError * error) {
  386. if (response.results) {
  387. GMSAddress *address = response.results[0];
  388. NSLog(@"%@",address.thoroughfare);
  389. _address = address.thoroughfare;
  390. if (![_address isEqualToString:@""]) {
  391. JXPlaceMarkModel *place = [[JXPlaceMarkModel alloc] init];
  392. place.placeName = address.locality;
  393. place.address = _address;
  394. [_nearMarkArray removeAllObjects];
  395. [_nearMarkArray addObject:place];
  396. [_nearMarkTableView reloadData];
  397. }
  398. }
  399. }];
  400. }
  401. #pragma mark - 移动镜头 得到位置
  402. -(void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position{
  403. if (_locationType == JXGooLocationTypeCurrentLocation) {
  404. self.longitude = position.target.longitude;
  405. self.latitude = position.target.latitude;
  406. }
  407. }
  408. - (nullable UIImage *)snapshotToImage:(CGSize)size {
  409. _gooMapView.myLocationEnabled = NO;
  410. CGPoint map_center = _gooMapView.center;
  411. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
  412. [_gooMapView drawViewHierarchyInRect:CGRectMake(0, -(map_center.y - size.height)-JX_SCREEN_TOP, _gooMapView.bounds.size.width, _gooMapView.bounds.size.height) afterScreenUpdates:YES];
  413. UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  414. UIGraphicsEndImageContext();
  415. return snapshotImage;
  416. }
  417. - (void)getServerData {
  418. }
  419. - (void)customView{
  420. if (_locationType == JXGooLocationTypeShowStaticLocation) {
  421. //复位
  422. UIButton * resetLoca = [[UIButton alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH -50, JX_SCREEN_HEIGHT -50, 30, 30)];
  423. [resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
  424. [resetLoca addTarget:self action:@selector(resetMapCenter) forControlEvents:UIControlEventTouchUpInside];
  425. [self.view addSubview:resetLoca];
  426. }else{
  427. //复位
  428. UIButton * resetLoca = [[UIButton alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH -50, JX_SCREEN_HEIGHT -50, 30, 30)];
  429. [resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
  430. [resetLoca addTarget:self action:@selector(resetLocation) forControlEvents:UIControlEventTouchUpInside];
  431. [self.view addSubview:resetLoca];
  432. //当前位置图标
  433. UIImageView * pointImage = [[UIImageView alloc]initWithFrame:CGRectMake((JX_SCREEN_WIDTH - 30) /2, (JX_SCREEN_HEIGHT -64)/2 - 50 +10 +64, 30, 50)];
  434. pointImage.image = [UIImage imageNamed:@"position"];
  435. pointImage.userInteractionEnabled = NO;
  436. [self.view addSubview:pointImage];
  437. //周边地点列表
  438. _nearMarkTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT - 100, JX_SCREEN_WIDTH, 100) style:UITableViewStylePlain];
  439. _nearMarkTableView.dataSource = self;
  440. _nearMarkTableView.delegate = self;
  441. _nearMarkTableView.separatorStyle = UITableViewCellSelectionStyleNone;
  442. [self.view addSubview:_nearMarkTableView];
  443. }
  444. }
  445. - (void)resetMapCenter {
  446. GMSCameraPosition *sydney = [GMSCameraPosition cameraWithLatitude:self.latitude
  447. longitude:self.longitude
  448. zoom:14];
  449. [_gooMapView setCamera:sydney];
  450. }
  451. - (void)onSelect {
  452. NSString *imagePath = [self screenShotAction];
  453. JXMapData * data = [[JXMapData alloc]init];
  454. if (_model != nil) {
  455. data.latitude = [NSString stringWithFormat:@"%f",_model.latitude];
  456. data.longitude = [NSString stringWithFormat:@"%f",_model.longitude];
  457. data.subtitle = _model.name;
  458. }else{
  459. data.latitude = [NSString stringWithFormat:@"%f",self.latitude];
  460. data.longitude = [NSString stringWithFormat:@"%f",self.longitude];
  461. data.subtitle = _address;
  462. }
  463. if (!data.subtitle || data.subtitle.length <= 0 || [data.subtitle isEqualToString:@"null"] || [data.subtitle isEqualToString:@"(null)"]) {
  464. if (g_server.address.length > 0) {
  465. data.subtitle = g_server.address;
  466. }else {
  467. data.subtitle = @"";
  468. }
  469. }
  470. data.imageUrl = imagePath;
  471. if(self.delegate != nil && [self.delegate respondsToSelector:self.didSelect])
  472. [self.delegate performSelectorOnMainThread:self.didSelect withObject:data waitUntilDone:NO];
  473. [self actionQuit];
  474. _delegate = nil;
  475. [_gooMapView removeFromSuperview];
  476. }
  477. -(NSString *)screenShotAction{
  478. UIImage *image = [self snapshotToImage:CGSizeMake(JX_SCREEN_WIDTH, JX_SCREEN_WIDTH/2.2)];
  479. UIImage *logoImage = [self addImageLogo:image text:[UIImage imageNamed:@"position"]];
  480. NSString* filePath = [FileInfo getUUIDFileName:@"jpg"];
  481. [g_server saveImageToFile:logoImage file:filePath isOriginal:NO];
  482. return filePath;
  483. }
  484. -(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
  485. {
  486. int w = img.size.width;
  487. int h = img.size.height;
  488. // int logoWidth = 120;
  489. // int logoHeight = 200;
  490. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  491. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  492. CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  493. // CGContextDrawImage(context, CGRectMake((w-logoWidth)/2, h/2-10, logoWidth, logoHeight), [logo CGImage]);
  494. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  495. CGContextRelease(context);
  496. CGColorSpaceRelease(colorSpace);
  497. return [UIImage imageWithCGImage:imageMasked];
  498. }
  499. - (void)resetLocation{
  500. [g_App showAlert:Localized(@"JXLoc_StartLocNotice")];
  501. }
  502. #pragma mark tableViewDelegate
  503. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  504. {
  505. if (_nearMarkArray.count>0) {
  506. return _nearMarkArray.count;
  507. }
  508. return 0;
  509. }
  510. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  511. if (_nearMarkArray.count>0) {
  512. return _nearMarkArray.count*44;
  513. }
  514. return 0;
  515. }
  516. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  517. {
  518. JXNearMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JXNearMarkCell class])];
  519. if (cell == nil) {
  520. cell = [[JXNearMarkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JXNearMarkCell class])];
  521. }
  522. if (_nearMarkArray.count>0) {
  523. [cell refreshWithModel:[_nearMarkArray objectAtIndex:indexPath.row]];
  524. }
  525. return cell;
  526. }
  527. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  528. // [_locService stopUserLocationService];
  529. _selIndex = indexPath.row;
  530. JXPlaceMarkModel*model = [_nearMarkArray objectAtIndex:indexPath.row];
  531. model.longitude = self.longitude;
  532. model.latitude = self.latitude;
  533. model.name = self.address;
  534. self.model = model;
  535. JXNearMarkCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  536. cell.selFlag.hidden = NO;
  537. if (self.lastCell != cell) {
  538. self.lastCell.selFlag.hidden = YES;
  539. }
  540. // self.textString = model.address;
  541. if (self.lastCell != cell) {
  542. self.lastCell = cell;
  543. }
  544. }
  545. @end