ios-关于使用高德地图api心得(1)

本文介绍如何使用高德地图SDK在iOS应用中实现地图功能,包括获取屏幕中心点经纬度、设置默认显示地区、计算两点间距离及持续定位等关键技术。

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

1.获取当前屏幕中心点的经纬度

MACoordinateRegion region;
CLLocationCoordinate2D centerCoordinate = mapView.region.center;
region.center= centerCoordinate;

NSLog(@"屏幕中心的经纬度------ %f,%f",centerCoordinate.latitude, centerCoordinate.longitude);

2.初始化,设置进入地图默认位置,图层

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    [MAMapServices sharedServices].apiKey = @"72539163b2a7d6e50b58044d0b263a40";

    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    self.mapView.delegate = self;

    //设置地图默认显示的地区的经纬度
    MACoordinateSpan span = MACoordinateSpanMake(8,8);
    MACoordinateRegion region;
    //113.757234 23.025191   23.120154,113.280029  112.448342 23.062575
    region.center = CLLocationCoordinate2DMake(23.062575,112.448342);
    region.span = span;

    [self.view addSubview:self.mapView];

    //解决地图越拉近,指定的坐标越偏右的问题。
    MAMapRect rect = MAMapRectForCoordinateRegion(region);
    [self.mapView setVisibleMapRect:rect animated:YES];

    [self.mapView setRegion:region  animated:YES];



}

3.计算两个坐标的距离

计算方法

另一种算法

double dd = M_PI/180;
double x1=centerCoordinate.latitude*dd,x2=getCenterLatitudeFloat*dd;
double y1=centerCoordinate.longitude*dd,y2=getCenterLongitudeFloat*dd;
double R = 6371004;
double myDistance = (2*R*asin(sqrt(2-2*cos(x1)*cos(x2)*cos(y1-y2) - 2*sin(x1)*sin(x2))/2));

4.定位(持续更新)
4.1.配置包

此步骤可以参照高德官方文档
引入jar包

例子: ios9

单独引入

2.代码声明
代码的编写

3.声明定位服务key (申请地图key是一样的)
声明定位

4.开始定位 (代码)
开始定位01

开始定位02

开始定位03

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值