仿微信地图定位列表

2023-05-16

gitbub源代码链接:https://github.com/MinLee6/LMMapLocationList

首先按照百度配置要求配置开发环境

1:在AppDelegate.mm中设置百度定位的key

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self configureBaiduLocation];
    return YES;
}
-(void)configureBaiduLocation
{
    _mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"bP9bEpZ5WjmIUo10dQQGKo0EXcXxtvnh" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}
- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}

2:控制器中编写代码

//
//  LMTimeLineLocationListVC.m
//  LMMapLocationList
//
//  Created by limin on 17/3/27.
//  Copyright © 2017年 君安信(北京)科技有限公司. All rights reserved.
//

#import "LMTimeLineLocationListVC.h"
#import "UIView+SDAutoLayout.h"
//地图定位
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

#define kBaiduMapMaxHeight 300
#define kCurrentLocationBtnWH 50
#define kPading 10
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
@interface LMTimeLineLocationListVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,UITableViewDataSource,UITableViewDelegate>
{
    BOOL isFirstLocation;
}

@property(nonatomic,strong)BMKMapView* mapView;
@property(nonatomic,strong)BMKLocationService* locService;
@property(nonatomic,strong)BMKGeoCodeSearch* geocodesearch;

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,assign)CLLocationCoordinate2D currentCoordinate;
@property(nonatomic,assign)NSInteger currentSelectLocationIndex;
@property(nonatomic,strong)UIImageView *centerCallOutImageView;
@property(nonatomic,strong)UIButton *currentLocationBtn;


@end

@implementation LMTimeLineLocationListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self configUI];
    [self startLocation];
}
-(void)configUI
{
    [self.view addSubview:self.mapView];
    self.mapView.sd_layout
    .leftSpaceToView(self.view,0)
    .rightSpaceToView(self.view,0)
    .topSpaceToView(self.view,0)
    .heightIs(kBaiduMapMaxHeight);
    
    
    [self.view addSubview:self.centerCallOutImageView];
    [self.view bringSubviewToFront:self.centerCallOutImageView];
    self.centerCallOutImageView.sd_layout
    .topSpaceToView(self.view,(kBaiduMapMaxHeight-30)*0.5)
    .leftSpaceToView(self.view,(kScreenWidth-30)*0.5)
    .widthIs(30)
    .heightIs(30);
    
    
    [self.mapView layoutIfNeeded];
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .topSpaceToView(self.mapView,0)
    .leftSpaceToView(self.view,0)
    .rightSpaceToView(self.view,0)
    .bottomSpaceToView(self.view,0);
    
    self.currentLocationBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.currentLocationBtn setImage:[UIImage imageNamed:@"location_back_icon"] forState:UIControlStateNormal];
    [self.currentLocationBtn setImage:[UIImage imageNamed:@"location_blue_icon"] forState:UIControlStateSelected];
    [self.currentLocationBtn addTarget:self action:@selector(startLocation) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.currentLocationBtn];
    [self.view bringSubviewToFront:self.currentLocationBtn];
    self.currentLocationBtn.sd_layout
    .widthIs(kCurrentLocationBtnWH)
    .heightIs(kCurrentLocationBtnWH)
    .topSpaceToView(self.view,kBaiduMapMaxHeight-10-kCurrentLocationBtnWH)
    .leftSpaceToView(self.view,10);
    
    
}

-(void)startLocation
{
    isFirstLocation=YES;//首次定位
    self.currentSelectLocationIndex=0;
    self.currentLocationBtn.selected=YES;
    [self.locService startUserLocationService];
    self.mapView.showsUserLocation = NO;//先关闭显示的定位图层
    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    self.mapView.showsUserLocation = YES;//显示定位图层
}

-(void)startGeocodesearchWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = coordinate;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }
}

-(void)setCurrentCoordinate:(CLLocationCoordinate2D)currentCoordinate
{
    _currentCoordinate=currentCoordinate;
    [self startGeocodesearchWithCoordinate:currentCoordinate];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.mapView viewWillAppear];
    self.mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    self.locService.delegate = self;
    self.geocodesearch.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil; // 不用时,置nil
    self.locService.delegate = nil;
    self.geocodesearch.delegate = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    if (_mapView)
    {
        _mapView = nil;
    }
    if (_geocodesearch)
    {
        _geocodesearch = nil;
    }
    if (_locService)
    {
        _locService=nil;
    }
}

#pragma mark - BMKMapViewDelegate

