邹越演讲,paulshop,梨子手机官网
之前项目当中的接入的高德逆地理编码功能偶尔会出现参数错误的bug,经过排查服务端异常log,发现请求的url中的location参数中的小数点变成了逗号。
代码如下
public async task<mapresult> getmapresultasync(double lat, double lng) { string url = string.format("http://restapi.amap.com/v3/geocode/regeo?output=json&location={0},{1}&key={2}", lng.tostring(), lat.tostring(), gouldak); goldermapresult res; try { string json = await httphelper.getasync(url); json = json.replace("[]", "\"\""); res = jsonconvert.deserializeobject<goldermapresult>(json); if (res.resultbase.addresscomponentbase.city.isnullorwhitespace()) res.resultbase.addresscomponentbase.city = res.resultbase.addresscomponentbase.province; } catch (exception ex) { logger.error("请求高德地图失败:" + url); throw ex; } if (res.status != 1) { logger.error("请求高德地图失败"); throw new userfriendlyexception("请求高德地图失败,状态码:" + res.status + " 说明:" + res.info + " url:" + url); } return res; }
其中 lng.tostring(), lat.tostring() 转换string的时候,偶尔会把中间的点号转成逗号,于是造成高德api返回参数错误。
后来在网上查找原因,参考这篇博客http://blog.chinaunix.net/uid-9255716-id-107923.html
对于用“.”分隔千位,用“,”分隔小数的国家,1,234.56 将会格式化成 1.234,56。如果您需要结果无论在什么语言下都是一样的,就请使用 cultureinfo.invariantculture 作为语言。
接下来就简单了,贴修改后的代码
public async task<mapresult> getmapresultasync(double lat, double lng) { string url = string.format("http://restapi.amap.com/v3/geocode/regeo?output=json&location={0},{1}&key={2}", lng.tostring(cultureinfo.invariantculture), lat.tostring(cultureinfo.invariantculture), gouldak); goldermapresult res; try { string json = await httphelper.getasync(url); json = json.replace("[]", "\"\""); res = jsonconvert.deserializeobject<goldermapresult>(json); if (res.resultbase.addresscomponentbase.city.isnullorwhitespace()) res.resultbase.addresscomponentbase.city = res.resultbase.addresscomponentbase.province; } catch (exception ex) { logger.error("请求高德地图失败:" + url); throw ex; } if (res.status != 1) { logger.error("请求高德地图失败"); throw new userfriendlyexception("请求高德地图失败,状态码:" + res.status + " 说明:" + res.info + " url:" + url); } return res; }
只是把 lng.tostring(), lat.tostring() 替换成了 lng.tostring(cultureinfo.invariantculture), lat.tostring(cultureinfo.invariantculture) ,这样这个bug就解决了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论