using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace gpsTobaidu
{
public class BaiduTo
{
public BaiduTo()
{
change();
}
//转换前的GPS坐标
double x = 116.397428;
double y = 39.90923;
//google 坐标转百度链接 //http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=116.32715863448607&y=39.990912172420714&callback=BMap.Convertor.cbk_3694
//gps坐标的type=0
//google坐标的type=2
//baidu坐标的type=4
void change()
{
String path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + x + "+&y=" + y + "&callback=BMap.Convertor.cbk_7594";
string res = SendDataByGET(path);
if (res.IndexOf("(") > 0 && res.IndexOf(")") > 0)
{
int sint=res.IndexOf("(") + 1;
int eint=res.IndexOf(")");
int ls = res.Length;
String str = res.Substring(sint,eint-sint);
int errint=res.IndexOf("error") + 7;
int enderr=res.IndexOf("error") + 8;
String err = res.Substring(errint, 1);
if ("0".Equals(err))
{
int sx = str.IndexOf(",\"x\":\"")+6;
int sy = str.IndexOf("\",\"y\":\"");
int endy = str.IndexOf("\"}");
int sl = str.Length;
string xp = str.Substring(sx,sy-sx);
string yp = str.Substring(sy + 7, endy - sy - 7);
byte[] outputb = Convert.FromBase64String(xp);
string XStr = Encoding.Default.GetString(outputb);
outputb = Convert.FromBase64String(yp);
string YStr = Encoding.Default.GetString(outputb);
//JSONObject js = JSONObject.fromObject(str);
编码转换
//String x1 = new String(Base64.decode(js.getString("x")));
//String y1 = new String(Base64.decode(js.getString("y")));
}
}
}
/// 通过GET方式发送数据
/// url
/// GET数据
/// GET容器
public string SendDataByGET(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}
代码二:
public MapPoint ConvertPoint(string lng,string lat)
{
string url = string.Format(@"http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x={0}&y={1}", lng,lat);
string webdata = util.GetData(url);
MapPoint point = JsonHelper.JsonDeserialize<MapPoint>(webdata);
if (point.error == "0")
{
point.x = Base64ToString(point.x);
point.y = Base64ToString(point.y);
}
return point;
}
public string Base64ToString(string base64Str)
{
byte[] byteArr = Convert.FromBase64String(base64Str);
return Encoding.UTF8.GetString(byteArr);
}
/// <summary>
/// JSON反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
}
//{"error":0,"x":"MTEyLjU1OTAwMzM2MTU2","y":"MzcuODQwODM4NzU1Mzg2"}
[DataContract]
public class MapPoint
{
[DataMember]
public string error;
[DataMember]
public string x;
[DataMember]
public string y;
}
原文链接:http://bbs.lbsyun.baidu.com/viewthread.php?tid=1410&extra=page%3D1