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.配置包
此步骤可以参照高德官方文档
例子: ios9
2.代码声明
3.声明定位服务key (申请地图key是一样的)
4.开始定位 (代码)