用墨卡托和GPS坐标计算距离时误差测试

前端之家收集整理的这篇文章主要介绍了用墨卡托和GPS坐标计算距离时误差测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

iOS墨卡托和GPS坐标计算距离时误差测试,测试结果:

  • 墨卡托和gps坐标来回转换没有误差。
  • 墨卡托坐标计算出的距离比gps坐标计算出的距离大,100/92*100 = 108米,每100米多算出8米。
  • 故随着导航距离缩短,误差会逐渐变小。

log

25.780135+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 10.00
25.781216+0800 gps_mktDistanceTest[91276:1928266] gps dis = 9.20
25.781387+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624927.127331,2653334.851787
25.781531+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624927.127331,2653334.851787
25.781669+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 20.00
25.781805+0800 gps_mktDistanceTest[91276:1928266] gps dis = 18.40
25.781952+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624937.127331,2653334.851787
25.782072+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624937.127331,2653334.851787
25.782184+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 30.00
25.782318+0800 gps_mktDistanceTest[91276:1928266] gps dis = 27.59
25.782418+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624947.127331,2653334.851787
25.782547+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624947.127331,2653334.851787
25.785722+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 40.00
25.785817+0800 gps_mktDistanceTest[91276:1928266] gps dis = 36.79
25.785952+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624957.127331,2653334.851787
25.786067+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624957.127331,2653334.851787
25.786170+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 50.00
25.786280+0800 gps_mktDistanceTest[91276:1928266] gps dis = 45.99
25.786379+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624967.127331,2653334.851787
25.786473+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624967.127331,2653334.851787
25.786561+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 60.00
25.786626+0800 gps_mktDistanceTest[91276:1928266] gps dis = 55.19
25.786718+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624977.127331,2653334.851787
25.786878+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624977.127331,2653334.851787
25.787094+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 70.00
25.787273+0800 gps_mktDistanceTest[91276:1928266] gps dis = 64.38
25.787465+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624987.127331,2653334.851787
25.787647+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624987.127331,2653334.851787
25.787853+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 80.00
25.788050+0800 gps_mktDistanceTest[91276:1928266] gps dis = 73.58
25.788224+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12624997.127331,2653334.851787
25.788399+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12624997.127331,2653334.851787
25.788586+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 90.00
25.788826+0800 gps_mktDistanceTest[91276:1928266] gps dis = 82.78
25.789067+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625007.127331,2653334.851787
25.789287+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625007.127331,2653334.851787
25.789430+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 100.00
25.789628+0800 gps_mktDistanceTest[91276:1928266] gps dis = 91.98
25.789865+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625017.127331,2653334.851787
25.790090+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625017.127331,2653334.851787
25.790289+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 110.00
25.790511+0800 gps_mktDistanceTest[91276:1928266] gps dis = 101.18
25.790775+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625027.127331,2653334.851787
25.791032+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625027.127331,2653334.851787
25.791194+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 120.00
25.791390+0800 gps_mktDistanceTest[91276:1928266] gps dis = 110.37
25.791586+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625037.127331,2653334.851787
25.791812+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625037.127331,2653334.851787
25.791985+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 130.00
25.792163+0800 gps_mktDistanceTest[91276:1928266] gps dis = 119.57
25.792457+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625047.127331,2653334.851787
25.792679+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625047.127331,2653334.851787
25.792808+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 140.00
25.793005+0800 gps_mktDistanceTest[91276:1928266] gps dis = 128.77
25.793225+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625057.127331,2653334.851787
25.793416+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625057.127331,2653334.851787
25.793572+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 150.00
25.793735+0800 gps_mktDistanceTest[91276:1928266] gps dis = 137.97
25.793928+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625067.127331,2653334.851787
25.794105+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625067.127331,2653334.851787
25.794290+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 160.00
25.794419+0800 gps_mktDistanceTest[91276:1928266] gps dis = 147.16
25.794611+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625077.127331,2653334.851787
25.794792+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625077.127331,2653334.851787
25.794965+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 170.00
25.795133+0800 gps_mktDistanceTest[91276:1928266] gps dis = 156.36
25.795327+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625087.127331,2653334.851787
25.795490+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625087.127331,2653334.851787
25.795654+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 180.00
25.795776+0800 gps_mktDistanceTest[91276:1928266] gps dis = 165.56
25.795974+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625097.127331,2653334.851787
25.796177+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625097.127331,2653334.851787
25.796311+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 190.00
25.796474+0800 gps_mktDistanceTest[91276:1928266] gps dis = 174.76
25.796658+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625107.127331,2653334.851787
25.796823+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625107.127331,2653334.851787
25.796981+0800 gps_mktDistanceTest[91276:1928266] mkt dis = 200.00
25.797129+0800 gps_mktDistanceTest[91276:1928266] gps dis = 183.96
25.797310+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换前12625117.127331,2653334.851787
25.797474+0800 gps_mktDistanceTest[91276:1928266] 墨卡托坐标 转换后12625117.127331,2653334.851787


