百度地图坐标转换API和地图API

时间:2023-03-09 00:19:27
百度地图坐标转换API和地图API

利用百度地图的服务将经纬度转换为米单位坐标

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double[,] LonLat = new double[, ] { { 114.21892734521, 29.575429778924 }, { 114.21892734521, 29.575429778924 } }; string url = string.Format("http://api.map.baidu.com/geoconv/v1/?coords={0},{1};{2},{3}&from=1&to=6&ak=你的密钥", LonLat[,], LonLat[,], LonLat[,], LonLat[,]);
string res = SendRequest(url, Encoding.UTF8);
Debug.WriteLine(res);
int x = res.IndexOf("\"x\":");
int y = res.IndexOf("\"y\":");
int endy = res.IndexOf("}");
string xx = res.Substring(x+ , y - x - );
string yy = res.Substring(y + , endy - y-);
Console.Write(xx+"米 ");
Console.WriteLine(yy + "米 ");
x = res.IndexOf("\"x\":", x+);
y = res.IndexOf("\"y\":",y+);
endy = res.IndexOf("}",endy+);
xx = res.Substring(x + , y - x - );
yy = res.Substring(y + , endy - y - );
Console.Write(xx+"米 ");
Console.WriteLine(yy + "米");
}
/// <summary>
/// Get方式获取url地址输出内容
/// </summary> /// <param name="url">url</param>
/// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param>
public static String SendRequest(String url, Encoding encoding)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
return sr.ReadToEnd();
}
}
}

结果:

百度地图坐标转换API和地图API

官方开发说明:http://developer.baidu.com/map/changeposition.htm

获取密钥:http://lbsyun.baidu.com/apiconsole/key

我自己的密钥:http://lbsyun.baidu.com/apiconsole/key

http://developer.baidu.com/map/jsdemo.htm#a1_2

利用密钥还建立了一个显示地图的网页

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=密钥"></script>
<title>地图展示</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap"); // 创建Map实例
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
</script>

百度地图坐标转换API和地图API

一个有关地图的开源项目:

http://greatmaps.codeplex.com/SourceControl/latest

百度地图坐标转换API和地图API

百度地图09坐标转wgs84坐标

1.百度09坐标转火星坐标系

2.火星坐标系转wgs84坐标系

