gpt4 book ai didi

c# - 如何使用 Bing map 检索邮政地址的纬度和经度?

转载 作者:行者123 更新时间:2023-12-03 09:30:26 24 4
gpt4 key购买 nike

我希望能够检索给定地址的地理坐标(纬度和经度)。如果我有完整的地址(街道地址 + 城市 + 州 + 邮政编码),我希望我能做到这一点。
如果重要的话,我正在使用 Bing map 。我得到的骨架代码是这样的(fullAddress、AddPushpin() 和 getGeoCordsForAddress() 是最相关的部分):

private void btnSave_Click(object sender, EventArgs e)
{
string fullAddress = string.Empty;
if (MissingValues()) return;

mapName = cmbxMapName.SelectedItem.ToString();
locationName = txtbxLocation.Text;
address = txtbxAddress.Text;
city = txtbxCity.Text;
st8 = txtbxSt8.Text;
zip = txtbxZip.Text;
mapDetailNotes = richtxtbxMapNotes.Text;
fullAddress = string.Format("{0} {1} {2} {3}", address, city, st8, zip);

if (InsertIntoCartographerDetail())
{
MessageBox.Show("insert succeeded");
AddPushpin(fullAddress);
ClearControls();
}
else
{
MessageBox.Show("insert failed");
}
}

private void AddPushpin(string fullAddress)
{
GeoCoordinate geoCoord = getGeoCordsForAddress(fullAddress);
Pushpin pin = new Pushpin();
pin.Location = new Location(geoCoord.Latitude, geoCoord.Longitude);
. . .
}

private GeoCoordinate getGeoCordsForAddress(string fullAddress)
{
GeoCoordinate gc = new GeoCoordinate();
. . . // What goes here?
return gc;
}

最佳答案

微软有官方BingMapsRESTToolkit它可以帮助您使用 Bing Maps REST Services容易地。为此,您需要安装 BingMapsRESTToolkit NuGet Package .
在这里,您对 GeocodeRequest 感兴趣通过地址获取位置。
示例 - 按地址获取纬度和经度并在 map 上创建图钉
安装 BingMapsRESTToolkit NuGet 包并运行以下代码:

private async void button1_Click(object sender, EventArgs e)
{
var request = new GeocodeRequest();
request.BingMapsKey = "YOUR MAP KEY";

request.Query = "172 Jalan Pinang, 50088 Kuala Lumpur, " +
"Federal Territory of Kuala Lumpur, Malaysia";
//OR
//request.Address = new SimpleAddress()
//{
// CountryRegion = "Malaysia",
// AddressLine = "172 Jalan Pinang",
// AdminDistrict = "Kuala Lumpur, Federal Territory of Kuala Lumpur",
// PostalCode = "50088",
//};

var result = await request.Execute();
if (result.StatusCode == 200)
{
var toolkitLocation = (result?.ResourceSets?.FirstOrDefault())
?.Resources?.FirstOrDefault()
as BingMapsRESTToolkit.Location;
var latitude = toolkitLocation.Point.Coordinates[0];
var longitude = toolkitLocation.Point.Coordinates[1];
var mapLocation = new Microsoft.Maps.MapControl.WPF.Location(latitude, longitude);
this.userControl11.myMap.SetView(mapLocation, 15);
var p = new Pushpin() { Location = mapLocation, ToolTip = "KLCC Park" };
this.userControl11.myMap.Children.Add(p);
}
}
enter image description here
如何在 Windows 窗体中使用 WPF Bing map :
  • How can I add a Bing Maps Component to my C# Winforms app?
  • Getting Bing Maps key

  • Bing Maps REST Toolkit 相关资源:
  • Getting Started
  • API Reference
  • Project Homepage
  • NuGet Package
  • 关于c# - 如何使用 Bing map 检索邮政地址的纬度和经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65752688/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com