code

//
//  ViewController.m
//  gps_mktDistanceTest
//
#import "ViewController.h"
#import "HTCoorTransformEach/HTCoorTransforEach.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    for (int i = 1; i <= 20; ++i) {
        //mkt计算距离
        double x0 = 12624917.12733079;
        double y0 = 2653334.851787222;
        
        double x1 = x0 + 10 * i;
        double y1 = y0;
        
        double dis1 = sqrt(pow(x1 - x0,2) + pow(y1 - y0,2));
        NSLog(@"mkt dis = %.2f",dis1);
        
        //gps计算距离
        HTMktCoor lCoor = {x0,y0};
      
        CLLocationCoordinate2D lGPS0 = [HTCoorTransforEach Mercator2WorldGS:lCoor];
        CLLocation *lLoc0 = [[CLLocation alloc] initWithLatitude:lGPS0.latitude longitude:lGPS0.longitude];
        
        HTMktCoor lCoor1 = {x1,y1};
        CLLocationCoordinate2D lGPS1 = [HTCoorTransforEach Mercator2WorldGS:lCoor1];
        CLLocation *lLoc1 = [[CLLocation alloc] initWithLatitude:lGPS1.latitude longitude:lGPS1.longitude];
        
       HTMktCoor lMkt = [HTCoorTransforEach WorldGS2Mercator:lGPS1];
        double gpsDis = [lLoc0 distanceFromLocation:lLoc1];
        NSLog(@"gps dis = %.2f",gpsDis);
        
        NSLog(@"墨卡托坐标 转换前%f,%f",x1,y1);
        NSLog(@"墨卡托坐标 转换后%f,lMkt.x,lMkt.y);
    }
}

@end
//
//  HTCoorTransfor.h
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef struct{
    
    ///墨卡托x坐标,对应经纬度中的经度
    double x;
    
    ///墨卡托y坐标,对应经纬度中的纬度
    double y;
}HTMktCoor;
NS_ASSUME_NONNULL_BEGIN
@interface HTCoorTransforEach : NSObject
//WGS-84 坐标转换成 墨卡托 坐标
+ (HTMktCoor)WorldGS2Mercator:(CLLocationCoordinate2D)coordinate;
//墨卡托 坐标转换成 WGS-84 坐标
+ (CLLocationCoordinate2D)Mercator2WorldGS:(HTMktCoor)mercator;
@end
NS_ASSUME_NONNULL_END

//
//  HTCoorTransfor.m
//
#import "HTCoorTransforEach.h"
@implementation HTCoorTransforEach
//WGS-84 坐标转换成 墨卡托 坐标。警告:墨卡托的x对应经度longtitude,y对应纬度latitude,千万别搞反了!
+ (HTMktCoor)WorldGS2Mercator:(CLLocationCoordinate2D)coordinate
{
    double lon = coordinate.longitude*20037508.34/180;
    double lat = log(tan((90+coordinate.latitude)*M_PI/360))/(M_PI/180);
    lat = lat*20037508.34/180;
    
    HTMktCoor lCoor;
    lCoor.x = lon;
    lCoor.y = lat;
    return lCoor;
}
//墨卡托 坐标转换成 WGS-84 坐标。警告:墨卡托的x对应经度longtitude,y对应纬度latitude,千万别搞反了!
+ (CLLocationCoordinate2D)Mercator2WorldGS:(HTMktCoor)mercator
{
    double lon = mercator.x/20037508.34*180;
    double lat = mercator.y/20037508.34*180;
    lat = 180/M_PI*(2*atan(exp(lat*M_PI/180))-M_PI/2);
    return CLLocationCoordinate2DMake(lat,lon);
}
@end

猜你在找的iOS相关文章