/**
 *在地图View将要启动定位时,会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户方向更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [self.mapView updateLocationData:userLocation];
    //    NSLog(@"heading is %@",userLocation.heading);
}

/**
 *用户位置更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    isFirstLocation=NO;
    self.currentLocationBtn.selected=NO;
    [self.mapView  updateLocationData:userLocation];
    self.currentCoordinate=userLocation.location.coordinate;
    
    if (self.currentCoordinate.latitude!=0)
    {
        [self.locService stopUserLocationService];
    }
}

/**
 *在地图View停止定位后,会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后,会调用此函数
 *@param mapView 地图View
 *@param error 错误号,参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"无法定位" message:@"请在iPhone的\"设置-隐私-定位服务\"中允许灵佛使用定位服务。" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //
    }];
    [alertVC addAction:action];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
{
    NSLog(@"map view: click blank");
}
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    if (!isFirstLocation)
    {
        CLLocationCoordinate2D tt =[mapView convertPoint:self.centerCallOutImageView.center toCoordinateFromView:self.centerCallOutImageView];
        self.currentCoordinate=tt;
    }
    
}
#pragma mark - BMKGeoCodeSearchDelegate

/**
 *返回地址信息搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结BMKGeoCodeSearch果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSLog(@"返回地址信息搜索结果,失败-------------");
}

/**
 *返回反地理编码搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果
 *@param error 错误号,@see BMKSearchErrorCode
 */

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR)
    {
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:result.poiList];
        
        if (isFirstLocation)
        {
            //把当前定位信息自定义组装 放进数组首位
            BMKPoiInfo *first =[[BMKPoiInfo alloc]init];
            first.address=result.address;
            first.name=@"[当前位置]";
            first.pt=result.location;
            first.city=result.addressDetail.city;
            [self.dataSource insertObject:first atIndex:0];
        }
        
        [self.tableView reloadData];
        
    }
}

#pragma mark - TableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        
    }
    BMKPoiInfo *model=[self.dataSource objectAtIndex:indexPath.row];
    cell.textLabel.text=model.name;
    cell.detailTextLabel.text=model.address;
    cell.detailTextLabel.textColor=[UIColor grayColor];
    
    if (self.currentSelectLocationIndex==indexPath.row)
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType=UITableViewCellAccessoryNone;
    
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BMKPoiInfo *model=[self.dataSource objectAtIndex:indexPath.row];
    BMKMapStatus *mapStatus =[self.mapView getMapStatus];
    mapStatus.targetGeoPt=model.pt;
    [self.mapView setMapStatus:mapStatus withAnimation:YES];
    self.currentSelectLocationIndex=indexPath.row;
    [self.tableView reloadData];
}

#pragma mark - InitMethod

-(BMKMapView*)mapView
{
    if (_mapView==nil)
    {
        _mapView =[BMKMapView new];
        _mapView.zoomEnabled=NO;
        _mapView.zoomEnabledWithTap=NO;
        _mapView.zoomLevel=17;
    }
    return _mapView;
}

-(BMKLocationService*)locService
{
    if (_locService==nil)
    {
        _locService = [[BMKLocationService alloc]init];
    }
    return _locService;
}
-(BMKGeoCodeSearch*)geocodesearch
{
    if (_geocodesearch==nil)
    {
        _geocodesearch=[[BMKGeoCodeSearch alloc]init];
    }
    return _geocodesearch;
}

-(UITableView*)tableView
{
    if (_tableView==nil)
    {
        _tableView=[UITableView new];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        
    }
    return _tableView;
}

-(UIImageView*)centerCallOutImageView
{
    if (_centerCallOutImageView==nil)
    {
        _centerCallOutImageView=[UIImageView new];
        [_centerCallOutImageView setImage:[UIImage imageNamed:@"location_green_icon"]];
    }
    return _centerCallOutImageView;
}
-(NSMutableArray*)dataSource
{
    if (_dataSource==nil) {
        _dataSource=[[NSMutableArray alloc]init];
    }
    
    return _dataSource;
}


