0

I'm a noob at C#, this is my first programming language and I am currently working my way through the book C# in a Nutshell. I have started a project to automate a lot of what I do at work all day.

If it is possible can anyone recommend a good place to start researching how to do this specifically? The only resource that I can find is:

Google maps autocomplete textbox in c#

But he says in the end he could never get it to work. It gives me an idea about how to go about it but I don't know what he was doing wrong.

I want to implement an order placing system and the first thing the user will need to do is enter customer name and address.

The end game is getting the address and comparing it to a database of stored addresses, if the post/zip code matches then load up the account and if it doesn't then create a new account using the address details.

Being able to use a Microsoft or Google api would prevent me from having to host a database with every UK address in it.

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

As per Google, below is the JavaScript code for this functionality but in a web page. As you can see the comments state that the JS library Places is required. Does this mean I can't just work on the conversion and that some functionality will be missing. This is all embedded within HTML which I have not included:

// 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());
    });
  }
}
Logan9Fingers
  • 37
  • 1
  • 3
  • 9
  • Autocompletion usually works by having a Alternative Task make a quick database query using what you just typed in and return the top few hits. Some cases can skip the database (a Browser might ahve all recently visited adresses in RAM). You would not need "every UK adress". Only the Adress of "every Customer in the Database". – Christopher Apr 27 '18 at 17:38
  • You are right, something that I left out of the functionality I would be looking for would be to have a completed address in the correct format if there was no 'hit' against the addresses for the existing customers in the database. – Logan9Fingers Apr 27 '18 at 17:46
  • I can tell you what I usualyl do: First I make a Subform/Dailog where the user can Select all Customers in teh DB (with and without filters). It might even have "add" functionality. That is my primary way to to select the Customer. It *might* even be a full on CRUD Form. I *might* add Autocompletion on the Textbox whose value you are selecting with this Dialog too. If a Customer (or his Adress) is not in the DB, that usually means it has to be added to the DB. So the Form (including the related "Adress" CRUD one) are the primary way of doing this (continues) – Christopher Apr 27 '18 at 19:41
  • (continues)Maybe you can add that "ask google maps for valid names" feature to the Adress CRUD. Or not, it is not that big of a deal. I have not seen it used anywhere, so it might be one of those "features" or "helpers" that is prone to cause issues. Few things are as dumb as a Programm that tries to be smart. – Christopher Apr 27 '18 at 19:43
  • Thanks for the advice. Valid things to consider. Without getting too much into the specifics of the overall program implementation though I still would like to know if it can be successfully achieved using the api's. I know that the google api supports this kind of functionality in some web apps using javascript. If it doesn't work in windows forms or there is some reason that people struggle to implement it using c# in a windows form setting I'd be really interested to know the mechanisms behind why this is. Thanks for your input though and for taking the time to respond to a noob. – Logan9Fingers Apr 27 '18 at 20:20
  • I do not see why it would be impossible to do something in WindowsForms that JavaScript can do. The other way around is way more likely. There could maybe exist an issue with something like a cookie being required (trivial for the webpage, tricky for a WindowsForm Application). But beyond that using a WebAPI is: Finding out wich specific kind (Restfull, XML based) it is. And learning how to call taht kind from C#. – Christopher Apr 27 '18 at 20:46

1 Answers1

0

Bing Maps only provides auto suggest/complete through their web control currently. You can host this inside of a web browser control in your app but that will be messy and likely won't work well with your layout.

Take a look at Azure Location Based Serv- ices. They have several REST services which can be used for auto suggest/complete for addresses/places, points of interest and more. Simply set the typeahead URL parameter to true to put the service into predictive mode. Here are some useful resources:

rbrundritt
  • 16,570
  • 2
  • 21
  • 46