usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;namespaceConsoleApp1
{classProgram
{static void Main(string[] args)
{string address = GetAddressByGcjLngLat("120.204319", "30.287145");
Console.WriteLine(address);
Console.ReadKey();
}///
///根据GPS经纬度,返回对应位置的地址信息。///请求API:http://restapi.amap.com/v3/geocode/regeo?output=json&location=120.204319,30.287145&key=7de8697669288fc848e12a08f58d995e&radius=100&extensions=base///
///
///
///
public static string GetAddressByGcjLngLat(string longitude, stringlatitude)
{
Tuple gcjLngLat =TransformGPS2GCJ(longitude, latitude);//Console.WriteLine("{0}:{1}:{2}", gcjLngLat.Item1, gcjLngLat.Item2, gcjLngLat.Item3);
if (false ==gcjLngLat.Item1)return string.Empty;string address = string.Empty;try{//格式:http://restapi.amap.com/v3/geocode/regeo?output=json&location=120.204319,30.287145&key=7de8697669288fc848e12a08f58d995e&radius=100&extensions=base
string content = HttpGet("http://restapi.amap.com/v3/geocode/regeo?output=json&location=" + gcjLngLat.Item2 + "," + gcjLngLat.Item3 + "&key=7de8697669288fc848e12a08f58d995e&radius=200&extensions=base");//返回值格式:{"status":"1","info":"OK","infocode":"10000","regeocode":{"formatted_address":"浙江省杭州市江干区闸弄口街道万家花园(天城路)","addressComponent":{"country":"中国","province":"浙江省","city":"杭州市","citycode":"0571","district":"江干区","adcode":"330104","township":"闸弄口街道","towncode":"330104007000","neighborhood":{"name":"万家花园(天城路)","type":"商务住宅;住宅区;住宅小区"},"building":{"name":[],"type":[]},"streetNumber":{"street":"天城路","number":"87号","location":"120.204319,30.2871444","direction":"南","distance":"0.0617778"},"businessAreas":[{"location":"120.20604745161295,30.28125106451613","name":"火车东站","id":"330104"}]}}}
string[] adddressItems = content.Replace("\"", string.Empty).Split(new char[] { '{', ',', '}', '[', ']'});if (adddressItems != null && adddressItems.Length > 0)
{foreach (string addressItem inadddressItems)
{if (addressItem.StartsWith("formatted_address:", StringComparison.OrdinalIgnoreCase))
{
address= addressItem.Replace("formatted_address:", string.Empty);break;
}
}
}
}catch(Exception ex)
{//出现异常,返回转化失败且不扑捉异常信息。
}//Console.WriteLine(address);
returnaddress;
}///
///接口地址/// http://api.zdoz.net/transmore.ashx
///接口说明///批量纠偏,一次最大可纠偏1000个坐标点///参数///lats:维度,多个维度用“;”隔开///lngs:经度,多个经度用“;”隔开(要注意经纬度个数相等)///type:转换类型 【1.WGS -> GCJ】 【2.GCJ -> WGS】 【3.GCJ -> BD】 【4.BD -> GCJ】 【5.WGS -> BD】 【6.BD -> WGS】///返回值JSON///根据次序返回一个json格式的数组///演示///参数:lats=34.123;34.332;55.231&lngs=113.123;112.213;115.321&type=1///返回:[{"Lng":113.12942937312582,"Lat":34.121761850760855},{"Lng":112.21911710957568,"Lat":34.3306763095054}, {"Lng":115.33036232125529,"Lat":55.232930158541052}]///
/// gps格式的经度,将会被转化为GCJ02格式的经度
/// gps格式的纬度,将会被转化为GCJ02格式的纬度
/// (bool:是否转化成功,string:gcj02格式的经度,string:gcj02格式的纬度)
public static Tuple TransformGPS2GCJ(string lngGPS, stringlatGPS)
{
Tuple result = new Tuple(false, string.Empty, string.Empty);try{//格式:http://api.zdoz.net/transmore.ashx?lats=34.123&lngs=113.123&type=1
string content = HttpGet("http://api.zdoz.net/transmore.ashx?lats=" + latGPS + "&lngs=" + lngGPS + "&type=1");//返回值格式:[{"Lng":113.12942937312582,"Lat":34.121761850760855}]
content = content.Replace("[{", string.Empty).Replace("}]", string.Empty).Replace("\"", string.Empty);string[] lngLatItems = content.Split(new char[] { ','});if (lngLatItems != null && lngLatItems.Length == 2 && lngLatItems[0].StartsWith("lng", StringComparison.OrdinalIgnoreCase) && lngLatItems[1].StartsWith("lat", StringComparison.OrdinalIgnoreCase))
{
result= new Tuple(true, lngLatItems[0].ToLower().Replace("lng:", string.Empty), lngLatItems[1].ToLower().Replace("lat:", string.Empty));
}
}catch(Exception ex)
{//出现异常,返回转化失败且不扑捉异常信息。
}returnresult;
}///
///Get访问uri并反回请求响应内容。///
///
/// 请求超时时间,默认:30s超时
///
public static string HttpGet(string uri, int timeout = 30 * 1000)
{string result = string.Empty;
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(uri);
request.Method= "GET";
request.ContentType= "text/html;charset=UTF-8";
request.Timeout=timeout;using (HttpWebResponse response =(HttpWebResponse)request.GetResponse())
{using (Stream myResponseStream =response.GetResponseStream())
{using (StreamReader myStreamReader = newStreamReader(myResponseStream, Encoding.UTF8))
{
result=myStreamReader.ReadToEnd();
}
}
}returnresult;
}
}
}