您的位置:首页 > 移动开发 > IOS开发

iOS百度地图简单应用( iOS地图定位(定位、地理编码与反地理编码、mapView、大头针)

2016-06-02 17:07 603 查看
导入百度SDK

注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述

NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述

下面为核心代码。

//
//  ViewController.m
//  SwalleMap
//
//  Created by dev on 16/6/1.
//  Copyright © 2016年 SWALLE. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Extension.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件

@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
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) {

//初始化反地理编码类
_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<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
效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: