高德地图:地理/逆地理编码
Posted yy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高德地图:地理/逆地理编码相关的知识,希望对你有一定的参考价值。
高德API:http://lbs.amap.com/api/webservice/guide/api/georegeo/
http://restapi.amap.com/v3/geocode/geo?key=7de8697669288fc848e12a08f58d995e&s=rsv3&city=杭州市&address=杭州市天成路87号现代大厦
这里边的key是别人开放的key,用起来感觉还可以。基本上没有太大的限制,使用上边的接口获取经纬度是基本上100%成功,但是这个坐标系是GCJ02坐标。
按照上边的接口可以返回的结果信息为:
{"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返回具体地址信息:
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; } } }
以上是关于高德地图:地理/逆地理编码的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 调用高德地图APP实现位置搜索路线规划逆地理编码