/// <summary>
/// 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换 帮助类
/// </summary>
public static class WGSGCJLatLonHelper
{
/*
*参考:
*1.http://blog.csdn.net/yorling/article/details/9175913
*2.http://kongxz.com/2013/10/wgs-cgj/
*3.https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#EvilTransform.cs
*4.https://github.com/googollee/eviltransform
*5.https://github.com/shenqiliang/WGS2Mars
*
*WGS84:World Geodetic System 1984,是为GPS全球定位系统使用而建立的坐标系统。通过遍布世界的卫星观测站观测到
*的坐标建立,其初次WGS84的精度为1-2m,在1994年1月2号,通过10个观测站在GPS测量方法上改正,得到了WGS84
*(G730),G表示由GPS测量得到,730表示为GPS时间第730个周。1996年,National Imagery and Mapping Agency (NIMA)
*为美国国防部 (U.S.Departemt of Defense, DoD)做了一个新的坐标系统。这样实现了新的WGS版本:WGS(G873)。其因为
*加入了USNO站和北京站的改正,其东部方向加入了31-39cm 的改正。所有的其他坐标都有在1分米之内的修正。
*
*GCJ-02:国家保密插件,也叫做加密插件或者加偏或者SM模组,其实就是对真实坐标系统进行人为的加偏处理,按照几行代码的算
*法,将真实的坐标加密成虚假的坐标,而这个加偏并不是线性的加偏,所以各地的偏移情况都会有所不同。而加密后的坐标也常
*被人称为火星坐标系统。GCJ-02 意味“国测局-2002”,也就是说,这是国家测绘局于2002年弄出的标准。
*/
#region 常量以及私有方法
const double pi = 3.14159265358979324;//Math.PI;//;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
private static double TransformLat(double lonX, double latY)
{
double _ret = -100.0 + 2.0 * lonX + 3.0 * latY + 0.2 * latY * latY + 0.1 * lonX * latY + 0.2 * Math.Sqrt(Math.Abs(lonX));
_ret += (20.0 * Math.Sin(6.0 * lonX * pi) + 20.0 * Math.Sin(2.0 * lonX * pi)) * 2.0 / 3.0;
_ret += (20.0 * Math.Sin(latY * pi) + 40.0 * Math.Sin(latY / 3.0 * pi)) * 2.0 / 3.0;
_ret += (160.0 * Math.Sin(latY / 12.0 * pi) + * Math.Sin(latY * pi / 30.0)) * 2.0 / 3.0;
return _ret;
}
private static double TransformLon(double lonX, double latY)
{
double _ret = 300.0 + lonX + 2.0 * latY + 0.1 * lonX * lonX + 0.1 * lonX * latY + 0.1 * Math.Sqrt(Math.Abs(lonX));
_ret += (20.0 * Math.Sin(6.0 * lonX * pi) + 20.0 * Math.Sin(2.0 * lonX * pi)) * 2.0 / 3.0;
_ret += (20.0 * Math.Sin(lonX * pi) + 40.0 * Math.Sin(lonX / 3.0 * pi)) * 2.0 / 3.0;
_ret += (150.0 * Math.Sin(lonX / 12.0 * pi) + 300.0 * Math.Sin(lonX / 30.0 * pi)) * 2.0 / 3.0;
return _ret;
}
private static MyPoint Transform(MyPoint point)
{
MyPoint _transPoint = new MyPoint();
double _lat = TransformLat(point.LonX - 105.0, point.LatY - 35.0);
double _lon = TransformLon(point.LonX - 105.0, point.LatY - 35.0);
double _radLat = point.LatY / 180.0 * pi;
double _magic = Math.Sin(_radLat);
_magic = - ee * _magic * _magic;
double _sqrtMagic = Math.Sqrt(_magic);
_lat = (_lat * 180.0) / ((a * ( - ee)) / (_magic * _sqrtMagic) * pi);
_lon = (_lon * 180.0) / (a / _sqrtMagic * Math.Cos(_radLat) * pi);
_transPoint.LatY = _lat;
_transPoint.LonX = _lon; return _transPoint;
}
#endregion
#region 火星坐标转 (GCJ-02)地球坐标(WGS-84)
/// <summary>
/// 火星坐标转 (GCJ-02)地球坐标(WGS-84)
/// </summary>
/// <param name="gcjPoint">火星坐标转 (GCJ-02)</param>
/// <returns>地球坐标(WGS-84)</returns>
public static MyPoint GCJ02ToWGS84(MyPoint gcjPoint)
{
if (outOfChina(gcjPoint))
{
return gcjPoint;
}
MyPoint _transPoint = Transform(gcjPoint);
return new MyPoint(gcjPoint.LatY - _transPoint.LatY, gcjPoint.LonX - _transPoint.LonX);
}
#endregion
#region 地球坐标(WGS-84)转火星坐标 (GCJ-02)
/// <summary>
/// 地球坐标(WGS-84)转火星坐标 (GCJ-02)
/// </summary>
/// <param name="wgsPoint">地球坐标(WGS-84)</param>
/// <returns>火星坐标 (GCJ-02)</returns>
public static MyPoint WGS84ToGCJ02(MyPoint wgsPoint)
{
if (outOfChina(wgsPoint))
{
return wgsPoint;
}
MyPoint _transPoint = Transform(wgsPoint);
return new MyPoint(wgsPoint.LatY + _transPoint.LatY, wgsPoint.LonX + _transPoint.LonX);
}
#endregion
#region 火星坐标系转百度坐标系
public static MyPoint GCJ02ToBD09(MyPoint gcjpoint)
{
double x = gcjpoint.LonX, y = gcjpoint.LatY;
double z =Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * pi);
double theta =Math.Atan2(y, x) + 0.000003 *Math.Cos(x * pi);
double bd_lon = z * Math.Cos(theta) + 0.0065;
double bd_lat = z * Math.Sin(theta) + 0.006;
return new MyPoint(bd_lat + 0.006, bd_lon);
}
#endregion
#region 百度坐标系转火星坐标系
public static MyPoint BD09ToGCJ02(MyPoint bdpoint)
{
double x = bdpoint.LonX - 0.0065, y = bdpoint.LatY - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * pi);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * pi);
double gg_lon = z * Math.Cos(theta);
double gg_lat = z * Math.Sin(theta);
return new MyPoint(gg_lat, gg_lon);
}
#endregion
#region 百度坐标转GPS坐标
public static MyPoint BD09ToWGS84(MyPoint bdpoint)
{
MyPoint gcj02 = BD09ToGCJ02(bdpoint);
MyPoint wgs84 = GCJ02ToWGS84(gcj02);
return wgs84;
}
#endregion
private static bool outOfChina(MyPoint pt)
{
if (pt.LonX < 72.004 || pt.LonX > 137.8347)
return true; if (pt.LatY < 0.8293 || pt.LatY > 55.8271)
return true; return false;
} }
public class MyPoint
{
public double LonX
{
get;
set;
} public double LatY
{
get;
set;
}
public MyPoint(double latY, double lonX)
{
LatY = latY;
LonX = lonX;
}
public MyPoint()
{ }
}