| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- //
- // QMapLocationView.m
- // O2O
- //
- // Created by xfg on 15/9/10.
- // Copyright (c) 2015年 fanwe. All rights reserved.
- //
- #import "QMapLocationView.h"
- #define kStopLocationTime 60 //停止定位时间
- @interface QMapLocationView()<QMapViewDelegate,QMSSearchDelegate>
- @property (nonatomic, strong) QMapView *mapView; //腾讯地图
- @property (nonatomic, strong) QMSSearcher *mapSearcher;
- @end
- @implementation QMapLocationView
- static id _instance;
- + (instancetype)allocWithZone:(struct _NSZone *)zone
- {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [super allocWithZone:zone];
- });
- return _instance;
- }
- + (instancetype)sharedInstance
- {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- return _instance;
- }
- - (id)copyWithZone:(NSZone *)zone
- {
- return _instance;
- }
- - (id)mutableCopyWithZone:(NSZone *)zone {
- return _instance;
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- _BuguLive = [GlobalVariables sharedInstance];
- self.mapView = [[QMapView alloc]initWithFrame:self.bounds];
- [self addSubview:self.mapView];
- self.mapView.userTrackingMode = QUserTrackingModeFollowWithHeading;
- self.mapSearcher = [[QMSSearcher alloc] init];
- }
- return self;
- }
- #pragma mark 开始定位
- - (void)startLocate
- {
- if (!self.mapView.delegate){
- self.mapView.delegate = self;
- self.mapSearcher.delegate = self;
- [self.mapView setShowsUserLocation:YES];
- [self performSelector:@selector(stopLocate) withObject:nil afterDelay:kStopLocationTime];
- }
- }
- #pragma mark 停止定位
- - (void)stopLocate{
- if (self.mapView.delegate){
- [self.mapView setShowsUserLocation:NO];
- self.mapView.delegate = nil;
- self.mapSearcher.delegate = nil;
- }
- }
- #pragma mark 重新定位
- - (void)locateAgain{
- self.mapView.delegate = self;
- self.mapSearcher.delegate = self;
- [self.mapView setShowsUserLocation:YES];
- _locateNum = 1;
- [self performSelector:@selector(stopLocate) withObject:nil afterDelay:kStopLocationTime];
- }
- /*!
- * @brief 在地图view将要启动定位时,会调用此函数
- *
- * @param mapView 地图view
- */
- - (void)mapViewWillStartLocatingUser:(QMapView *)mapView{
-
- }
- /*!
- * @brief 在地图view定位停止后,会调用此函数
- *
- * @param mapView 地图view
- */
- - (void)mapViewDidStopLocatingUser:(QMapView *)mapView{
-
- }
- /*!
- * @brief 位置或者设备方向更新后,会调用此函数
- *
- * @param mapView 地图view
- * @param userLocation 用户定位信息(包括位置与设备方向等数据)
- * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
- */
- - (void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
- CLLocationCoordinate2D coordinate = userLocation.coordinate;
- if (coordinate.latitude && coordinate.longitude) {
- _BuguLive.latitude = coordinate.latitude;
- _BuguLive.longitude = coordinate.longitude;
- // NSLog(@"----_BuguLive.latitude:%f",_BuguLive.latitude);
- // NSLog(@"----_BuguLive.longitude:%f",_BuguLive.longitude);
- }
- if (_BuguLive.latitude || _BuguLive.longitude) {
- if (_delegate && [_delegate respondsToSelector:@selector(startLoadView)]) {
- [_delegate startLoadView];
- }
- }
-
- // //配置搜索参数
- // QMSReverseGeoCodeSearchOption *reGeoSearchOption = [[QMSReverseGeoCodeSearchOption alloc] init];
- // [reGeoSearchOption setLocationWithCenterCoordinate:coordinate];
- // [reGeoSearchOption setGet_poi:YES];
- // [self.mapSearcher searchWithReverseGeoCodeSearchOption:reGeoSearchOption];
- }
- #pragma mark 反向地理编码
- - (void)searchWithReverseGeoCodeSearchOption:(QMSReverseGeoCodeSearchOption *)reverseGeoCodeSearchOption didReceiveResult:(QMSReverseGeoCodeSearchResult *)reverseGeoCodeSearchResult
- {
-
- if (reverseGeoCodeSearchResult) {
- if (reverseGeoCodeSearchResult.address && ![reverseGeoCodeSearchResult.address isEqualToString:@""]){
-
- QMSReGeoCodeAdInfo *reGeoCodeAdInfo = reverseGeoCodeSearchResult.ad_info;
- if (reGeoCodeAdInfo)
- {
- _BuguLive.locationCity = reGeoCodeAdInfo.city;
- _BuguLive.province = reverseGeoCodeSearchResult.ad_info.province;
- _BuguLive.area = reverseGeoCodeSearchResult.ad_info.district;
- _BuguLive.locateName = reverseGeoCodeSearchResult.formatted_addresses.recommend;
- NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
- // [mdic setObject:reverseGeoCodeSearchResult.formatted_addresses.recommend forKey:@"recommend"];
- [mdic setObject:[NSString stringWithFormat:@"%.8f",_BuguLive.latitude] forKey:@"lat"];
- [mdic setObject:[NSString stringWithFormat:@"%.8f",_BuguLive.longitude] forKey:@"lng"];
- if (![reverseGeoCodeSearchResult.ad_info.province isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.province) {
-
- [mdic setObject:reverseGeoCodeSearchResult.ad_info.province forKey:@"province"];
- }
- if (![reverseGeoCodeSearchResult.ad_info.city isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.city) {
-
- [mdic setObject:reverseGeoCodeSearchResult.ad_info.city forKey:@"city"];
- }
- if (![reverseGeoCodeSearchResult.ad_info.district isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.district) {
-
- [mdic setObject:reverseGeoCodeSearchResult.ad_info.district forKey:@"area"];
- }
-
- _BuguLive.addressJsonStr = [BGUtils dataTOjsonString:mdic];
- // NSLog(@"========ssss:%@",_BuguLive.addressJsonStr);
-
- NSString *strUrl = [_BuguLive.addressJsonStr stringByReplacingOccurrencesOfString:@" " withString:@""];
- // NSLog(@"========bbbb:%@",strUrl);
-
- NSString *strUrl2 = [strUrl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- // NSLog(@"========bbbb:%@",strUrl2);
-
- _BuguLive.addressJsonStr = strUrl2;
-
- }
-
- if (_locateNum<2) {
- if (_delegate && [_delegate respondsToSelector:@selector(reverseGeoCodeResult:)]) {
- [_delegate reverseGeoCodeResult:reverseGeoCodeSearchResult];
- }
- _locateNum++;
- }
- }
- }
- }
- /*!
- * @brief 定位失败后,会调用此函数
- *
- * @param mapView 地图view
- * @param error 错误号,参考CLError.h中定义的错误号
- */
- #pragma mark -- 定位失败
- - (void)mapView:(QMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
- NSLog(ASLocalizedString(@"定位失败"));
- if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
- {
- // 用户关闭了该应用的定位权限.
- [FanweMessage alert:ASLocalizedString(@"您关闭了该应用的定位权限.请在设置->隐私->定位服务->本App下选择使用期间")];
- NSLog(ASLocalizedString(@"用户关闭了该应用的定位权限."));
-
- }
-
- }
- @end
|