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

问题描述 投票:0回答:2

我想在Visual Studio 2017中使用名为Bing.RestClient v0.8 beta 1的NuGet,但我不知道如何在Windows窗体中使用它来获取位置(纬度/经度)。

我还不熟悉REST服务。

任何代码示例都可以帮助我构建项目并理解它是如何工作的?

我尝试过使用Web客户端,我可以获得可以解析的TEXT响应,但我想使用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类来反序列化结果,但我不知道如何使用它们来获取我的纬度和经度,通过地址查询。

c# json rest bing-maps
2个回答
0
投票

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

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

    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窗体中使用它们。


0
投票

感谢布莱恩的宝贵榜样。最后,我在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}"
                        ;
                }
            }
        } 
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.