B. *_*non 7 c# wpf geo bing-maps winforms
我希望能够检索给定地址的地理坐标(纬度和经度)。如果我有完整的地址(街道地址 + 城市 + 州 + 邮政编码),我希望我能做到这一点。
如果重要的话,我正在使用 Bing 地图。我得到的骨架代码是这样的(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;
}
Run Code Online (Sandbox Code Playgroud)
Microsoft 拥有官方BingMapsRESTToolkit,可帮助您轻松使用Bing Maps REST 服务。为此,您需要安装BingMapsRESTToolkit NuGet Package。
在这里,您对通过地址获取位置的GeocodeRequest感兴趣。
示例 - 按地址获取纬度和经度并在地图上创建图钉
安装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);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 Windows 窗体中使用 WPF Bing 地图:
Bing 地图 REST 工具包相关资源: