gps坐标离线转百度坐标

坐标系简介

WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
GCJ-02:中国坐标偏移标准,Google
Map、高德、腾讯使用 BD-09:百度坐标偏移标准,Baidu Map使用

在线gps坐标转百度坐标

在百度地图中,如果直接给出gps坐标,将造成偏差,需要将gps坐标转换成百度坐标
如在百度地图中给出gps坐标(120.1755556, 35.94)

在线转换后的百度坐标为
在线转换结果


离线gps转百度地址

参考地址:http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
原文给出的代码为java,稍加修改为C#代码

测试结果

在给出代码以前先给出测试结果

gps坐标离线转百度坐标_第1张图片

与在线转换结果比较,小数点前4位为为精确,在民用中已经足够


gps坐标转百度坐标
相关文件下载:http://download.csdn.net/detail/u014124220/9645257


相关代码

在线测试代码

该效果测试html代码,同时也包含在线坐标转换的方法


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gp2312" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=9fb983ecd9b505f8fedcc9ab07c65e3e">script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js">script>
<title>GPS转百度title>
head>
<body>
<div id="allmap">div>
body>
html>
<script type="text/javascript">
//GPS坐标
var xx = 120.1755556;
var yy = 35.94;
var gpsPoint = new BMap.Point(xx,yy);

//地图初始化
var bm = new BMap.Map("allmap");
bm.centerAndZoom(gpsPoint, 17);
bm.addControl(new BMap.NavigationControl());

//添加谷歌marker和label
var markergps = new BMap.Marker(gpsPoint);
bm.addOverlay(markergps); //添加GPS标注
var labelgps = new BMap.Label("我是GPS标注哦",{offset:new BMap.Size(20,-10)});
markergps.setLabel(labelgps); //添加GPS标注

//坐标转换完之后的回调函数
translateCallback = function (point){
    var marker = new BMap.Marker(point);
    bm.addOverlay(marker);
    var label = new BMap.Label("我是百度标注哦",{offset:new BMap.Size(20,-10)});
    marker.setLabel(label); //添加百度label
    bm.setCenter(point);
    alert("转化为百度坐标为:"+point.lng + "," + point.lat);
}

setTimeout(function(){
    BMap.Convertor.translate(gpsPoint,0,translateCallback);     //真实经纬度转成百度坐标
}, 2000);
script>

————–离线代码——————

java和C#代码一并给出,相关变量如下

pi: 圆周率。 a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
ee: 椭球的偏心率。
x_pi: 圆周率转换量。
transformLat(lat, lon): 转换方法,比较复杂,不必深究了。输入:横纵坐标,输出:转换后的横坐标。
transformLon(lat, lon): 转换方法,同样复杂,自行脑补吧。输入:横纵坐标,输出:转换后的纵坐标。
wgs2gcj(lat, lon): WGS坐标转换为GCJ坐标。 gcj2bd(lat, lon): GCJ坐标转换为百度坐标。

离线转换 C#参考代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mr_newbiebt
{
    class gps2bd
    {
        /*
         WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
         GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
         BD-09:百度坐标偏移标准,Baidu Map使用

        代码参考:http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
         */

        static double pi = 3.14159265358979324;
        static double a = 6378245.0;
        static double ee = 0.00669342162296594323;
        public const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

        /// 
        /// gps坐标转换成百度坐标,小数点前4位为准确坐标
        /// 
        /// 纬度
        /// 经度
        /// 
        public static double[] wgs2bd(double lat, double lon)
        {
            double[] dwgs2gcj = wgs2gcj(lat, lon);
            double[] dgcj2bd = gcj2bd(dwgs2gcj[0], dwgs2gcj[1]);
            return dgcj2bd;
        }

        public static double[] gcj2bd(double lat, double lon)
        {
            double x = lon, y = lat;
            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
            double bd_lon = z * Math.Cos(theta) + 0.0065;
            double bd_lat = z * Math.Sin(theta) + 0.006;
            return new double[] { bd_lat, bd_lon };
        }

        public static double[] bd2gcj(double lat, double lon)
        {
            double x = lon - 0.0065, y = lat - 0.006;
            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
            double gg_lon = z * Math.Cos(theta);
            double gg_lat = z * Math.Sin(theta);
            return new double[] { gg_lat, gg_lon };
        }

        public static double[] wgs2gcj(double lat, double lon)
        {
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            double[] loc = { mgLat, mgLon };
            return loc;
        }

        private static double transformLat(double lat, double lon)
        {
            double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
            ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(lon * pi) + 40.0 * Math.Sin(lon / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.Sin(lon / 12.0 * pi) + 320 * Math.Sin(lon * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        private static double transformLon(double lat, double lon)
        {
            double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
            ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(lat * pi) + 40.0 * Math.Sin(lat / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.Sin(lat / 12.0 * pi) + 300.0 * Math.Sin(lat / 30.0 * pi)) * 2.0 / 3.0;
            return ret;
        }
    }
}

离线转换 java参考代码

static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

public static double[] wgs2bd(double lat, double lon) {
       double[] wgs2gcj = wgs2gcj(lat, lon);
       double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]);
       return gcj2bd;
}

public static double[] gcj2bd(double lat, double lon) {
       double x = lon, y = lat;
       double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
       double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
       double bd_lon = z * Math.cos(theta) + 0.0065;
       double bd_lat = z * Math.sin(theta) + 0.006;
       return new double[] { bd_lat, bd_lon };
}

public static double[] bd2gcj(double lat, double lon) {
       double x = lon - 0.0065, y = lat - 0.006;
       double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
       double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
       double gg_lon = z * Math.cos(theta);
       double gg_lat = z * Math.sin(theta);
       return new double[] { gg_lat, gg_lon };
}

public static double[] wgs2gcj(double lat, double lon) {
       double dLat = transformLat(lon - 105.0, lat - 35.0);
       double dLon = transformLon(lon - 105.0, lat - 35.0);
       double radLat = lat / 180.0 * pi;
       double magic = Math.sin(radLat);
       magic = 1 - ee * magic * magic;
       double sqrtMagic = Math.sqrt(magic);
       dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
       dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
       double mgLat = lat + dLat;
       double mgLon = lon + dLon;
       double[] loc = { mgLat, mgLon };
       return loc;
}

private static double transformLat(double lat, double lon) {
       double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
       ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
       ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
       ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi  / 30.0)) * 2.0 / 3.0;
       return ret;
}

private static double transformLon(double lat, double lon) {
       double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
       ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
       ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
       ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
       return ret;
}

你可能感兴趣的:(C#)