是否可以使用 C# 在桌面 windows 表单应用程序中放置 Bing/Google 地图自动完成邮政地址文本框?

Is it possible to place Bing/Google Maps autocomplete postal address textbox in desktop windows form app using C#?

我是 C# 的菜鸟,这是我的第一门编程语言,目前我正在学习 C# in a Nutshell 这本书。我已经开始了一个项目来自动化我整天工作中所做的很多事情。

如果可能的话,谁能推荐一个好的地方来开始研究如何具体地做到这一点?我能找到的唯一资源是:

Google maps autocomplete textbox in c#

但他说最后他永远无法让它发挥作用。它给了我一个关于如何去做的想法,但我不知道他做错了什么。

我想实现一个下订单系统,用户需要做的第一件事是输入客户姓名和地址。

最终游戏是获取地址并将其与存储地址的数据库进行比较,如果 post/zip 代码匹配则加载帐户,如果不匹配则使用该地址创建一个新帐户详情。

能够使用 Microsoft 或 Google api 将使我不必托管包含每个英国地址的数据库。

编辑:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

根据 Google,下面是此功能的 JavaScript 代码,但在网页中。如您所见,评论指出需要 JS 库 Places。这是否意味着我不能只处理转换并且某些功能将丢失。这全部嵌入在 HTML 中,我没有包括:

// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?  key=YOUR_API_KEY&libraries=places">

var placeSearch, autocomplete;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

function initAutocomplete() {
  // Create the autocomplete object, restricting the search to geographical
  // location types.
  autocomplete = new google.maps.places.Autocomplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
      {types: ['geocode']});

  // When the user selects an address from the dropdown, populate the address
  // fields in the form.
  autocomplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

Bing 地图目前仅通过其网络控件提供自动 suggest/complete。您可以在您的应用程序的 Web 浏览器控件中托管它,但这会很混乱,并且可能无法很好地适应您的布局。

看看 Azure 基于位置的服务。他们有几个 REST 服务,可用于自动 suggest/complete for addresses/places、兴趣点等。只需将 typeahead URL 参数设置为 true 即可将服务置于预测模式。这里有一些有用的资源: