当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS之高德地图显示出地图并定位成功

IOS之高德地图显示出地图并定位成功

2018年10月03日  | 移动技术网移动技术  | 我要评论

任务:显示地图在界面上并成功定位。

一:我们导入高德地图的api

在podfile

platform :ios, '7.0'  
target ‘你的项目名称’ do  
pod 'amap3dmap' 
pod ‘amapsearch’
pod ‘amaplocation’
end

注意:target中 正确输入你自己的项目名称

cd 进项目文件夹 pod install 成功后进入下一步。

二:加载地图视图

在.h文件中

#import 
#import 
#import 


@interface mainpagevc : uiviewcontroller

@property (nonatomic,strong)mamapview *mapview;

@end

在.m文件中

- (void)viewdidload{
    
    [amapservices sharedservices].enablehttps = yes;
    
    _mapview = [[mamapview alloc]initwithframe:cgrectmake(0, self.navigationcontroller.navigationbar.frame.size.height + [[uiapplication sharedapplication]statusbarframe].size.height, self.view.frame.size.width, self.view.frame.size.height - (self.navigationcontroller.navigationbar.frame.size.height + [[uiapplication sharedapplication]statusbarframe].size.height))];
    
    //delegate
    [_mapview setdelegate:self];
    
    //是否显示指南针
    [_mapview setshowscompass:yes];
    
    //是否显示比例尺
    [_mapview setshowsscale:yes];
    
    //地图旋转
    [_mapview setrotateenabled:no];
    
    //地图倾斜
    [_mapview setrotatecameraenabled:no];
    
    //设置缩放级别
    [_mapview setzoomlevel:15];
    
    //是否显示用户位置
    _mapview.showsuserlocation = yes;
    
    //设置导航模式为跟随
    _mapview.usertrackingmode = mausertrackingmodefollow ;
    
    
    [self.view addsubview:_mapview];
}

至此,能够正确显示mapview了(不过显示的是北京的或者是你模拟器设置的坐标点的地方地图)

 

三:定位

3.1

获取key值(从这个网页里获得):点击打开链接

https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key

appdelegate.m 中添加设置key值

#import 
@interface appdelegate ()

@end

@implementation appdelegate


- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
    
    [amapservices sharedservices].apikey = @"987e1c858fb578f98b3c39e9516ecf96";
    
    return yes;
}

3.2

给定位权限:

在info.plist中添加这三个权限

privacy - location always and when in use usage description

privacy - location always usage description

privacy - location when in use usage description

在app transport security settings 中添加allow arbitrary loads 为 yes

\

其后设置

\

 

3.3

#import 
- (void)location{
    
    if([cllocationmanager locationservicesenabled]){
    
        amaplocationmanager *locationmanager = [[amaplocationmanager alloc]init];
        
        [locationmanager setdelegate:self];
        
        //是否允许后台定位。默认为no。只在ios 9.0及之后起作用。设置为yes的时候必须保证 background modes 中的 location updates 处于选中状态,否则会抛出异常。由于ios系统限制,需要在定位未开始之前或定位停止之后,修改该属性的值才会有效果。
        [locationmanager setallowsbackgroundlocationupdates:no];
        
        //指定定位是否会被系统自动暂停。默认为no。
        [locationmanager setpauseslocationupdatesautomatically:no];
        
        //设定定位的最小更新距离。单位米,默认为 kcldistancefilternone,表示只要检测到设备位置发生变化就会更新位置信息
        [locationmanager setdistancefilter:20];
        
        //设定期望的定位精度。单位米,默认为 kcllocationaccuracybest
        [locationmanager setdesiredaccuracy:kcllocationaccuracybest];
        
        //开始定位服务
        [locationmanager startupdatinglocation];
        
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网