How can I retrieve Latitude and Longitude of a postal address using Bing Maps?

故事扮演 提交于 2021-02-18 22:22:09

问题


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

回答1:


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:

  • How can I add a Bing Maps Component to my C# Winforms app?
  • Getting Bing Maps key

Bing Maps REST Toolkit related resources:

  • Getting Started
  • API Reference
  • Project Homepage
  • NuGet Package


来源:https://stackoverflow.com/questions/65752688/how-can-i-retrieve-latitude-and-longitude-of-a-postal-address-using-bing-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!