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

本文介绍了如何在iOS应用中集成百度地图SDK,包括设置info.plist文件以获取定位权限,以及核心定位和地图显示的相关代码。适用于需要在iOS应用中实现地图定位功能的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入百度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
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值