【发布时间】:2018-10-08 14:21:30
【问题描述】:
我是 C# 的菜鸟,这是我的第一门编程语言,我目前正在阅读 C# in a Nutshell 一书。我已经启动了一个项目来自动化我一整天的工作。
如果可能的话,任何人都可以推荐一个好地方来开始专门研究如何做到这一点?我能找到的唯一资源是:
Google maps autocomplete textbox in c#
但他最后说他永远无法让它发挥作用。它给了我一个关于如何去做的想法,但我不知道他做错了什么。
我想实现一个下订单系统,用户需要做的第一件事是输入客户姓名和地址。
最终游戏是获取地址并将其与存储地址的数据库进行比较,如果邮政/邮政编码匹配,则加载帐户,如果不匹配,则使用地址详细信息创建一个新帐户。
能够使用 Microsoft 或 Google api 将使我不必托管一个包含每个英国地址的数据库。
编辑:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
根据 Google,以下是此功能的 JavaScript 代码,但在网页中。如您所见,cmets 声明 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());
});
}
}
【问题讨论】:
-
自动完成通常通过让替代任务使用您刚刚输入的内容进行快速数据库查询并返回前几个命中来工作。某些情况下可以跳过数据库(浏览器可能会将所有最近访问过的地址都保存在 RAM 中)。您不需要“每个英国地址”。只有“数据库中每个客户”的地址。
-
你说得对,如果现有客户的地址没有“命中”,我将在我正在寻找的功能中遗漏的东西是正确格式的完整地址在数据库中。
-
我可以告诉你我通常做什么:首先我制作一个子表单/日志,用户可以在其中选择数据库中的所有客户(有和没有过滤器)。它甚至可能具有“添加”功能。这是我选择客户的主要方式。它可能甚至是完整的 CRUD 表单。我可能在您正在使用此对话框选择其值的文本框上添加自动完成功能。如果客户(或他的地址)不在数据库中,这通常意味着必须将其添加到数据库中。所以表单(包括相关的“地址”CRUD)是执行此操作的主要方式(继续)
-
(继续)也许您可以将“向谷歌地图询问有效名称”功能添加到地址 CRUD。或者不是,这没什么大不了的。我还没有看到它在任何地方使用过,所以它可能是那些容易引起问题的“功能”或“助手”之一。很少有事情像试图变得聪明的程序那样愚蠢。
-
感谢您的建议。要考虑的有效事项。尽管我仍然想知道是否可以使用 api 成功实现,但无需过多了解整个程序实现的细节。我知道 google api 在一些使用 javascript 的网络应用程序中支持这种功能。如果它在 Windows 窗体中不起作用,或者由于某种原因人们很难在 Windows 窗体设置中使用 c# 来实现它,我真的很想知道为什么会这样。不过,感谢您的意见并花时间回复菜鸟。
标签: c# winforms google-maps google-maps-api-3 bing-maps