如何使用Bing Maps检索邮政地址的纬度和经度? [英] How can I retrieve Latitude and Longitude of a postal address using Bing Maps?

查看:61
本文介绍了如何使用Bing Maps检索邮政地址的纬度和经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果有关系,我正在使用Bing地图.我得到的框架代码是这样的(fullAddress,AddPushpin()和getGeoCordsForAddress()是最相关的部分):

  private void btnSave_Click(对象发送者,EventArgs e){字符串fullAddress = string.Empty;如果(MissingValues())返回;mapName = cmbxMapName.SelectedItem.ToString();locationName = txtbxLocation.Text;地址= txtbxAddress.Text;city = txtbxCity.Text;st8 = txtbxSt8.Text;zip = txtbxZip.Text;mapDetailNotes = richtxtbxMapNotes.Text;fullAddress = string.Format("{0} {1} {2} {3}",地址,城市,st8,邮编);如果(InsertIntoCartographerDetail()){MessageBox.Show(插入成功");AddPushpin(fullAddress);ClearControls();}别的{MessageBox.Show(插入失败");}}私有无效AddPushpin(string fullAddress){GeoCoordinate geoCoord = getGeoCordsForAddress(fullAddress);图钉图钉=新图钉图钉();pin.Location =新位置(geoCoord.Latitude,geoCoord.Longitude);...}私有GeoCoordinate getGeoCordsForAddress(string fullAddress){GeoCoordinate gc = new GeoCoordinate();...//这里是什么?返回gc;} 

解决方案

Microsoft具有官方

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

I want to be able to retrieve the geographical coordinates (latitude and longitude) for a given address. I'm hoping I can do that if I have the full address (street address + city + state + zip).

If it matters, I am using Bing Maps. The skeleton code I've got is this (fullAddress, AddPushpin(), and getGeoCordsForAddress() are the most pertinent parts):

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;
}

解决方案

Microsoft has an official BingMapsRESTToolkit which helps you to work with Bing Maps REST Services easily. To do so, you need to install BingMapsRESTToolkit NuGet Package.

Here, you are interested in GeocodeRequest to get location by address.

Example - Get latitude and longitude by address and create a pushpin on map

Install BingMapsRESTToolkit NuGet Package and run the following code:

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);
   }
}

How to use WPF Bing Maps in Windows Forms:

Bing Maps REST Toolkit related resources:

这篇关于如何使用Bing Maps检索邮政地址的纬度和经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