swixpop / craft-locate

Harness the power of the Google Maps API Autocomplete feature inside Craft CMS
MIT License
15 stars 6 forks source link

'google' is not defined when creating entry from within an entries field modal #3

Open dbusck opened 6 years ago

dbusck commented 6 years ago

First time submitting an issue, please let me know if you need any more information!

Steps to reproduce:

My suggestion is to either.

  1. Use a callback in the google API script URL to initiate when google has finished loading the script
  2. add a setTimeout in the Plugin.prototype init function, which checks if google is defined yet:

Before:

        init: function (id) {
            var _this = this;
            $(function () {
                var fields = {
                    lat: document.getElementById(_this.options.prefix + _this.options.name + '-lat'),
                    lng: document.getElementById(_this.options.prefix + _this.options.name + '-lng'),
                    placeid: document.getElementById(_this.options.prefix + _this.options.name + '-placeid'),
                }
                var input = document.getElementById(_this.options.prefix + _this.options.name + '-location');
                var options = JSON.parse('{' + _this.options.optionsObject + '}');
                var autocomplete = new google.maps.places.Autocomplete(input, options);
                autocomplete.addListener('place_changed', function () {
                    var place = autocomplete.getPlace();
                    console.log(place);
                    fields.lat.value = place.geometry.location.lat();
                    fields.lng.value = place.geometry.location.lng()
                    fields.placeid.value = place.place_id;
                });

                input.addEventListener('change', function(e) {
                   if (!input.value) {
                       fields.lat.value = '';
                       fields.lng.value = '';
                       fields.placeid.value = '';
                   }
                });
            });
        }

After:

        init: function (id) {
            var _this = this;
            var google = window.google;

            if (!google) {
              setTimeout(function() {
                _this.init();
              }, 250);
            } else {
              $(function () {
                  var fields = {
                      lat: document.getElementById(_this.options.prefix + _this.options.name + '-lat'),
                      lng: document.getElementById(_this.options.prefix + _this.options.name + '-lng'),
                      placeid: document.getElementById(_this.options.prefix + _this.options.name + '-placeid'),
                  }
                  var input = document.getElementById(_this.options.prefix + _this.options.name + '-location');
                  var options = JSON.parse('{' + _this.options.optionsObject + '}');
                  var autocomplete = new google.maps.places.Autocomplete(input, options);
                  autocomplete.addListener('place_changed', function () {
                      var place = autocomplete.getPlace();
                      console.log(place);
                      fields.lat.value = place.geometry.location.lat();
                      fields.lng.value = place.geometry.location.lng()
                      fields.placeid.value = place.place_id;
                  });

                  input.addEventListener('change', function(e) {
                     if (!input.value) {
                         fields.lat.value = '';
                         fields.lng.value = '';
                         fields.placeid.value = '';
                     }
                  });
              });
            }
        }