热门标签 | HotTags
当前位置:  开发笔记 > 开放平台 > 正文

iOS实现百度地图拖拽后更新位置以及反编码

百度地图已经开放了地图API,大家可以很方便的调用地图中的相应数据,并完成各项个性化的展示,下面这篇文章主要给大家介绍了关于iOS如何实现百度地图拖拽后更新位置以及反编码的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

最近在开发中遇到了百度地图的开发,功能类似于微信中的发送位置,拖拽从新定位,以及反编码,列表附近的位置。分析出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

效果图:

百度地图拖拽更新位置.gif

实现思路

思路就是将一个UIImageView固定在地图中间,每次更新位置,给UIImageView添加动画即可。

代码如下:

#import "FTBasicController.h"
typedef void (^SelectBlock) (NSString *address,CLLocationCoordinate2D select);
@interface FTUploadAddressController : FTBasicController
@property(nonatomic, copy)SelectBlock selectBlock;
@end
#import "FTUploadAddressController.h"
#import "FTBMKPoiInfo.h"
#import "FTPoiCell.h"
@interface FTUploadAddressController ()@property(nonatomic,strong)BMKLocationService *locService;
@property(nonatomic,strong)BMKUserLocation *userLocation;
@property(nonatomic,strong)BMKMapView *mapView;
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)BMKGeoCodeSearch *geocodesearch;
@property(nonatomic,strong)UIImageView *loactionView;
@property(nonatomic,strong)NSMutableArray *dataA;
@property(nonatomic,strong)LxButton *poiBackBtn;
@property(nonatomic,assign)CLLocationCoordinate2D selectedCoordinate;
@property(nonatomic,strong)NSString *selectAddress;
@end
@implementation FTUploadAddressController
-(void)viewWillAppear:(BOOL)animated{
 [super viewWillAppear:animated];
 self.fd_interactivePopDisabled = YES;
 if (!([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) &&[CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
  [self judgeOpenlocation];
 }else{
  [_mapView viewWillAppear];
  _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  _locService.delegate = self;
   _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  _mapView.showsUserLocation = NO;//先关闭显示的定位图层
  _mapView.userTrackingMode = 0;
  _mapView.showsUserLocation = YES;//显示定位图层
  [self.locService startUserLocationService];
 }
}
-(void)judgeOpenlocation{
 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"打开[定位服务]来允许[应用名字]确定您的位置" message:@"请在系统设置中开启定位服务(设置>隐私>定位服务>应用名字>始终)" preferredStyle:UIAlertControllerStyleAlert];
 [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
 [alertVC addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.000000) {
   //跳转到定位权限页面
   NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
   if( [[UIApplication sharedApplication]canOpenURL:url] ) {
    [[UIApplication sharedApplication] openURL:url];
   }
  }else {
   //跳转到定位开关界面
   NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
   if( [[UIApplication sharedApplication]canOpenURL:url] ) {
    [[UIApplication sharedApplication] openURL:url];
   }
  }
 }]];
 [self presentViewController:alertVC animated:YES completion:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
  self.fd_interactivePopDisabled = NO;
 [_mapView viewWillDisappear];
 _mapView.delegate = nil; // 不用时,置nil
 [self.locService stopUserLocationService];
  _geocodesearch.delegate = nil; // 不用时,置nil
 _locService.delegate = nil;
}
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 self.title = @"所在位置";
  self.locService = [[BMKLocationService alloc]init];
 self.geocodesearch = [[BMKGeoCodeSearch alloc]init];
  [self setup];
  self.navigationItem.leftBarButtOnItem=[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"return"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backReturn)];
}
-(void)backReturn{
 if (self.selectBlock) {
  self.selectBlock(self.selectAddress, self.selectedCoordinate);
  [self.navigationController popViewControllerAnimated:YES];
 }
}
-(void)setup{
  [self.view addSubview:self.mapView];
 [self.view addSubview:self.tableview];
 [self.mapView addSubview:self.loactionView];
 [self.mapView addSubview:self.poiBackBtn];
  [self.poiBackBtn LX_SetShadowPathWith:[UIColor grayColor] shadowOpacity:0.5 shadowRadius:5 shadowSide:LXShadowPathBottom shadowPathWidth:3];
 FTWS(weakSelf);
 [self.poiBackBtn addClickBlock:^(UIButton *button) {
   [weakSelf.mapView setCenterCoordinate:weakSelf.userLocation.location.coordinate];
 }];
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
 //  NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
 [_mapView updateLocationData:userLocation];
 self.userLocation = userLocation;
 [self.mapView setCenterCoordinate:userLocation.location.coordinate];
 BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init];
 option.reverseGeoPoint = userLocation.location.coordinate;
 BOOL flag = [_geocodesearch reverseGeoCode:option];
 if (flag) { 
 }
 //更新位置之后必须停止定位,
 [_locService stopUserLocationService];
}
-(void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
 NSLog(@"地图拖动");
 [UIView animateWithDuration:0.30 animations:^{
  self.loactionView.centerY -=8;
 } completion:^(BOOL finished) {
  self.loactionView.centerY +=8;
 }];
 CGPoint touchPoint = self.mapView.center;
 CLLocationCoordinate2D touchMapCoordinate =
 [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了
 NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
 //选择的上传地址
 self.selectedCoordinate = touchMapCoordinate;
 BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init];
 option.reverseGeoPoint = touchMapCoordinate;
 BOOL flag = [_geocodesearch reverseGeoCode:option];
 if (flag) { 
 }
}
#pragma mark---获取反编码的数据---
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
  BMKAddressComponent *compOnent=[[BMKAddressComponent alloc]init];
  compOnent=result.addressDetail;
 [self.dataA removeAllObjects];
 for (int i =0; i

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。


推荐阅读
author-avatar
刘少静mm_527
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有