【问题标题】:How can I retrieve Latitude and Longitude of a postal address using Bing Maps?如何使用 Bing 地图检索邮政地址的纬度和经度?
【发布时间】:2021-04-21 11:05:17
【问题描述】:

我希望能够检索给定地址的地理坐标(纬度和经度)。如果我有完整的地址(街道地址 + 城市 + 州 + 邮编),我希望我能做到这一点。

如果重要的话,我正在使用 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;
}

【问题讨论】:

标签: c# wpf winforms bing-maps geo


【解决方案1】:

Microsoft 有一个官方的BingMapsRESTToolkit,可以帮助您轻松使用Bing Maps REST Services。为此,您需要安装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);
   }
}

如何在 Windows 窗体中使用 WPF Bing 地图:

Bing Maps REST Toolkit 相关资源:

【讨论】:

    猜你喜欢
    • 2012-10-09
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多