trouble with linking autocmplete to read from google api

Hi I am having trouble with linking autocmplete to read from google api, can you please help me out?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using Xamarin.Forms;

namespace Giftoria.MobileV2.Views.ShoppingCart
{

    public partial class CartDetailsPage : ContentPage
{
public CartDetailsPage ()
{
            InitializeComponent();
            autoComplete.DataSource = new GooglePlacesSuggestion(autoComplete.Text).PlacesCollection;
            autoComplete.DisplayMemberPath = "Name";

        }

        
        public class Places
        {
            private int id;
            public int ID
            {
                get { return id; }
                set { id = value; }
            }
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
        }

        public class GooglePlacesSuggestion
        {
            private ObservableCollection<Places> placesCollection;
            public ObservableCollection<Places> PlacesCollection
            {
                get { return placesCollection; }
                set { placesCollection = value; }
            }
            public GooglePlacesSuggestion(string address )
            {
                var places = GetPlacesAutocompleteAsync(address).Result;

                placesCollection = new ObservableCollection<Places>(places);
            }

            public async Task<List<Places>> GetPlacesAutocompleteAsync(string search)
        {
            // from: https://developers.google.com/places/documentation/autocomplete
            // e.g. https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=Kirk&key=AddYourOwnKeyHere
            string request = string.Format("https://maps.googleapis.com/maps/api/place/autocomplete/xml?input={0}&key={1}", search, "");
            var xml = await (new HttpClient()).GetStringAsync(request);
            var results = XDocument.Parse(xml).Element("AutocompletionResponse").Elements("prediction");

            var suggestions = new List<Places>();
            foreach (var result in results)
            {
                var suggestion = result.Element("description").Value;
                int.TryParse(result.Element("place_id").Value, out int placeId);
                suggestions.Add(new Places() { ID=placeId, Name=suggestion });
            }

            return suggestions;
        }
        }
    }
}

6 Replies

SK Selva Kumar  Veerakrishnan Syncfusion Team February 2, 2018 11:31 AM UTC

Hi Giftoria,

Thank you for contacting Syncfusion support.

We have prepared a sample for your requirement from the data provided from your update, please download the sample from the link given below.

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/AutoCompleteOnline_data_fetch_sample-1473715625

In this sample we have used the link provided on your update and fetched the data from that link.

Please let us know if you have any concern about this.

Regards,

Selva Kumar V.


GP Giftoria PTY LTD February 3, 2018 06:10 AM UTC

I have downloaded it but the project has some issue and it throws an error while I am trying to deploy it.



Attachment: error_fb177878.rar


GP Giftoria PTY LTD February 4, 2018 11:36 AM UTC

Ok I already fixed it on my computer using your code, and it is working but once I pass certain number of letter it stop showing on the autocomplete if it pass 16 characters it stop binding. 


RB Rabhia Beham Kathar Mideenar Syncfusion Team February 5, 2018 11:23 AM UTC

Hi Giftoria,

Sorry for the inconvenience.

We have modified the sample with the GoogleAPI link provided, and typed the “Kirkland” the dropdown opened as expected we have recorded a video for the same, please download the sample and video from the link given below.

Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/AutoComplete_PinCodeSearch_3-1570578052 

If the issue still gets reproduced at your end, please update us with the Sample modification or the replication video in which the issue occurs.This could help us to provide better solution on this.

Regards,
Rabhia Beham K.
 



AM Amin May 8, 2018 07:52 AM UTC

Hello SF Team,

I am trying to run this zip file posted on the last message. But it throws a run-time error. Can you please provide a new solution? I need to implement the same functionality.


RB Rabhia Beham Kathar Mideenar Syncfusion Team May 10, 2018 04:07 AM UTC

Hi Amin,

We have created another sample with google Api using SfAutoComplete Control. Please have the sample from the below link.

Sample Link: https://www.syncfusion.com/downloads/support/forum/135685/ze/AutoComplete_PinCodeSearch_(2)-420417010
 

Please check the sample and let us know if you have any concern.

Regards,
Rabhia Beham K.
 


Loader.
Up arrow icon