百度地图-反编码心得

类似于微信中的发送位置,拖拽重新定位,以及反编码,列表附近的位置。

思路就是将一个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 ()<BMKMapViewDelegate,BMKLocationServiceDelegate,UITableViewDelegate,UITableViewDataSource,BMKGeoCodeSearchDelegate>
@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< result.poiList.count; i++) {
        BMKPoiInfo *info = result.poiList[i];
        FTBMKPoiInfo *ftInfo =[[FTBMKPoiInfo alloc]init];
        ftInfo.address = info.address;
        ftInfo.seleced = NO;
        if (i == 0) {
            ftInfo.seleced = YES;
            self.selectAddress = ftInfo.address;
        }
        [self.dataA addObject:ftInfo];
    }
    [self.tableview reloadData];
}

#pragma mark--- 定位的方法--
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    //    NSLog(@"heading is %@",userLocation.heading);
}
-(BMKMapView *)mapView{
    if (!_mapView) {
        _mapView =[[BMKMapView alloc]initWithFrame:CGRectMake(0, NAVH, Device_Width, 350)];
        _mapView.zoomLevel = 18;
        _mapView.minZoomLevel = 3;
        _mapView.maxZoomLevel = 21;
//        BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
//        displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
//        displayParam.isAccuracyCircleShow = false;//精度圈是否显示
//        displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
//        displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
//        [_mapView updateLocationViewWithParam:displayParam];

    }
    return _mapView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataA.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FTPoiCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[FTPoiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    FTBMKPoiInfo *info = self.dataA[indexPath.row];
    cell.info = info;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    FTBMKPoiInfo *info = self.dataA[indexPath.row];
    self.selectAddress = info.address;

    [self.dataA enumerateObjectsUsingBlock:^(FTBMKPoiInfo * obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if (obj == info) {
            obj.seleced = YES;
        }else{
            obj.seleced = NO;
        }

        [self.tableview reloadData];
    }];

    if (self.selectBlock) {
        self.selectBlock(self.selectAddress,self.selectedCoordinate);

        [self.navigationController popViewControllerAnimated:YES];
    }

}
-(UITableView *)tableview{

    if (!_tableview) {
        _tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, self.mapView.bottom, Device_Width, Device_Height - self.mapView.bottom) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.tableFooterView = [UIView new];
        _tableview.rowHeight = 44;
        [_tableview registerNib:[UINib nibWithNibName:@"FTPoiCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    }
    return _tableview;
}
-(NSMutableArray *)dataA{
    if (!_dataA) {
        _dataA =[NSMutableArray array];
    }
    return _dataA;
}
-(UIImageView *)loactionView{
    if (!_loactionView) {
        _loactionView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ditu_red"]];
        _loactionView.center = CGPointMake(self.mapView.width/2, self.mapView.height/2);

    }
    return _loactionView;
}
-(LxButton *)poiBackBtn{
    if (!_poiBackBtn) {
        _poiBackBtn =[LxButton LXButtonWithTitle:nil titleFont:nil Image:nil backgroundImage:nil backgroundColor:[UIColor whiteColor] titleColor:nil frame:CGRectMake(Device_Width - 75, self.mapView.height - 75, 50, 50)];
        [_poiBackBtn setFTCornerdious:25];
        UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"poi_back"]];

        imageView.center = CGPointMake(25, 25);
        [_poiBackBtn addSubview:imageView];

    }
    return _poiBackBtn;
}
@end

原文地址:https://www.cnblogs.com/fengmin/p/8176820.html

时间: 2024-07-30 08:57:12

百度地图-反编码心得的相关文章

在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路**号院**社区&output=json&ak=您的AK密钥 返回结果实例: { "status":0, "result": { "location":{"lng":116.79, "lat":

百度地图反地理

1.地理编码指的是将地址位置(中文地址)转换成经纬度,反地址编码指的是将经纬度转换成地址位置: 2.在百度地图中需要用到三个关键性的类:BMKGeoCodeSearch.BMKGeoCodeSearchOption.BMKReverseGeoCodeOption: 3.BMKGeoCodeSearch:地理编码主类,用来查询.返回结果信息(地址位置或经纬度): 4.BMKGeoCodeSearchOption:地理编码选项,即地理编码的数据模型,地址是通过该类传递进去的: 5.BMKRevers

乐卡上海网点地图制作心得 | 百度地图API使用心得

前言 事情的起因是我的爱人喜欢收集一些美丽的乐卡(明信片的一种,正面是美丽壮阔的风景照).作为一个坚实的后盾自然要支持她!于是我经常借着午休穿梭在大街小巷,凭借乐卡官方提供的乐卡网点地址进行寻找并取卡.在搜寻过程中,萌生了制作一张基于那些地址的专门戳点地图的想法.期间也看到蚂蚁家制作的北京乐卡地图使用的是百度地图API,但是该网页仅能运行于PC端,手机打开巨卡.刚好我的github博客站最近配置完成了,准备自己也写一个. 材料单 百度开放平台账号一个 百度开放平台浏览器端启用了Javascrip

iOS 百度地图反检索地址的问题

今天碰到了这样一个问题,我从服务器端获取了当前用户的轨迹数据列表,想通过反向检索获取用户的轨迹地址,逻辑代码基本上是这样 BMKGeoSearch *signleSearch; for(location in locationArray) { CLLocationCoordinate2d pt = xxxxx; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

百度地图sdk踩坑之旅

1.写在前面 项目中需要加上路线规划,导航,添加覆盖物,因为我最开始项目中定位我使用的是百度定位,所以为了省事,接着使用百度地图sdk实现这些.这两天踩了很多百度地图的坑,记下来.因为一些原因,后面会说,需求还没做完,所以效果图很简单,如下. 2.坑1,环境配置 对于百度地图sdk的配置我是无语.因为一开始项目中并没打算使用百度地图其他功能,只是准备定位.所以我下载sdk时只是下载了定位的sdk,如图 现在需要加上地图和导航的sdk(后面才知道导航并不需要下载专门的导航sdk,因为地图sdk可以

百度地图开发(二)之添加覆盖物 + 地理编码和反地理编码

之前写过一篇关于百度地图开发的blog,主要介绍了百度地图的基本地图的显示. 详见:Android百度地图开发(一)之初体验 下面来看一下地图上覆盖物的添加,以及地理编码和反地理编码. 添加覆盖物 在地图上添加覆盖物,一般需要以下几个步骤: 1. 定义坐标点,有可能是一个,有可能是多个(比如:多边形覆盖物). 2. 构造OverlayOptions(地图覆盖物选型基类). 3. 在地图上添加覆盖物. 4. 添加相应的监听事件. 在API中可以看到,BaiDuMap类中有一个方法: 这个方法就是用

iOS第三方地图-百度地图常用功能使用(POI搜索,地理正反编码,定位,添加标注)

百度地图官网: http://developer.baidu.com/map/index.php?title=iossdk 百度地图集成 1.引入相关包 注:不需要使用地图功能的可以不用boundle包,模拟器使用的framework和真机不一样具体看官网 2.info.plist中 3.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC. 4.将一个文件设置为.mm 百度地图使用 1.AppDelegate中注册 _mapManager

地图定位CoreLocation框架,地理位置编码与反编码

在现代互联网时代,越来越多的应用,都用到了地图定位功能,在iOS开发中,想要加入这种功能,必须基于两个框架进行开发: 1.Map Kit:用于显示地图, 2.CoreLocation:用于显示地理位置 这里我们简单了解一下CoreLocation,用于显示地理位置,坐标信息. 一.相关类介绍 CLLocationManager.用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向. CLLocation.封装了位置和高度信息. CLL

objective-c开发——地图定位之地理编码和地理反编码

我们平时做地图定位,主要是靠经纬度来准确定位某个位置. 但是,我们是人啊,我们不是卫星啊. 用户在地图上查一个地方,我们总不能告诉他,这个地方是东经多少度,北纬多少度吧. 咱们好歹得告诉人家个地名不是? 这就是我们今天说的地理编码和地理反编码. 地理编码:你说个地名,比如“西湖”,我们给你返回它的经纬度,然后你通过查出来的这个经纬度去定位 反地理编码:我告诉你一个经纬度,你通过经度纬度返回地名.最好在插个大头针在地图上就更好了,啥叫大头针,咱们以后再说. 首先,我的界面是这个样纸的,就是两个按钮