ios 百度地图定位
2017-05-13 18:11
357 查看
#import "ViewController.h"
#import "UIView+Extension.h"
#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入云检索功能所有的头文件
#import //引入定位功能所有的头文件
#import //引入计算工具所有的头文件
#import //引入周边雷达功能所有的头文件
#import //只引入所需的单个头文件
@interface ViewController ()
{
BMKPinAnnotationView *newAnnotation;
BMKGeoCodeSearch *_geoCodeSearch;
BMKReverseGeoCodeOption *_reverseGeoCodeOption;
}
@property (nonatomic, strong) BMKLocationService *localService;
@property (weak, nonatomic) UIButton *mapPin;
@end
@implementation ViewController
-(BMKLocationService *)localService{
if (!_localService) {
_localService = [[BMKLocationService alloc] init];
[_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度
}
return _localService;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
self.mapPin = [UIButton buttonWithType:UIButtonTypeSystem];//大头针
self.mapPin.width = 40;
self.mapPin.height = 80;
self.mapPin.center = self.mapView.center;
[self.mapPin setBackgroundImage:[UIImage imageNamed:@"serach_Map"] forState:UIControlStateNormal];
self.mapPin.backgroundColor = [UIColor greenColor];
[self.mapView addSubview:self.mapPin];
[self.view addSubview:self.mapView];
self.mapView.zoomLevel=17;//比例尺
[self.mapView setMapType:BMKMapTypeStandard];//地图类型
self.mapView.delegate = self;
self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
self.mapView.showsUserLocation = YES;//显示定位图层
self.localService.delegate = self;
[self.localService startUserLocationService];//用户开始定位
//self.mapView.showsUserLocation = NO;//先关闭显示的定位图层
self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
self.mapView.showsUserLocation = YES;//显示定位图层
[self.mapView bringSubviewToFront:self.mapPin];
}
#pragma mark -- BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
self.mapView.showsUserLocation = YES;//显示定位图层
//设置地图中心为用户经纬度
[self.mapView updateLocationData:userLocation];
// _mapView.centerCoordinate = userLocation.location.coordinate;
BMKCoordinateRegion region ;//表示范围的结构体
region.center = self.mapView.centerCoordinate;//中心点
region.span.latitudeDelta = 0.004;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
region.span.longitudeDelta = 0.004;//纬度范围
[self.mapView setRegion:region animated:YES];
}
#pragma mark -- BMKMapViewDelegate
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
//屏幕坐标转地图经纬度
CLLocationCoordinate2D MapCoordinate=[_mapView convertPoint:_mapPin.center toCoordinateFromView:_mapView];
NSLog(@"latitude == %f longitude == %f",MapCoordinate.latitude,MapCoordinate.longitude);
if (_geoCodeSearch==nil) {
//初始化地理编码类
_geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
_geoCodeSearch.delegate = self;
}
if (_reverseGeoCodeOption==nil) {
bcf1
//初始化反地理编码类
_reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
}
//需要逆地理编码的坐标位置
_reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;
[_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];
//创建地理编码对象
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
//创建位置
CLLocation *location=[[CLLocation alloc]initWithLatitude:MapCoordinate.latitude longitude:MapCoordinate.longitude];
//反地理编码
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<</span>CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//判断是否有错误或者placemarks是否为空
if (error !=nil || placemarks.count==0) {
NSLog(@"%@",error);
return ;
}
for (CLPlacemark *placemark in placemarks) {
//赋值详细地址
NSLog(@"%@",placemark.name);
}
}];
}
#pragma mark -- BMKGeoCodeSearchDelegate
//周边信息
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
for (BMKPoiInfo *poi in result.poiList) {
NSLog(@"%@",poi.name);//周边建筑名
NSLog(@"%d",poi.epoitype);
}
}
@end
#import "UIView+Extension.h"
#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入云检索功能所有的头文件
#import //引入定位功能所有的头文件
#import //引入计算工具所有的头文件
#import //引入周边雷达功能所有的头文件
#import //只引入所需的单个头文件
@interface ViewController ()
{
BMKPinAnnotationView *newAnnotation;
BMKGeoCodeSearch *_geoCodeSearch;
BMKReverseGeoCodeOption *_reverseGeoCodeOption;
}
@property (nonatomic, strong) BMKLocationService *localService;
@property (weak, nonatomic) UIButton *mapPin;
@end
@implementation ViewController
-(BMKLocationService *)localService{
if (!_localService) {
_localService = [[BMKLocationService alloc] init];
[_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度
}
return _localService;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
self.mapPin = [UIButton buttonWithType:UIButtonTypeSystem];//大头针
self.mapPin.width = 40;
self.mapPin.height = 80;
self.mapPin.center = self.mapView.center;
[self.mapPin setBackgroundImage:[UIImage imageNamed:@"serach_Map"] forState:UIControlStateNormal];
self.mapPin.backgroundColor = [UIColor greenColor];
[self.mapView addSubview:self.mapPin];
[self.view addSubview:self.mapView];
self.mapView.zoomLevel=17;//比例尺
[self.mapView setMapType:BMKMapTypeStandard];//地图类型
self.mapView.delegate = self;
self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
self.mapView.showsUserLocation = YES;//显示定位图层
self.localService.delegate = self;
[self.localService startUserLocationService];//用户开始定位
//self.mapView.showsUserLocation = NO;//先关闭显示的定位图层
self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
self.mapView.showsUserLocation = YES;//显示定位图层
[self.mapView bringSubviewToFront:self.mapPin];
}
#pragma mark -- BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
self.mapView.showsUserLocation = YES;//显示定位图层
//设置地图中心为用户经纬度
[self.mapView updateLocationData:userLocation];
// _mapView.centerCoordinate = userLocation.location.coordinate;
BMKCoordinateRegion region ;//表示范围的结构体
region.center = self.mapView.centerCoordinate;//中心点
region.span.latitudeDelta = 0.004;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
region.span.longitudeDelta = 0.004;//纬度范围
[self.mapView setRegion:region animated:YES];
}
#pragma mark -- BMKMapViewDelegate
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
//屏幕坐标转地图经纬度
CLLocationCoordinate2D MapCoordinate=[_mapView convertPoint:_mapPin.center toCoordinateFromView:_mapView];
NSLog(@"latitude == %f longitude == %f",MapCoordinate.latitude,MapCoordinate.longitude);
if (_geoCodeSearch==nil) {
//初始化地理编码类
_geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
_geoCodeSearch.delegate = self;
}
if (_reverseGeoCodeOption==nil) {
bcf1
//初始化反地理编码类
_reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
}
//需要逆地理编码的坐标位置
_reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;
[_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];
//创建地理编码对象
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
//创建位置
CLLocation *location=[[CLLocation alloc]initWithLatitude:MapCoordinate.latitude longitude:MapCoordinate.longitude];
//反地理编码
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<</span>CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//判断是否有错误或者placemarks是否为空
if (error !=nil || placemarks.count==0) {
NSLog(@"%@",error);
return ;
}
for (CLPlacemark *placemark in placemarks) {
//赋值详细地址
NSLog(@"%@",placemark.name);
}
}];
}
#pragma mark -- BMKGeoCodeSearchDelegate
//周边信息
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
for (BMKPoiInfo *poi in result.poiList) {
NSLog(@"%@",poi.name);//周边建筑名
NSLog(@"%d",poi.epoitype);
}
}
@end
相关文章推荐
- iOS 蓝牙定位 iBeacons
- IOS &nbsp; 手势
- CSS&nbsp;Sprites&nbsp;:&nbsp;样式表贴图定位&nbsp;&amp;nd…
- 下iOS5 和iOS 4.3版本的函数差异 …
- iOS UI一些设置方法
- IOS Objective c 同步 异步链接 …
- 谷歌下周推Google Drive iOS应用程…
- IOS 关闭广告
- ios 应用程序测试
- Xcode4 ios SDK 4.3 安装初…
- IOS 关灯游戏
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- 第二天 营火 《苹果iOS实例…
- 第一天 午夜倒数《苹果iOS实…
- iOS 使用socket连接Internet
- 百度地图定位跳转 (iOS)
- iOS&nbsp;获取当点位置,并绘制当…
- 第四天 图像移动《苹果iOS实…
- 30个常用动画机制 ios
- IOS 彩虹灯 程序