public static async Task<string> GetAddressForCoordinates(double latitude,double longitude) { HttpClient httpClient = new HttpClient {BaseAddress = new Uri("http://nominatim.openstreetmap.org")}; HttpResponseMessage httpResult = await httpClient.GetAsync( String.Format("reverse?format=json&lat={0}&lon={1}",latitude,longitude)); JsonObject jsonObject = JsonObject.Parse(await httpResult.Content.ReadAsStringAsync()); return jsonObject.GetNamedObject("address").GetNamedString("road");
}
如何得到相反的(如果地址是已知的坐标)?
UPDATE
我为此增加赏金;我已经(如上所示)是反向地理编码(获取坐标的地址);我需要的是地理编码(获取地址的坐标).
根据我上面的反向地理编码代码,我猜这可能是这样的:
public static async Task<string> GetCoordinatesForAddress(string address) { HttpClient httpClient = new HttpClient {BaseAddress = new Uri("http://nominatim.openstreetmap.org")}; HttpResponseMessage httpResult = await httpClient.GetAsync( String.Format("format=json&address={0}",address)); JsonObject jsonObject = JsonObject.Parse(await httpResult.Content.ReadAsStringAsync()); return jsonObject.GetNamedObject("address").GetNamedString("lat"); // <-- what about "lon"? }
…但是我不知道如何组合两个坐标(经度和纬度)值(假设这是正确的或接近正确的).任何人都可以验证这一点,清理它,或提供更好的例子(使用nominatim或其他)?
更新2
回答Peter Ritchie的问题/评论如下:
在原始(反向地理编码代码)中,我有:
return jsonObject.GetNamedObject("address").GetNamedString("road");
它只是返回路;所以我假设像“157河滨大道”一样.
但是,对于地理编码(需要两个值,经度和纬度),我有这个伪代码:
return jsonObject.GetNamedObject("address").GetNamedString("lat"); // <-- what about "lon"?
所以我不知道是否需要从Task< string>中更改返回值.到任务<列出和调用(详细的伪代码)[注意:我有一个困难的时候逃脱角色括号为任务与列表的字符串]:
var latitude jsonObject.GetNamedObject("address").GetNamedString("lat"); var longitude jsonObject.GetNamedObject("address").GetNamedString("lat"); List<string> listCoordinates = new List<string>(); listCoordinates.Add(latitude); listCoordinates.Add(longitude); return listCoordinates;
…或者这样:
string latitude jsonObject.GetNamedObject("address").GetNamedString("lat"); string longtude jsonObject.GetNamedObject("address").GetNamedString("long"); return string.Format("{0};{1}",longitude);
…要么 ???
更新3
响应提供的Json代码进行地理编码:
根据原来的反向地理编码代码,不应该更像是这样:
HttpClient httpClient = new HttpClient { BaseAddress = new Uri("http://nominatim.openstreetmap.org/") }; var httpResult = await httpClient.GetAsync( String.Format("search?format=json&addressdetails={0}",address);
…但无论如何:
JArray类型不能被识别,
虽然JsonValue是JValue类型,但是无法识别JValue类型.
JsonConverter类型无法识别;也许是Json.Net的一部分?
var result = await httpResult.Content.ReadAsStringAsync(); var r = (JsonArray)JsonConverter.DeserializeObject(result);//<-- JsonConvert[er] not recognized; part of Json.NET? var latString = ((JsonValue)r[0]["lat"]).ValueType as string; var longString = ((JsonValue)r[0]["lon"]).ValueType as string;
…但是即使这样(接近但没有Bob Seger),JsonConvert以及JsonConverter都不被识别.
更新4
通过http://wiki.openstreetmap.org/wiki/Nominatim#Search的文档更加一致地发表了口号,我认为我的原始(反向地理编码)方法可能会更好:
public static async Task`<string`> GetAddressForCoordinates(double latitude,double longitude) { HttpClient httpClient = new HttpClient {BaseAddress = new Uri("http://nominatim.openstreetmap.org/")}; HttpResponseMessage httpResult = await httpClient.GetAsync( String.Format("reverse?format=json&lat={0}&lon={1}",longitude)); JsonObject jsonObject = JsonObject.Parse(await httpResult.Content.ReadAsStringAsync()); string house = jsonObject.GetNamedObject("addressparts").GetNamedString("house"); string road = jsonObject.GetNamedObject("addressparts").GetNamedString("road"); string city = jsonObject.GetNamedObject("addressparts").GetNamedString("city"); string state = jsonObject.GetNamedObject("addressparts").GetNamedString("state"); string postcode = jsonObject.GetNamedObject("addressparts").GetNamedString("postcode"); string country = jsonObject.GetNamedObject("addressparts").GetNamedString("country"); return string.Format("{0} {1},{2},{3} {4} ({5})",house,road,city,state,postcode,country); }
这将返回,对于相应的坐标参数传递,如:“157河滨大道,香槟,IL 55555(美国)”
关于文件我觉得奇怪的是地址部分中没有“状态”元素;如果这真的是真的,而不仅仅是一个文档监督,我上面的代码将在GetNamedString(“state”)的调用失败.
我仍然不能确定正确的语法等于相反的(geocode)方法,在传入地址后获得坐标.
更新5
好的,我下载了Json.NET并编译它.我还没有测试,但我已经将彼得·里奇(Peter Ritchie)标记为(50分)答案.
这是我使用的代码:
public static async Task<string> GetCoordinatesForAddress(string address) { HttpClient httpClient = new HttpClient { BaseAddress = new Uri("http://nominatim.openstreetmap.org/") }; HttpResponseMessage httpResult = await httpClient.GetAsync( String.Format("search?q={0}&format=json&addressdetails=1",Pluggify(address))); // In my Pluggify() method,I replace spaces with + and then lowercase it all var result = await httpResult.Content.ReadAsStringAsync(); var r = (JArray)JsonConvert.DeserializeObject(result); var latString = ((JValue)r[0]["lat"]).Value as string; var longString = ((JValue)r[0]["lon"]).Value as string; return string.Format("{0};{1}",latString,longString); }
也:
一个有趣的事情发生在回到这个论坛的路上:在通过NuGet安装Json.NET的同时,我也看到“.NET的最快的JSON串行器由ServiceStack”声称比Json.NET快3倍. FIWW,它最近比Json.NET更新.思考/反应?
更新6
我有这个代码实现这个(应用程序ID和代码已被更改以保护半无辜
(我)):
// If address has not been explicitly entered,try to suss it out: address = textBoxAddress1.Text.Trim(); lat = textBoxLatitude1.Text.Trim(); lng = textBoxLongitude1.Text.Trim(); if (string.IsNullOrWhiteSpace(address)) { address = await SOs_Classes.SOs_Utils.GetAddressForCoordinates(lat,lng); } . . . public async static Task<string> GetAddressForCoordinates(string latitude,string longitude) { string currentgeoLoc = string.Format("{0},{1}",longitude); string queryString = string.Empty; string nokiaAppID = "j;dsfj;fasdkdf"; object nokiaAppCode = "-14-14-1-7-47-178-78-4"; var hereNetUrl = string.Format( "http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2} &app_code={3}&accept=application/json",currentgeoLoc,queryString,nokiaAppID,nokiaAppCode); // get data from HERE.net REST API var httpClient = new HttpClient(); var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl); // deseralize JSON from Here.net using (var tr = new StringReader(hereNetResponse)) using (var jr = new JsonTextReader(tr)) { var rootObjectResponse = new JsonSerializer ().Deserialize<JsonDOTNetHelperClasses.RootObject>(jr); var firstplace = rootObjectResponse.results.items.First(); return HtmlUtilities.ConvertToText(firstplace.vicinity); // NOTE: There is also a title (such as "Donut Shop","Fire stations",etc.?) and type (such as "residence" or "business",etc.?) } }
…但在GetAddressForCoordinates()中的这一行:
var firstplace = rootObjectResponse.results.items.First();
…我得到这个错误msg:“* System.InvalidOperationException未被用户代码处理
的HResult = -2146233079
Message = Sequence不包含元素
来源= System.Core程序
堆栈跟踪:
在System.Linq.Enumerable.First [TSource](IEnumerable`1源)
在SpaceOverlays.SOs_Classes.SOs_Utils.d__12.MoveNext()在c:… *“
hereNetResponse的值是:
{"results":{"items":[]},"search":{"context":{"location":{"position":[38.804967,-90.113183],"address": {"postalCode":"62048","city":"Hartford","stateCode":"IL","county":"Madison","countryCode":"USA","country":" USA","text":"Hartford IL 62048 USA"}},"type":"urn:nlp-types:place","href":"http://demo.places.nlp.nokia.com/places/v1/places/loc- dmVyc2lvbj0xO3RpdGxlPUhhcnRmb3JkO2xhdD0zOC44MDQ5Njc7bG9uPS05MC4xMTMxODM7Y2l0eT1IY XJ0Zm9yZDtwb3N0YWxDb2RlPTYyMDQ4O2NvdW50cnk9VVNBO3N0YXRlQ29kZT1JTDtjb3VudHk9TWFka XNvbjtjYXRlZ29yeUlkPWNpdHktdG93bi12aWxsYWdl;context=Zmxvdy1pZD02YmUzZDM4Yi0wNGVhLTUyM jgtOWZmNy1kNWNkZGM0ODI5OThfMTM1NzQyMDI1NTg1M18wXzE2MA? app_id=F6zpNc3TjnkiCLwl_Xmh&app_code=QoAM_5BaVDZvkE2jRvc0mw"}}}
…所以看起来里面有有效的信息,比如应该返回“哈特福德,伊利诺伊州”
无论如何,一个空白的返回值不应该抛出异常,我会想…
解决方法
您必须解析结果(XML,JSON或HTML)以获取您感兴趣的字段.
更新1:
关于如何处理实际价值观:这取决于.如果要查看表单中的坐标,可以简单地将拉长和长字符串放入单独的控件中.如果你想把它放在一个控件中,可以使用string.Format(“{0},{1}”,longString).如果要对Windows Store应用程序使用各种方法/类型的coords,则可能需要使用Microsoft.Maps.MapControl.Location类.例如:
Double latNumber; Double longNumber; if(false == Double.TryParse(latString,out latNumber)) throw new InvalidOperationException(); if(false == Double.TryParse(longString,out longNumber)) throw new InvalidOperationException(); var location = new Location(latNumber,longNumber);
上面假设你已经从响应中提取了lat和long,并将它们分别放在latString,longString中.
一些接口可能需要lat / long作为单独的double值,在这种情况下,只需使用latNumber和longNumber.
除此之外,它真的取决于您要使用的接口.但是,上面应该给你足够的使用大多数接口.
更新2:
如果问题不是“如何获取坐标”,而是“如何解析json对象”,那么我建议使用JSon.Net来获取json结果中的lat / long字符串.例如:
var httpClient = new HttpClient(); var httpResult = await httpClient.GetAsync( "http://nominatim.openstreetmap.org/search?q=135+pilkington+avenue,+birmingham&format=json&polygon=1&addressdetails=1"); var result = await httpResult.Content.ReadAsStringAsync(); var r = (JArray) JsonConvert.DeserializeObject(result); var latString = ((JValue) r[0]["lat"]).Value as string; var longString = ((JValue)r[0]["lon"]).Value as string;
…见上文w.r.t.如何使用latString和longString