@end


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仿微信地图定位列表 的相关文章

  • 飞思卡尔中出现NO TBDML Interface found

    1 xff0c 安装BDM的驱动器 xff0c 安装完成之后检查USBDM上的绿灯是否闪烁 xff0c 有时候驱承购安装之后 xff0c 但绿灯不亮 xff1b 2 xff0c 若绿灯不亮 xff0c 而且在电脑属性里面的驱动安装成功之后
  • ubuntu装RTL8111/8168B网卡驱动

    给本本装了ubuntu xff0c 结果被上网的问题搞疯了 表现为开机后有较小概率网卡能正常工作 xff08 这两天来N次能用的时候有2次 xff09 正常的时候网络连接信息显示都和win下一样 不能联网的时候网络信息上全部显示0 0 0
  • LabVIEW如何调用C#Winform

    界面交互并不是Labview的强项 xff0c 使用Labview创建UI时候总会受制于VI有限的控件 xff0c 有限的皮肤和有限的控件事件 而当需要实现UI的多文档的窗口 xff0c 窗口的浮动停靠 xff0c 动画等功能时还需要花很大
  • Intellij IDEA 社区版集成 Database Navigator 数据库管理工具

    Intellij IDEA 社区版集成 DB Navigator数据库管理工具 第一步 xff1a 打开IDEA xff0c Intellij IDEA gt Preferences gt Plugins 第二步 xff1a 在Plugin
  • LabVIEW类方法浏览器-Class Method Browser

    随着LabVIEW的类编程应用增多 xff0c 当打开较多的VI进行编辑时候 xff0c 添加该类对应的VI方法到程序后背板上操作显得繁琐 xff08 需要在Project浏览器或类浏览器或库浏览器中找到该类的方法VI xff0c 然后再拖
  • vue学习笔记—bootstrap+vue用户管理

    vue xff0c 读音view xff0c 简单易用的前端框架 特点如下 xff1a 1 一个mvvm的前端框架 xff0c 内部做好了html中dom对象和后台用js语言定义的变量的双向绑定 2 中国人尤雨溪维护的个人项目 xff0c
  • Mysql查询创建和导入操作

    如何安装 xff1a https www cnblogs com bigbrotherer p 7241845 html 登录 xff1a mysql uroot p 输入密码 xff1a xxxx 显示当前数据库 xff1a show d
  • Typescript日期Date如何做格式化字符串

    使用一个date fns format的库 安装npm i date fns save import format from 39 date fns format 39 const newdate 61 new Date console l
  • C#匿名类型和动态解析减少定义传输类模板

    C 作为强类型语言 xff0c 在序列化和反序列化 xff08 json xff09 场景中对字符串解析常常需要定义强类型模板 xff0c 造成编码上的繁琐 其实可以使用匿名类型和动态解析减少json序列化时候的数据模板定义 xff1b s
  • Sourcetree 克隆代码,将git@打头路径,写入URL显示无效路径的处理

    情景 xff1a 刚安装完sourceTree 点击 43 xff0c 进行Clone项目 xff0c 输入 git 64 195 178 10 181 datas sys git路径 xff0c 提示 无效路径 信息 处理方法 xff1a
  • docker安装samba,网络硬盘

    安装好docker之后可以安装 dperson samba 使用下面的语句 xff0c 创建默认的用户 docker run it name samba p 139 139 p 445 445 v home ftpserver smb mo
  • Linux下执行可执行文件提示:No such file or directory的解决方法

    最近在Linux下安装交叉编译链工具 xff0c 解压完成后执行却提示 xff1a No such file or directory 查了一下发现我是64位的Ubuntu系统但是执行的是32位程序 xff0c 缺少32位lib库所以无法执
  • C语言typeof详解

    typeof 是 GNU C 标准里特有的扩展 xff0c 标准的 ISO C 并没有这个关键字 xff0c 所以在编译的时候不能加任何 ISO 的 C 标准选项 xff0c 否则会报错 使用时加入 std 61 gnu90 即 GNU 的
  • S5PV210-uboot解析(二)-start.S解析

    start S解析 首先是头文件包含 include lt config h gt include lt version h gt if defined CONFIG ENABLE MMU include lt asm proc domai
  • MPV播放器快捷键

    MPV播放器快捷键 Location of user defined bindings config mpv input conf Lines starting with are comments Use SHARP to assign t
  • u-boot学习总结

    1 移植前准备 Ubuntu 配置ssh tftp nfs 烧写uboot前格式化SD卡 1 Unsupported SD reader 2 dev sdb is NOT identified Vmware出问题在控制面板 gt 程序和功能
  • setjmp和longjmp

    setjmp 和 longjmp 在C标准库 lt setjmp h gt 中 setjmp jmp buf j 必须首先被调用 它表示 使用变量 j 记录现在的位置 函数返回 0 longjmp jmp buf j int i 可以接着被
  • GNU C typeof

    typeof 是 GNU C 标准里特有的扩展 xff0c 标准的 ISO C 并没有这个关键字 xff0c 所以在编译的时候不能加任何 ISO 的 C 标准选项 xff0c 否则会报错 使用时加入 std 61 gnu90 即 GNU 的
  • linux内核常用宏container_of

    linux内核常用宏 container of 定义如下 xff1a linux 2 6 38 8 include linux kernel h container of cast a member of a structure out t

随机推荐