ios 百度地图指定区域_IOS 百度地图定位,显示地理位置

最近公司要做一个类似与美团的东西,让我用百度地图来进行定位,并显示地理信息.

预览图

那么我们要如何做呢,直接上代码

先看看包结构,要把需要用到的库都要引入进来.

包结构

注意:appdelegate.mm 后缀一定要加个m

不然的话编译会不通过,好像是因为它的编译原理是c++还是怎样,要详细了解的可以百度.

那么来看控制器代码

_locService = [[BMKLocationService alloc]init];//定位功能的初始化

_locService.delegate = self;//设置代理位self

//启动LocationService

[_locService startUserLocationService];//启动定位服务

_geocodesearch = [[BMKGeoCodeSearch alloc] init];

//编码服务的初始化(就是获取经纬度,或者获取地理位置服务)

_geocodesearch.delegate = self;//设置代理为self

这段代码需要放在viewDidLoad里面

启动定位服务后,_locService.userLocation.location.coordinate就应该会有值了,也就是说经度和纬度都可以获取出来了.

开始定位的点击事件,将刚刚定位的经纬度取出来并设置lable的值

-(void)heheda:(UIButton *)btn{

NSLog(@"进入普通定位态");

NSLog(@"定位的经度:%f,定位的纬度:%f",_locService.userLocation.location.coordinate.longitude,_locService.userLocation.location.coordinate.latitude);

self.lon.text = [NSString stringWithFormat:@"%f",_locService.userLocation.location.coordinate.longitude];

[self.lat setText:[NSString stringWithFormat:@"%f",_locService.userLocation.location.coordinate.latitude]];

}

获取地址的点击事件

-(void)onClickReverseGeocode //发送反编码请求的.

{

isGeoSearch = false;

CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//初始化

if (_locService.userLocation.location.coordinate.longitude!= 0

&& _locService.userLocation.location.coordinate.latitude!= 0) {

//如果还没有给pt赋值,那就将当前的经纬度赋值给pt

pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude,

_locService.userLocation.location.coordinate.longitude};

}

BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反编码请求

reverseGeocodeSearchOption.reverseGeoPoint = pt;//设置反编码的店为pt

BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//发送反编码请求.并返回是否成功

if(flag)

{

NSLog(@"反geo检索发送成功");

}

else

{

NSLog(@"反geo检索发送失败");

}

}

如果发送成功,百度将会返回东西给你,然后你可以在下面这个代理函数中处理

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

if (error == 0) {

BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];

item.coordinate = result.location;

item.title = result.address;

NSString* titleStr;

NSString* showmeg;

titleStr = @"反向地理编码";

showmeg = [NSString stringWithFormat:@"%@",item.title];

self.addr.text = showmeg;

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];

[myAlertView show];

}

}

效果图

获取百度地图可视区域范围的数据,你可以使用以下方法: 1. 获取当前地图的中心点坐标: ``` CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate; ``` 2. 获取当前地图的缩放级别: ``` float zoomLevel = mapView.zoomLevel; ``` 3. 计算出当前地图可视区域的四个顶点坐标: ``` BMKCoordinateRegion region = BMKCoordinateRegionMakeWithDistance(centerCoordinate, 1000 * pow(2, (21 - zoomLevel)), 1000 * pow(2, (21 - zoomLevel))); CLLocationCoordinate2D topLeftCoordinate = CLLocationCoordinate2DMake(centerCoordinate.latitude + region.span.latitudeDelta / 2.0, centerCoordinate.longitude - region.span.longitudeDelta / 2.0); CLLocationCoordinate2D bottomLeftCoordinate = CLLocationCoordinate2DMake(centerCoordinate.latitude - region.span.latitudeDelta / 2.0, centerCoordinate.longitude - region.span.longitudeDelta / 2.0); CLLocationCoordinate2D topRightCoordinate = CLLocationCoordinate2DMake(centerCoordinate.latitude + region.span.latitudeDelta / 2.0, centerCoordinate.longitude + region.span.longitudeDelta / 2.0); CLLocationCoordinate2D bottomRightCoordinate = CLLocationCoordinate2DMake(centerCoordinate.latitude - region.span.latitudeDelta / 2.0, centerCoordinate.longitude + region.span.longitudeDelta / 2.0); ``` 4. 将四个顶点坐标转换成百度地图的墨卡托坐标: ``` BMKMapPoint topLeftPoint = BMKMapPointForCoordinate(topLeftCoordinate); BMKMapPoint bottomLeftPoint = BMKMapPointForCoordinate(bottomLeftCoordinate); BMKMapPoint topRightPoint = BMKMapPointForCoordinate(topRightCoordinate); BMKMapPoint bottomRightPoint = BMKMapPointForCoordinate(bottomRightCoordinate); ``` 5. 计算出当前地图可视区域百度地图中的矩形区域: ``` BMKMapRect visibleRect = BMKMapRectMake(fmin(topLeftPoint.x, bottomLeftPoint.x), fmin(topLeftPoint.y, topRightPoint.y), fabs(topLeftPoint.x - topRightPoint.x), fabs(topLeftPoint.y - bottomLeftPoint.y)); ``` 6. 将矩形区域转换成经纬度坐标范围: ``` BMKCoordinateRegion visibleRegion = BMKCoordinateRegionForMapRect(visibleRect); CLLocationCoordinate2D visibleTopLeftCoordinate = visibleRegion.center; visibleTopLeftCoordinate.latitude += visibleRegion.span.latitudeDelta / 2.0; visibleTopLeftCoordinate.longitude -= visibleRegion.span.longitudeDelta / 2.0; CLLocationCoordinate2D visibleBottomRightCoordinate = visibleRegion.center; visibleBottomRightCoordinate.latitude -= visibleRegion.span.latitudeDelta / 2.0; visibleBottomRightCoordinate.longitude += visibleRegion.span.longitudeDelta / 2.0; ``` 最终,`visibleTopLeftCoordinate` 和 `visibleBottomRightCoordinate` 就是当前地图可视区域的经纬度坐标范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值