高德API:http://lbs.amap.com/api/webservice/guide/api/georegeo/html
http://restapi.amap.com/v3/geocode/geo?key=7de8697669288fc848e12a08f58d995e&s=rsv3&city=杭州市&address=杭州市天成路87號現代大廈git
這裏邊的key是別人開放的key,用起來感受還能夠。基本上沒有太大的限制,使用上邊的接口獲取經緯度是基本上100%成功,可是這個座標系是GCJ02座標。web
按照上邊的接口能夠返回的結果信息爲:json
{"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"浙江省杭州市江乾區現代大廈","province":"浙江省","citycode":"0571","city":"杭州市","district":"江乾區","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"330104","street":[],"number":[],"location":"120.204319,30.287145","level":"興趣點"}]}
根據GPS格式的經緯度,調用高德API返回具體地址信息:api
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string address = GetAddressByGcjLngLat("120.204319", "30.287145"); Console.WriteLine(address); Console.ReadKey(); } /// <summary> /// 根據GPS經緯度,返回對應位置的地址信息。 /// 請求API:http://restapi.amap.com/v3/geocode/regeo?output=json&location=120.204319,30.287145&key=7de8697669288fc848e12a08f58d995e&radius=100&extensions=base /// </summary> /// <param name="longitude"></param> /// <param name="latitude"></param> /// <returns></returns> public static string GetAddressByGcjLngLat(string longitude, string latitude) { Tuple<bool, string, string> 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 in adddressItems) { if (addressItem.StartsWith("formatted_address:", StringComparison.OrdinalIgnoreCase)) { address = addressItem.Replace("formatted_address:", string.Empty); break; } } } } catch (Exception ex) { // 出現異常,返回轉化失敗且不撲捉異常信息。 } // Console.WriteLine(address); return address; } /// <summary> /// 接口地址 /// 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}] /// </summary> /// <param name="lngGPS">gps格式的經度,將會被轉化爲GCJ02格式的經度</param> /// <param name="latGPS">gps格式的緯度,將會被轉化爲GCJ02格式的緯度</param> /// <returns>(bool:是否轉化成功,string:gcj02格式的經度,string:gcj02格式的緯度)</returns> public static Tuple<bool, string, string> TransformGPS2GCJ(string lngGPS, string latGPS) { Tuple<bool, string, string> result = new Tuple<bool, string, string>(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<bool, string, string>(true, lngLatItems[0].ToLower().Replace("lng:", string.Empty), lngLatItems[1].ToLower().Replace("lat:", string.Empty)); } } catch (Exception ex) { // 出現異常,返回轉化失敗且不撲捉異常信息。 } return result; } /// <summary> /// Get訪問uri並反回請求響應內容。 /// </summary> /// <param name="uri"></param> /// <param name="timeout">請求超時時間,默認:30s超時</param> /// <returns></returns> 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 = new StreamReader(myResponseStream, Encoding.UTF8)) { result = myStreamReader.ReadToEnd(); } } } return result; } } }