如何使用 NuGet Bing 调用 Maps REST 服务 Bing.RestClient

How to call Bing Maps REST service using NuGet Bing.RestClient

我想在 Visual Studio 2017 年使用名为 Bing.RestClient v0.8 beta 1 的 NuGet,但我不知道如何在 Windows 表单中使用它来获取位置 ( Latitude/Longitude).

我还不熟悉 REST 服务。

任何可以帮助我构建项目并理解其工作原理的代码示例?

我已经尝试使用 Web 客户端,我可以获得可以解析的文本响应,但我想使用 NuGet Bing.RestClient.

中可用的 类
public partial class Form1 : Form
{
    // PERSONAL BING KEY
    String BingKey = "*******************************";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        GetFind();
    }

    private async void  GetFind()
    {

        // Take advantage of built-in Point of Interest groups
        var list = PoiEntityGroups.Government();
        list.Add(PoiEntityTypes.Bank);
        // Build your filter list from the group.
        var filter = PoiEntityGroups.BuildFilter(list);
        var client = new Bing.SpatialDataClient(BingKey);

        //---------------------------------------------------------
        // This does NOT use the Nuget but just a WebClient and I get the response in TEXT format. But this is not what I want.
        String AddressQuery = "Via Ravenna 10, Milano";
        String BaseQueryURL;
        BaseQueryURL = String.Format("http://dev.virtualearth.net/REST/v1/Locations?query={0}?maxResults=1&key={1}", AddressQuery, BingKey);

        // Create web client simulating IE6.
        using (System.Net.WebClient wclient = new WebClient())
        {
            wclient.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)";

            byte[] arr = wclient.DownloadData(BaseQueryURL);
            txtResult.Text = "Bytes: " + arr.Length + Environment.NewLine;
            txtResult.Text = txtResult.Text + wclient.DownloadString(BaseQueryURL);
        }
        //---------------------------------------------------------
    }
}

我希望通过使用 NuGet 类 获得反序列化的结果,但我不知道如何使用它们来获取我的纬度和经度,按地址查询。

我建议您查看 GitHub 页面上为 BingMapsRESTToolkit NuGet 包提供的示例。 (https://github.com/Microsoft/BingMapsRESTToolkit/blob/master/Docs/Getting%20Started.md#HowToMakeARequest)

这是我在 Visual Studio 中作为控制台应用程序工作的 GitHub 的基本示例:

    static async Task Main()
    {
        var bingKey = "**********************";

        var request = new GeocodeRequest()
        {
            Query = "Via Ravenna 10, Milano",
            IncludeIso2 = true,
            IncludeNeighborhood = true,
            MaxResults = 25,
            BingMapsKey = bingKey
        };

        //Process the request by using the ServiceManager.
        var response = await request.Execute();

        if (response != null &&
            response.ResourceSets != null &&
            response.ResourceSets.Length > 0 &&
            response.ResourceSets[0].Resources != null &&
            response.ResourceSets[0].Resources.Length > 0)
        {
            var result = response.ResourceSets[0].Resources[0] as Location;

            var coords = result.Point.Coordinates;
            if (coords != null && coords.Length == 2)
            {
                var lat = coords[0];
                var lng = coords[1];
                Console.WriteLine($"Geocode Results - Lat: {lat} / Long: {lng}");
            }
        }
    }

由此,您现在有一对 lat/long 作为 Double 类型。在您认为合适的 Windows 表格中使用它们。

感谢 Bryan 提供的宝贵示例。 最后我以这种方式让它工作得很好,在 Windows 形式中:

public partial class Form1 : Form
    {
        // PERSONAL BING KEY
        String BingKey = "*******************************";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetFind();
        }

        private async void GetFind()
        {    
            var request = new GeocodeRequest()
            {
                Query = "Via trento 1, Sondrio",
                IncludeIso2 = true,
                IncludeNeighborhood = true,
                MaxResults = 25,
                BingMapsKey = BingKey
            };


            //Process the request by using the ServiceManager.
            var response = await request.Execute();

            if (response != null &&
                response.ResourceSets != null &&
                response.ResourceSets.Length > 0 &&
                response.ResourceSets[0].Resources != null &&
                response.ResourceSets[0].Resources.Length > 0)
            {
                var result = response.ResourceSets[0].Resources[0] as Location;

                var myAddr = result.Address.AddressLine;
                var CAP = result.Address.PostalCode;
                var City = result.Address.Locality;
                var Province = result.Address.AdminDistrict2;
                var Region = result.Address.AdminDistrict;
                var Stato = result.Address.CountryRegion;

                var coords = result.Point.Coordinates;
                if (coords != null && coords.Length == 2)
                {
                    var lat = coords[0];
                    var lng = coords[1];

                    string Latitude = String.Format("{0:00.000000}", lat);
                    string Longitude = String.Format("{0:000.000000}", lng);

                    txtResult.Text = "Indirizzo: " + myAddr +
                        Environment.NewLine + "CAP: " + CAP +
                        Environment.NewLine + "Città: " + City +
                        Environment.NewLine + "Provincia: " + Province +
                        Environment.NewLine + "Regione: " + Region +
                        Environment.NewLine + "Stato: " + Stato + 
                        Environment.NewLine + $"Coordinate - Lat: {Latitude} / Long: {Longitude}"
                        ;
                }
            }
        } 
    }
}