QMapLocationView.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // QMapLocationView.m
  3. // O2O
  4. //
  5. // Created by xfg on 15/9/10.
  6. // Copyright (c) 2015年 fanwe. All rights reserved.
  7. //
  8. #import "QMapLocationView.h"
  9. #define kStopLocationTime 60 //停止定位时间
  10. @interface QMapLocationView()<QMapViewDelegate,QMSSearchDelegate>
  11. @property (nonatomic, strong) QMapView *mapView; //腾讯地图
  12. @property (nonatomic, strong) QMSSearcher *mapSearcher;
  13. @end
  14. @implementation QMapLocationView
  15. static id _instance;
  16. + (instancetype)allocWithZone:(struct _NSZone *)zone
  17. {
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. _instance = [super allocWithZone:zone];
  21. });
  22. return _instance;
  23. }
  24. + (instancetype)sharedInstance
  25. {
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. _instance = [[self alloc] init];
  29. });
  30. return _instance;
  31. }
  32. - (id)copyWithZone:(NSZone *)zone
  33. {
  34. return _instance;
  35. }
  36. - (id)mutableCopyWithZone:(NSZone *)zone {
  37. return _instance;
  38. }
  39. - (id)initWithFrame:(CGRect)frame
  40. {
  41. self = [super initWithFrame:frame];
  42. if (self) {
  43. _BuguLive = [GlobalVariables sharedInstance];
  44. self.mapView = [[QMapView alloc]initWithFrame:self.bounds];
  45. [self addSubview:self.mapView];
  46. self.mapView.userTrackingMode = QUserTrackingModeFollowWithHeading;
  47. self.mapSearcher = [[QMSSearcher alloc] init];
  48. }
  49. return self;
  50. }
  51. #pragma mark 开始定位
  52. - (void)startLocate
  53. {
  54. if (!self.mapView.delegate){
  55. self.mapView.delegate = self;
  56. self.mapSearcher.delegate = self;
  57. [self.mapView setShowsUserLocation:YES];
  58. [self performSelector:@selector(stopLocate) withObject:nil afterDelay:kStopLocationTime];
  59. }
  60. }
  61. #pragma mark 停止定位
  62. - (void)stopLocate{
  63. if (self.mapView.delegate){
  64. [self.mapView setShowsUserLocation:NO];
  65. self.mapView.delegate = nil;
  66. self.mapSearcher.delegate = nil;
  67. }
  68. }
  69. #pragma mark 重新定位
  70. - (void)locateAgain{
  71. self.mapView.delegate = self;
  72. self.mapSearcher.delegate = self;
  73. [self.mapView setShowsUserLocation:YES];
  74. _locateNum = 1;
  75. [self performSelector:@selector(stopLocate) withObject:nil afterDelay:kStopLocationTime];
  76. }
  77. /*!
  78. * @brief 在地图view将要启动定位时,会调用此函数
  79. *
  80. * @param mapView 地图view
  81. */
  82. - (void)mapViewWillStartLocatingUser:(QMapView *)mapView{
  83. }
  84. /*!
  85. * @brief 在地图view定位停止后,会调用此函数
  86. *
  87. * @param mapView 地图view
  88. */
  89. - (void)mapViewDidStopLocatingUser:(QMapView *)mapView{
  90. }
  91. /*!
  92. * @brief 位置或者设备方向更新后,会调用此函数
  93. *
  94. * @param mapView 地图view
  95. * @param userLocation 用户定位信息(包括位置与设备方向等数据)
  96. * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
  97. */
  98. - (void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
  99. CLLocationCoordinate2D coordinate = userLocation.coordinate;
  100. if (coordinate.latitude && coordinate.longitude) {
  101. _BuguLive.latitude = coordinate.latitude;
  102. _BuguLive.longitude = coordinate.longitude;
  103. // NSLog(@"----_BuguLive.latitude:%f",_BuguLive.latitude);
  104. // NSLog(@"----_BuguLive.longitude:%f",_BuguLive.longitude);
  105. }
  106. if (_BuguLive.latitude || _BuguLive.longitude) {
  107. if (_delegate && [_delegate respondsToSelector:@selector(startLoadView)]) {
  108. [_delegate startLoadView];
  109. }
  110. }
  111. // //配置搜索参数
  112. // QMSReverseGeoCodeSearchOption *reGeoSearchOption = [[QMSReverseGeoCodeSearchOption alloc] init];
  113. // [reGeoSearchOption setLocationWithCenterCoordinate:coordinate];
  114. // [reGeoSearchOption setGet_poi:YES];
  115. // [self.mapSearcher searchWithReverseGeoCodeSearchOption:reGeoSearchOption];
  116. }
  117. #pragma mark 反向地理编码
  118. - (void)searchWithReverseGeoCodeSearchOption:(QMSReverseGeoCodeSearchOption *)reverseGeoCodeSearchOption didReceiveResult:(QMSReverseGeoCodeSearchResult *)reverseGeoCodeSearchResult
  119. {
  120. if (reverseGeoCodeSearchResult) {
  121. if (reverseGeoCodeSearchResult.address && ![reverseGeoCodeSearchResult.address isEqualToString:@""]){
  122. QMSReGeoCodeAdInfo *reGeoCodeAdInfo = reverseGeoCodeSearchResult.ad_info;
  123. if (reGeoCodeAdInfo)
  124. {
  125. _BuguLive.locationCity = reGeoCodeAdInfo.city;
  126. _BuguLive.province = reverseGeoCodeSearchResult.ad_info.province;
  127. _BuguLive.area = reverseGeoCodeSearchResult.ad_info.district;
  128. _BuguLive.locateName = reverseGeoCodeSearchResult.formatted_addresses.recommend;
  129. NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
  130. // [mdic setObject:reverseGeoCodeSearchResult.formatted_addresses.recommend forKey:@"recommend"];
  131. [mdic setObject:[NSString stringWithFormat:@"%.8f",_BuguLive.latitude] forKey:@"lat"];
  132. [mdic setObject:[NSString stringWithFormat:@"%.8f",_BuguLive.longitude] forKey:@"lng"];
  133. if (![reverseGeoCodeSearchResult.ad_info.province isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.province) {
  134. [mdic setObject:reverseGeoCodeSearchResult.ad_info.province forKey:@"province"];
  135. }
  136. if (![reverseGeoCodeSearchResult.ad_info.city isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.city) {
  137. [mdic setObject:reverseGeoCodeSearchResult.ad_info.city forKey:@"city"];
  138. }
  139. if (![reverseGeoCodeSearchResult.ad_info.district isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.district) {
  140. [mdic setObject:reverseGeoCodeSearchResult.ad_info.district forKey:@"area"];
  141. }
  142. _BuguLive.addressJsonStr = [BGUtils dataTOjsonString:mdic];
  143. // NSLog(@"========ssss:%@",_BuguLive.addressJsonStr);
  144. NSString *strUrl = [_BuguLive.addressJsonStr stringByReplacingOccurrencesOfString:@" " withString:@""];
  145. // NSLog(@"========bbbb:%@",strUrl);
  146. NSString *strUrl2 = [strUrl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  147. // NSLog(@"========bbbb:%@",strUrl2);
  148. _BuguLive.addressJsonStr = strUrl2;
  149. }
  150. if (_locateNum<2) {
  151. if (_delegate && [_delegate respondsToSelector:@selector(reverseGeoCodeResult:)]) {
  152. [_delegate reverseGeoCodeResult:reverseGeoCodeSearchResult];
  153. }
  154. _locateNum++;
  155. }
  156. }
  157. }
  158. }
  159. /*!
  160. * @brief 定位失败后,会调用此函数
  161. *
  162. * @param mapView 地图view
  163. * @param error 错误号,参考CLError.h中定义的错误号
  164. */
  165. #pragma mark -- 定位失败
  166. - (void)mapView:(QMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
  167. NSLog(ASLocalizedString(@"定位失败"));
  168. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
  169. {
  170. // 用户关闭了该应用的定位权限.
  171. [FanweMessage alert:ASLocalizedString(@"您关闭了该应用的定位权限.请在设置->隐私->定位服务->本App下选择使用期间")];
  172. NSLog(ASLocalizedString(@"用户关闭了该应用的定位权限."));
  173. }
  174. }
  175. @end