2012-05-20 68 views

回答

4

當然,這完全取決於您想要執行的實際搜索。假設您想在英國找到所有以Lon開頭的地點。將執行該搜索(作爲一個例子,多少可以爲真正的搜索更改)的網址是:

http://api.geonames.org/search?name_startsWith=lon&country=GB&maxRows=10&username=demo 

可以彈出,在您的瀏覽器,並查看結果:

<geonames style="MEDIUM"> 
<totalResultsCount>334</totalResultsCount> 
<geoname> 
    <toponymName>London</toponymName> 
    <name>London</name> 
    <lat>51.50853</lat> 
    <lng>-0.12574</lng> 
    <geonameId>2643743</geonameId> 
    <countryCode>GB</countryCode> 
    <countryName>United Kingdom</countryName> 
    <fcl>P</fcl> 
    <fcode>PPLC</fcode> 
</geoname> 
<geoname> 
    <toponymName>Lone</toponymName> 
    <name>Lone</name> 
    <lat>58.33333</lat> 
    <lng>-4.88333</lng> 
    <geonameId>2643732</geonameId> 
    <countryCode>GB</countryCode> 
    <countryName>United Kingdom</countryName> 
    <fcl>P</fcl> 
    <fcode>PPL</fcode> 
</geoname> 
<!-- and so on ... --> 
</geonames> 

注意您需要每個geoname下的latlng元素。隨着LINQ到XML(包括在您的命名空間聲明System.LinqSystem.Linq.Xml):

var xml = XElement.Load("http://api.geonames.org/search?name_startsWith=lon&country=GB&maxRows=10&username=demo"); 

var locations = xml.Descendants("geoname").Select(g => new { 
        Name = g.Element("name").Value, 
        Lat = g.Element("lat").Value, 
        Long = g.Element("lng").Value 
       }); 

foreach (var location in locations) 
{ 
    Console.WriteLine("{0}: {1}, {2}", location.Name, location.Lat, location.Long); 
} 

當然,你可以選擇不同的方式使用這些值,你可能要解析LatLong成雙打。

+0

這樣做。謝謝! – Megaoctane

相關問題