iOS 使用百度地图,仿滴滴打车的定位方法。拖动时时定位
先看下效果,没有录屏,放张图片,如果想体验效果可以看看滴滴打车的定位,我这里没有写动画效果
参考http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=15752
这里的思路:
(1)把图片放到屏幕的中间,这样在拖动的时候就不会跟随着地图移动了。
(2)百度地图提供了,View坐标和地理坐标转换的方法。正式这个方法的存在,方便我们及时的获取拖动后的,屏幕中间的图片所在位置的经纬度。
当拖动地图的时候,定位的图片一直在屏幕的中央,当拖动停止的时候会显示出具体的信息
#import "HouseTypeMapVC.h"@interface HouseTypeMapVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{BMKLocationService * _locService;}
@property (nonatomic,strong) UIView * locationView;
@property (nonatomic,strong) UIImageView * locImageView;
@property (nonatomic,strong) UIView * messageView;
@property (nonatomic,strong) UILabel * addressLabel;
@property (nonatomic,strong) UIButton * sureButton;
@property (nonatomic,strong) NSString * name;
@property (nonatomic,assign) CLLocationCoordinate2D location2D;@property (nonatomic, strong)BMKGeoCodeSearch* searchAddress;
@property (strong, nonatomic) IBOutlet BMKMapView *mapView;
@property (nonatomic,strong)BMKUserLocation *userLocation; //定位功能
@end@implementation HouseTypeMapVC-(void)viewWillAppear:(BOOL)animated{[super viewWillAppear: animated];[_mapView viewWillAppear];_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放_locService.delegate = self;
}-(void)viewWillDisappear:(BOOL)animated{[_mapView viewWillDisappear];_mapView.delegate = nil; // 不用时,置nil_locService.delegate = nil;
}- (void)viewDidLoad {[super viewDidLoad];
}- (void)initUI{[self goBackBtn];_mapView.mapType=BMKMapTypeStandard;[self initlocationService];}#pragma mark --initlocationService--定位-(void)initlocationService{_locService = [[BMKLocationService alloc]init];[_locService startUserLocationService];_mapView.showsUserLocation = NO;//先关闭显示的定位图层_mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态_mapView.showsUserLocation = YES;//显示定位图层_mapView.showMapScaleBar = YES;//显示比例尺_mapView.zoomLevel = 17;//地图显示的级别_searchAddress = [[BMKGeoCodeSearch alloc]init];_searchAddress.delegate = self;}//这里是创建中心显示的图片和显示详细地址的View- (void)createLocationSignImage{//LocationView定位在当前位置,换算为屏幕的坐标,创建的定位的图标self.locationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 28, 35)];self.locImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28, 35)];self.locImageView.image = [UIImage imageNamed:@"myLocation"];[self.locationView addSubview:self.locImageView];//messageView 展示定位信息的View和Label和buttonself.messageView = [[UIView alloc]init];self.messageView.backgroundColor = [UIColor whiteColor];//把当前定位的经纬度换算为了View上的坐标CGPoint point = [self.mapView convertCoordinate:_mapView.centerCoordinate toPointToView:_mapView];//当解析出现错误的时候,会出现超出屏幕的情况,一种是大于了屏幕,一种是小于了屏幕if(point.x > ScreenWidth || point.x < ScreenWidth/5){point.x = _mapView.centerX;point.y = _mapView.centerY-64;}NSLog(@"Point------%f-----%f",point.x,point.y);//重新定位了LocationViewself.locationView.center = point;[self.locationView setFrame:CGRectMake(point.x-14, point.y-18, 28, 35)];//重新定位了messageView[self.messageView setFrame:CGRectMake(30, point.y-40-20, SCREEN_WIDTH-60, 40)];//展示地址信息的labelself.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.messageView.frame.size.width - 80, 40)];self.addressLabel.font = [UIFont systemFontOfSize:13.0f];[self.messageView addSubview:self.addressLabel];//把地址信息传递到上个界面的buttonself.sureButton = [[UIButton alloc]initWithFrame:CGRectMake(self.addressLabel.frame.origin.x + self.addressLabel.frame.size.width, 0,self.messageView.frame.size.width - self.addressLabel.frame.origin.x - self.addressLabel.frame.size.width, 40)];[self.messageView addSubview:self.sureButton];self.sureButton.backgroundColor = [UIColor colorWithHex:0x2ecb7d];[self.sureButton setTitle:@"确定" forState:UIControlStateNormal];self.sureButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];[self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[self.sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];[self.mapView addSubview:self.messageView];[self.mapView addSubview:self.locationView];}/***用户位置更新后,会调用此函数*@param userLocation 新的用户位置*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{BMKCoordinateRegion region;region.center.latitude = userLocation.location.coordinate.latitude;region.center.longitude = userLocation.location.coordinate.longitude;region.span.latitudeDelta = 0;region.span.longitudeDelta = 0;NSLog(@"当前的坐标是:%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);[_mapView updateLocationData:userLocation];[_locService stopUserLocationService];//取消定位 这个一定要写,不然无法移动定位了_mapView.centerCoordinate = userLocation.location.coordinate;NSLog(@" _mapView.centerCoordinate------%f-----%f", _mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude);[self createLocationSignImage];}//确定按钮的点击- (void)sureButtonClick:(UIButton *)button{if([self.chosseAddressDelegate respondsToSelector:@selector(chosseAddressBack:name:point:)]){[self.chosseAddressDelegate chosseAddressBack:self.addressLabel.text name:self.name point:self.location2D];}[self.navigationController popViewControllerAnimated:YES];
}- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view{NSLog(@"点击了");CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};pt=(CLLocationCoordinate2D){mapView.region.center.latitude,mapView.region.center.longitude};BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init];option.reverseGeoPoint = pt;BOOL flag=[_searchAddress reverseGeoCode:option];if (flag) {
// _mapView.showsUserLocation=NO;//不显示自己的位置}
}//地图被拖动的时候,需要时时的渲染界面,当渲染结束的时候我们就去定位然后获取图片对应的经纬度- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus*)status{NSLog(@"onDrawMapFrame");
}- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{NSLog(@"regionWillChangeAnimated");
}- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{NSLog(@"regionDidChangeAnimated");CGPoint touchPoint = self.locationView.center;CLLocationCoordinate2D touchMapCoordinate =[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init];option.reverseGeoPoint = touchMapCoordinate;BOOL flag=[_searchAddress reverseGeoCode:option];if (flag) {// _mapView.showsUserLocation=NO;//不显示自己的位置}
}//定位自己的位置
- (IBAction)locationButtonClick:(UIButton *)sender {[_locService startUserLocationService];_mapView.showsUserLocation = NO;//先关闭显示的定位图层_mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态_mapView.showsUserLocation = YES;//显示定位图层
}//点击地图的空白区域- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{NSLog(@"onClickedMapBlank-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);
}//点击地图中的背景有标记的区域- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi{NSLog(@"点击onClickedMapPoi---%@",mapPoi.text);CLLocationCoordinate2D coordinate = mapPoi.pt;//长按之前删除所有标注NSArray *arrayAnmation=[[NSArray alloc] initWithArray:_mapView.annotations];[_mapView removeAnnotations:arrayAnmation];//设置地图标注BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];annotation.coordinate = coordinate;annotation.title = mapPoi.text;[_mapView addAnnotation:annotation];BMKReverseGeoCodeOption *re = [[BMKReverseGeoCodeOption alloc] init];re.reverseGeoPoint = coordinate;[SVProgressHUD show];[_searchAddress reverseGeoCode:re];BOOL flag =[_searchAddress reverseGeoCode:re];if (!flag){NSLog(@"search failed!");}
}//根据经纬度返回点击的位置的名称
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{[SVProgressHUD dismiss];NSString * resultAddress = @"";NSString * houseName = @"";CLLocationCoordinate2D coor = result.location;if(result.poiList.count > 0){BMKPoiInfo * info = result.poiList[0];if([info.name rangeOfString:@"-"].location != NSNotFound){houseName = [info.name componentsSeparatedByString:@"-"][0];}else{houseName = info.name;}resultAddress = [NSString stringWithFormat:@"%@%@",result.address,info.name];}else{resultAddress =result.address;}if(resultAddress.length == 0){self.addressLabel.text = @"位置解析错误,请拖动重试!";return;}self.addressLabel.text = resultAddress;self.location2D = coor;self.name = houseName;
}//点击一个大头针- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{NSLog(@"点击didSelectAnnotationView-");
}/***在地图View将要启动定位时,会调用此函数*@param mapView 地图View*/
- (void)willStartLocatingUser
{NSLog(@"start locate");
}/***用户方向更新后,会调用此函数*@param userLocation 新的用户位置*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{[_mapView updateLocationData:userLocation];NSLog(@"heading is %@",userLocation.heading);
}/***在地图View停止定位后,会调用此函数*@param mapView 地图View*/
- (void)didStopLocatingUser
{NSLog(@"stop locate");
}/***定位失败后,会调用此函数*@param mapView 地图View*@param error 错误号,参考CLError.h中定义的错误号*/
- (void)didFailToLocateUserWithError:(NSError *)error
{NSLog(@"location error");
}- (void)dealloc {if (_mapView) {_mapView = nil;}
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end
iOS 使用百度地图,仿滴滴打车的定位方法。拖动时时定位相关推荐
- android 仿饿了么地图,iOS Andriod百度地图仿百度外卖 饿了么 选择我的地址 POI检索...
title: iOS Andriod百度地图仿百度外卖 饿了么 选择我的地址 POI检索 date: 2015-09-19 21:06:26 tags: 百度外卖选择送货地址:饿了么选择送货地址: 第 ...
- IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应
(转)IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应 IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油 ...
- IOS开发百度地图API
IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油站 IOS百度地图视角跳到用户当前位置 IOS百度地图开发实时路况 IO ...
- IOS开发百度地图API-用点生成路线,导航,气泡响应
原地址:http://blog.sina.com.cn/s/blog_68661bd80101k4rx.html IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发 ...
- Android端地图,百度地图学习(II)-Android端的定位
哎,经历了小编的最近时间的研究,我的百度定位终于成功啦,刹那间觉得自己萌萌哒啦(- ̄▽ ̄)- 话不多说,直接进入正题: 首先,我们来看一下效果: [分析定位原理] [编码分析] 2)增加布局信息:此处 ...
- ios 开发百度地图的使用
IOS开发百度地图API-用点生成路线,导航,气泡响应 IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油站 IOS百度地 ...
- 转:IOS开发百度地图API-用点生成路线,导航,气泡响应
IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油站 IOS百度地图视角跳到用户当前位置 IOS百度地图开发实时路况 IO ...
- Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!...
Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...
- 百度地图查找我的位置定位服务器,百度地图手机版如何进行我的位置定位?
不知大家有没有使用过百度地图手机版我的位置定位功能?百度地图的位置定位功能很不错,我的位置定位让我们随时分享位置信息,可以通过这个位置定位功能让其他的好友都知道我们的具体所在位置,真是一个很实用的功能 ...
- Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!
Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...
最新文章
- 解决使用Navicat等工具进行连接登录mysql的1130错误,无法使用Ip远程连接的问题(mysql为8.0版本)
- Adobe Flash Builder 4.6 开发环境详解
- TPYBoard:一款可以发挥无限创意的MicroPython开发板
- OpenCV运行分类深度学习网络的实例(附完整代码)
- GNU ARM汇编--(十七)u-boot的makefile和mkconfig解读
- scrapy框架结构
- Python内存管理以及垃圾回收机制
- U大师U盘装系统——原版XP系统安装(V1.2.0版)
- Cesium中的坐标系及转换
- 第三:启发式搜索:A* 算法
- java tomcat jvm内存_【转】Linux下tomcat JVM内存
- 2021-09-08推荐系统有如下三大类算法
- 【Unity3D】GUI控件
- 畅谈无线通信系统物理层之系统概述
- PC微信防撤回多开补丁 v0.6
- 四叉树与八叉树原理 / AABB OBB / 碰撞检测优化
- vmbox 宿主机ssh登录虚拟机
- 设计模式学习之访问者模式
- Wireshark分析网络慢
- php 系统分隔符,php脚本由哪个分隔符包围