c# - 有没有办法根据坐标获取地名?

标签 c# geolocation coordinates windows-store-apps bing-maps

给定一个位置的坐标,有没有办法得到地名?

我指的不是地址,我指的是这个地方的“标题”,例如:

Coordinates: 76.07, -62.0 // or whatever they are
Address: 157 Riverside Avenue, Champaign, Illinois
Place name: REO Speedwagon's Rehearsal Spot

-或者:

Coordinates: 76.07, -62.0 // or whatever they are
Address: 77 Sunset Strip, Hollywood, CA
Place name: Famous Amos Cookies

那么是否有反向地理编码 Web 服务或我可以调用的东西,a la:

string placeName = GetPlaceNameForCoordinates(76.07, -62.0)

...这将返回“Wal*Mart”或“Columbia Jr. College”或任何合适的内容?

我找到了对其他语言的引用,例如 java 和 ios(我猜是 Objective C),但没有具体说明如何在 Windows 商店应用程序中使用 C# 执行此操作...

更新

我已经有了这个用于获取地址(改编自 Freeman 的“Metro Revealed:使用 XAML 和 C# 构建 Windows 8 应用程序”第 75 页):

public static async Task<string> GetStreetAddressForCoordinates(double latitude, double longitude)
{
    HttpClient httpClient = new HttpClient();
    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 string.format("{0} {1}", jsonObject.GetNamedObject("address").GetNamedString("house"),
                                    jsonObject.GetNamedObject("address").GetNamedString("road"));
 }

...但我在他们的文档中看不到地名;他们似乎提供了房屋、道路、村庄、城镇、城市、县、邮政编码和国家,但没有地名。

最佳答案

我通常存储纬度/经度坐标,然后使用 GMaps 查找位置,然后“尽最大努力”- 使用地址查找地点名称- 再次通过 Google map 。

static string baseUri = 
  "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
string location = string.Empty;

public static void RetrieveFormatedAddress(string lat, string lng)
{
    string requestUri = string.Format(baseUri, lat, lng);

    using (WebClient wc = new WebClient())
    {
        string result = wc.DownloadString(requestUri);
        var xmlElm = XElement.Parse(result);
        var status = (from elm in xmlElm.Descendants() where 
            elm.Name == "status" select elm).FirstOrDefault();
        if (status.Value.ToLower() == "ok")
        {
            var res = (from elm in xmlElm.Descendants() where 
                elm.Name == "formatted_address" select elm).FirstOrDefault();
            requestUri = res.Value;
        }
    }
}

编辑:

这是一个简单的逆向版本:

public static Coordinate GetCoordinates(string region)
{
    using (var client = new WebClient())
    {

        string uri = "http://maps.google.com/maps/geo?q='" + region + 
          "'&output=csv&key=sadfwet56346tyeryhretu6434tertertreyeryeryE1";

        string[] geocodeInfo = client.DownloadString(uri).Split(',');

        return new Coordinate(Convert.ToDouble(geocodeInfo[2]), 
                   Convert.ToDouble(geocodeInfo[3]));
    }
}

public struct Coordinate
{
    private double lat;
    private double lng;

    public Coordinate(double latitude, double longitude)
    {
        lat = latitude;
        lng = longitude;

    }

    public double Latitude { get { return lat; } set { lat = value; } }
    public double Longitude { get { return lng; } set { lng = value; } }

}

关于c# - 有没有办法根据坐标获取地名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13689631/

相关文章:

c# - ASP.NET MVC 中的问题模型绑定(bind)嵌套列表

javascript - 我如何使用 Javascript 将 Lat 和 Long 转换为英国邮政编码

php - 搜索用户距离内的项目(地理位置)PHP/MySQL

android - 如何将手机的屏幕坐标映射到电脑屏幕?

string - 获取用户位置并将坐标用作 didUpdateLocations 之外的字符串

python - Bokeh hexbin - 找到每个六边形的原始索引

c# - 无法找到合适的构造函数错误

c# - 如何在 Visual Studio 中为 mac 启用 VIM?

mysql - 如何将地理位置查询与其他条件结合起来

c# - 对具有动态操作数的空合并运算符进行类型检查