Advertisement:

Author Topic: Use of Algolia Places  (Read 2729 times)

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Use of Algolia Places
« on: December 10, 2016, 12:10:19 am »
Hey guys,

i'm just playing around with Algolia Places and try to implement to my theme. It's pretty easy to implement, but i need to adjust it a little bit more, to get the selected country etc...

I think its a good addition instead the locations management od OSClass, because there you dont need to enter the locations... What do you think, it's worth to give it a try?

SteveJohnson

  • Sr. Member
  • ****
  • Posts: 328
  • Golden tip - Clear your cache :|
Re: Use of Algolia Places
« Reply #1 on: December 10, 2016, 01:11:46 am »
I was once considering to implement Algolia search, but then went with sphinx. Algolia places looks interesting, i'm gonna check :) Thanks for telling...

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #2 on: December 10, 2016, 02:10:06 am »
it seems to be really easy to implement at all... for now all works fine

when i select a city, it change values for country and region too




MkRahamath

  • Jr. Member
  • **
  • Posts: 73
Re: Use of Algolia Places
« Reply #3 on: December 10, 2016, 03:17:06 pm »
Code are simple ? can you pleas share here..

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #4 on: December 10, 2016, 07:08:47 pm »
After intergrating Algolia JavaScript and Stylesheet, i've done following steps...

At first, i've created a new file in my themes js folder

Quote from: ../oc-content/themes/THEMENAME/assets/js/places.js
Code: [Select]
function loadPlaces(locale, country) {
    var placesAutocomplete = places({
        container: document.querySelector('#sCity'),
        type: 'city',
        language: locale,
        countries: [country],
        templates: {
            value: function(suggestion) {
                return suggestion.name;
            }
        }
    });
    placesAutocomplete.on('change', function resultSelected(e) {
        var region  = e.suggestion.administrative,
            country = e.suggestion.countryCode.toUpperCase(),
            parent  = $('#sCountry').parent("div"),
            button  = parent.children("button");


        if (region) { $("#sRegion").val(region); }
        else { $("#sRegion").val(""); }
       
        if (country) {
            $("#sCountry").val(country);
            $('#sCountry option[value='+country+']').attr('selected','selected');                             //only needed for Material-Input
            $(button).children('span.filter-option.pull-left').html($("#sCountry option:selected").text());   //only needed for Material-Input
        }
    });
}

then in footer.php
Quote from: ../oc-content/themes/THEMENAME/footer.php
Code: [Select]
$countries = array();
foreach(osc_get_countries() as $v) { array_push($countries, "'".strtolower($v['pk_c_code']."'")); }
$countries = implode(", ", $countries);

//Doesnt work well, because you need the ISO 639-1 Country Code, not the first two letters of language code
$locale = explode("_", osc_current_user_locale());
$locale = $locale[0];
?>
...
...
<script>
    $(document).ready(function(){   
        $("#sCountry").on("change", function(event){                     
            var selCountry = $(this).val().toLowerCase();
            loadPlaces("<?php echo $locale?>", (selCountry.length >=1 ? selCountry : "<?php echo $countries?>"));     
        });       
        loadPlaces("<?php echo $locale?>", "<?php echo $countries?>");   
    });
    </script>
</body>
</html>

But... there are two problems left...

  • The function is started when page is loaded, but doesnt working unless you are selecting a country
  • Sometimes if you change the country, there are still citys from other countrys left
Maybe another developer has ideas for a better implementing of this function, to work all well


Edit:
If you are using the autocomplete from osclass, you must first disable it, otherwise the results will appear twice.
« Last Edit: December 10, 2016, 07:43:23 pm by Liath »

MkRahamath

  • Jr. Member
  • **
  • Posts: 73
Re: Use of Algolia Places
« Reply #5 on: December 10, 2016, 07:14:53 pm »
Thanks for the code.

I will try work with this.

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #6 on: December 10, 2016, 07:18:07 pm »
If you have any suggestion, to work all well... please share here too :) i'm still working with it, to make it perfect
« Last Edit: December 10, 2016, 07:33:59 pm by Liath »

MkRahamath

  • Jr. Member
  • **
  • Posts: 73
Re: Use of Algolia Places
« Reply #7 on: December 10, 2016, 07:39:18 pm »
Sure i do (Y)

webcity

  • Newbie
  • *
  • Posts: 14
Re: Use of Algolia Places
« Reply #8 on: January 20, 2017, 07:56:03 am »
Hi,

This looks a great option :-). I'm looking to add it to my theme too. Has anyone managed to get it working OK that wouldn't mind sharing the coding?

Many thanks

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #9 on: January 20, 2017, 06:36:32 pm »
I have solved all problems and made a plugin for this. I have to finish some other projects first, then i can upload it to the market.

MkRahamath

  • Jr. Member
  • **
  • Posts: 73
Re: Use of Algolia Places
« Reply #10 on: January 21, 2017, 01:23:19 am »
How much it cost or free?

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #11 on: January 21, 2017, 01:34:32 am »
all my plugins will be free

tito

  • issues
  • Sr. Member
  • *
  • Posts: 397
Re: Use of Algolia Places
« Reply #12 on: January 21, 2017, 02:07:50 am »
And are good plugins also :)

Liath

  • issues
  • Hero Member
  • *
  • Posts: 1346
  • </html> the end is always near
Re: Use of Algolia Places
« Reply #13 on: January 21, 2017, 02:57:03 am »
And are good plugins also :)


thanks for compliment  ;D

webcity

  • Newbie
  • *
  • Posts: 14
Re: Use of Algolia Places
« Reply #14 on: January 21, 2017, 09:07:18 am »
Fantastic! Thanks for all your efforts Liath, looking forward to the plugin release.

Many thanks,
Spencer