我想在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类来反序列化结果,但是我不知道如何使用它们来获取我的经度和纬度,并按地址查询。
答案 0 :(得分:0)
我建议您查看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}");
}
}
}
由此,您现在有了一对Double类型的经/纬度。视需要在Windows窗体中使用它们。
答案 1 :(得分: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}"
;
}
}
}
}
}