MKMapView计算视图两个点之间的距离米方式比较

如题:
方式一:根据当前纬度地图每个像素所代表的米数 * 当前地图区域总共有多少像素
方式二:计算视图的点,转化到coordinate,在转化为地图上的点,计算地图两点间距离
方式三:直接用mapView的visibleMapRect,计算坐标点,在计算两地图间的距离

 MKMapView *mapview = self.otMapView.mapView;

    //当前显示范围中点的纬度
    CLLocationDegrees centerLat = mapview.centerCoordinate.latitude;
    //中点纬度,在纬度向,每个地图像素代表的米
    CLLocationDistance metersPerMapPointAtLat = MKMetersPerMapPointAtLatitude(centerLat);
    //当前显示地图的宽度包含的地图像素数量

    MKMapRect mapRect = mapview.visibleMapRect;
    MKMapRect realRect = [mapview mapRectThatFits:mapRect];
    double mapPoints = mapRect.size.width;
    double mapPoints2 = realRect.size.width;
    //当前显示范围内的宽度所代表的距离 (米) = 每个地图像素代表的米数 * 显示的地图Point数;

    //方式1
    CGFloat metersFromViewWidthAtCenterLat = metersPerMapPointAtLat * mapPoints2;

    //方式2
    CGPoint leftpt = CGPointMake(0, mapview.frame.size.height / 2);
    CGPoint rightpt = CGPointMake(mapview.frame.size.width, mapview.frame.size.height / 2);
    CGFloat metersFromViewWidthAtCenterLat2 = [self getMetersBetweenViewPointA:leftpt pointB:rightpt fromMapView:mapview];

    方式3
    MKMapPoint mA = MKMapPointMake(mapRect.origin.x, mapRect.origin.y + mapRect.size.height / 2);
    MKMapPoint mB = MKMapPointMake(mapRect.origin.x + mapRect.size.width, mapRect.origin.y + mapRect.size.height / 2);
    CGFloat metersFromViewWidthAtCenterLat3 = MKMetersBetweenMapPoints(mA,mB);

结论

metersFromViewWidthAtCenterLat  CGFloat 833805.90912895347
metersFromViewWidthAtCenterLat2 CGFloat 834171.02708121063
metersFromViewWidthAtCenterLat3 CGFloat 834171.0270812253
方式2和3结果基本一致
方式1有误差,推测误差在于地图宽度*地图纬度米/度的 此处产生的

你可能感兴趣的:(MKMapView计算视图两个点之间的距离米方式比较)