查找两个英国邮政编码之间的距离 [英] Find Distance between two uk postcodes
本文介绍了查找两个英国邮政编码之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了以下代码,但http://maps.google.com/maps/geo?q={0}&output=xml始终返回null。还有其他办法吗?
使用System;
02.使用System.Net;
03.using System.Web;
04.使用System.Xml;
05.
06.namespace Codebrain
07. {
08. public static class Distance
09. {
10. // Long的Handy结构/ lat信息
11. public struct Coords
12. {
13.公共双倍经度;
14.公共双倍纬度;
15.}
16.
17. //单位计算
18.公共枚举单位
19. {
20.里程,
21.公里
22.}
23.
24. //如果Google API无法找到任何邮政编码,或者国家/地区约束失败,则返回null
25.公共静态加倍? BetweenTwoPostCodes(string postcodeA,string postcodeB,string countryCodeWithin,Units units)
26. {
27. var ll1 = PostCodeToLongLat(postcodeA,countryCodeWithin);
28. if(!ll1.HasValue)返回null;
29. var ll2 = PostCodeToLongLat(postcodeB,countryCodeWithin);
30. if(!ll2.HasValue)返回null;
31. return ll1.Value.DistanceTo(ll2.Value,units);
32.}
33.
34. //英国邮政编码超载
35.公共静态加倍? BetweenTwoUKPostCodes(string postcodeA,string postcodeB)
36. {
37. return BetweenTwoPostCodes(postcodeA,postcodeB,GB,Units.Miles);
38.}
39.
40. //使用Google API解析邮政编码(在指定国家/地区内)
41. public static Coords? PostCodeToLongLat(string postcode,string countryCodeWithin)
42. {
43. //从Google下载XML响应
44. var client = new WebClient();
45. var encodedPostCode = HttpUtility.UrlEncode(postcode);
46. var url = string.Format(http://maps.google.com/maps/geo?q={0}&output=xml,encodedPostCode);
47. var xml = client.DownloadString(url);
48. var doc = new XmlDocument();
49. doc.LoadXml(xml);
50.
51. //创建自定义命名空间管理器
52. var nsmgr = new XmlNamespaceManager(doc.NameTable);
53. nsmgr.AddNamespace(ge,http://earth.google.com/kml/2.0);
54. nsmgr.AddNamespace(oa,urn:oasis:names:tc:ciq:xsdschema:xAL:2.0);
55.
56. //有什么结果吗?
57. var nodelist = doc.SelectNodes(// ge:kml / ge:Response / ge:Placemark,nsmgr);
58. if(nodelist == null || nodelist.Count == 0)返回null;
59.
60. //结果已按准确性排序,因此请先取一个
61. var node = nodelist [0];
62.
63. //检查国家约束
64. var countryname = node.SelectSingleNode(oa:AddressDetails / oa:Country / oa:CountryNameCode,nsmgr);
65. if(countryname.FirstChild.Value!= countryCodeWithin)
66. return null;
67.
68. //获取原始的长/纬坐标(我希望有一个更好的方式..
69. //也许是LeL封闭盒的平均值?)
70. var coords = node.SelectSingleNode(ge:Point / ge:coordinates,nsmgr).FirstChild.Value.Split(',');
71.双倍经度;
72.双倍数;
73. if(!Double.TryParse(coords [0],经度))返回null;
74. if(!Double.TryParse(coords [1],out lattitude))返回null;
75.
76.返回新Coords
77. {
78.经度=经度,
79.纬度= beitude
80.};
81.}
82.
83. public static double DistanceTo(此Coords from,Coords to,Units units)
85. {
85. // Haversine公式...
86. var dLat1InRad = from.Latitude *(Math.PI / 180.0);
87. var dLong1InRad = from.Longitude *(Math.PI / 180.0);
88. var dLat2InRad = to.Latitude *(Math.PI / 180.0);
89. var dLong2InRad = to.Longitude *(Math.PI / 180.0);
90.
91. var dLongitude = dLong2InRad - dLong1InRad;
92. var dLatitude = dLat2InRad - dLat1InRad;
93.
94. //中间结果a。
95. var a = Math.Pow(Math.Sin(dLatitude / 2.0),2.0)+
96. Math.Cos(dLat1InRad)* Math.Cos(dLat2InRad)*
97 .Math.Pow(Math.Sin(dLongitude / 2.0),2.0);
98.
99. //中间结果c(弧度中的大圆距离)。
100. var c = 2.0 * Math.Atan2(Math.Sqrt(a),Math.Sqrt(1.0 - a));
101.
102. //计量单位
103. var radius = 6371;
104. if(units == Units.Miles)radius = 3959;
105.
106. return radius * c;
107.}
108.}
109.}
/ xml>
解决方案
< blockquote>如果您在浏览器中加载XML,您将看到以下错误消息:
地理编码API v2已被在2013年9月9日拒绝了。现在应该使用Geocoding API v3。有关详情,请访问 https://developers.google.com/maps/documentation/geocoding/ [ ^ ]
如果您点击该链接,您会看到一个页面,其中包含有关升级到v3的详细信息:
https://developers.google.com/maps/articles/geocodingupgrade [ ^ ]
如果您仔细阅读,您会发现需要使用新端点:
https://maps.googleapis.com/maps/api/geocode/xml?address={0}
你必须使用Google Maps&放置API以获得两个地方之间的距离。请查看以下链接,了解如何:
http://www.aspsnippets.com/Articles/Google-Maps-V3-Calculate-Distance-Travel-持续时间 - 绘制 - 绘图 - 路线 - 和 - 显示 - 两个位置之间的方向.aspx [ ^ ]
I have used the following code, but http://maps.google.com/maps/geo?q={0}&output=xml, always returns null. Is there any other way to do it?
using System; 02.using System.Net; 03.using System.Web; 04.using System.Xml; 05. 06.namespace Codebrain 07.{ 08. public static class Distance 09. { 10. // Handy structure for Long/Lat information 11. public struct Coords 12. { 13. public double Longitude; 14. public double Latitude; 15. } 16. 17. // Unit calculations 18. public enum Units 19. { 20. Miles, 21. Kilometres 22. } 23. 24. // Will return a null if the Google API is unable to find either post code, or the country constraint fails 25. public static double? BetweenTwoPostCodes(string postcodeA, string postcodeB, string countryCodeWithin, Units units) 26. { 27. var ll1 = PostCodeToLongLat(postcodeA, countryCodeWithin); 28. if (!ll1.HasValue) return null; 29. var ll2 = PostCodeToLongLat(postcodeB, countryCodeWithin); 30. if (!ll2.HasValue) return null; 31. return ll1.Value.DistanceTo(ll2.Value, units); 32. } 33. 34. // Overload for UK post codes 35. public static double? BetweenTwoUKPostCodes(string postcodeA, string postcodeB) 36. { 37. return BetweenTwoPostCodes(postcodeA, postcodeB, "GB", Units.Miles); 38. } 39. 40. // Uses the Google API to resolve a post code (within the specified country) 41. public static Coords? PostCodeToLongLat(string postcode, string countryCodeWithin) 42. { 43. // Download the XML response from Google 44. var client = new WebClient(); 45. var encodedPostCode = HttpUtility.UrlEncode(postcode); 46. var url = string.Format("http://maps.google.com/maps/geo?q={0}&output=xml", encodedPostCode); 47. var xml = client.DownloadString(url); 48. var doc = new XmlDocument(); 49. doc.LoadXml(xml); 50. 51. // Create a custom namespace manager 52. var nsmgr = new XmlNamespaceManager(doc.NameTable); 53. nsmgr.AddNamespace("ge", "http://earth.google.com/kml/2.0"); 54. nsmgr.AddNamespace("oa", "urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"); 55. 56. // Any results? 57. var nodelist = doc.SelectNodes("//ge:kml/ge:Response/ge:Placemark", nsmgr); 58. if (nodelist == null || nodelist.Count == 0) return null; 59. 60. // Results are already ordered by accuracy, so take the first one 61. var node = nodelist[0]; 62. 63. // Check the Country constraint 64. var countryname = node.SelectSingleNode("oa:AddressDetails/oa:Country/oa:CountryNameCode", nsmgr); 65. if (countryname.FirstChild.Value != countryCodeWithin) 66. return null; 67. 68. // Get the raw Long/Lat coordinates (I wish there was a nicer way.. 69. // perhaps averaging the LongLat enclosing box?) 70. var coords = node.SelectSingleNode("ge:Point/ge:coordinates", nsmgr).FirstChild.Value.Split(','); 71. double longitude; 72. double lattitude; 73. if (!Double.TryParse(coords[0], out longitude)) return null; 74. if (!Double.TryParse(coords[1], out lattitude)) return null; 75. 76. return new Coords 77. { 78. Longitude = longitude, 79. Latitude = lattitude 80. }; 81. } 82. 83. public static double DistanceTo(this Coords from, Coords to, Units units) 84. { 85. // Haversine Formula... 86. var dLat1InRad = from.Latitude * (Math.PI / 180.0); 87. var dLong1InRad = from.Longitude * (Math.PI / 180.0); 88. var dLat2InRad = to.Latitude * (Math.PI / 180.0); 89. var dLong2InRad = to.Longitude * (Math.PI / 180.0); 90. 91. var dLongitude = dLong2InRad - dLong1InRad; 92. var dLatitude = dLat2InRad - dLat1InRad; 93. 94. // Intermediate result a. 95. var a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) + 96. Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) * 97. Math.Pow(Math.Sin(dLongitude / 2.0), 2.0); 98. 99. // Intermediate result c (great circle distance in Radians). 100. var c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a)); 101. 102. // Unit of measurement 103. var radius = 6371; 104. if (units == Units.Miles) radius = 3959; 105. 106. return radius * c; 107. } 108. } 109.} /xml>
解决方案
If you load the XML in the browser, you'll see the following error message:
The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/[^]
If you follow the link, you'll see a page with details on upgrading to v3:
https://developers.google.com/maps/articles/geocodingupgrade[^]
If you read through that, you'll find that you need to use the new endpoint:
"https://maps.googleapis.com/maps/api/geocode/xml?address={0}"
You have to use Google Maps & Places API to get the distance between two places. Check the link below on how to:
http://www.aspsnippets.com/Articles/Google-Maps-V3-Calculate-Distance-Travel-Duration-draw-plot-Route-and-display-Directions-between-two-locations.aspx[^]
这篇关于查找两个英国邮政编码之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文