location_field_google.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <div class="bx-form-input-location-cont bx-clearfix">
  2. __input__
  3. <bx_if:auto_input>
  4. <div id="__id_status__" class="bx-def-margin-sec-left">__location_string__</div>
  5. </bx_if:auto_input>
  6. </div>
  7. <input type="hidden" name="__name___lat" value="__lat__" />
  8. <input type="hidden" name="__name___lng" value="__lng__" />
  9. <input type="hidden" name="__name___country" value="__country__" />
  10. <input type="hidden" name="__name___state" value="__state__" />
  11. <input type="hidden" name="__name___city" value="__city__" />
  12. <input type="hidden" name="__name___zip" value="__zip__" />
  13. <input type="hidden" name="__name___street" value="__street__" />
  14. <input type="hidden" name="__name___street_number" value="__street_number__" />
  15. <script>
  16. var oBxLocationHashMap = {
  17. lat: {html: '__name___lat', api: '', done: false},
  18. lng: {html: '__name___lng', api: '', done: false},
  19. city: {html: '__name___city', api: ['locality', 'sublocality', 'sublocality_level_1', 'postal_town'], done: false},
  20. state: {html: '__name___state', api: ['administrative_area_level_1', 'administrative_area_level_2'], done: false},
  21. country: {html: '__name___country', api: ['country'], done: false},
  22. zip: {html: '__name___zip', api: ['postal_code'], done: false},
  23. street: {html: '__name___street', api: ['route'], done: false},
  24. street_number: {html: '__name___street_number', api: ['street_number'], done: false}
  25. };
  26. var bx_location_reset = function() {
  27. for(sKey in oBxLocationHashMap)
  28. $('[name="' + oBxLocationHashMap[sKey].html + '"]').val('');
  29. };
  30. var bx_location_process_address = function(oApi) {
  31. var sCountry, sCity;
  32. var sNameShort = '__api_field_name_short__';
  33. var sNameLong = '__api_field_name_long__';
  34. var aName2Lenth = __api_field_name_2_length__;
  35. aName2Lenth = jQuery.extend({}, aName2Lenth != undefined ? aName2Lenth : {});
  36. for(var sKey in oBxLocationHashMap) {
  37. oBxLocationHashMap[sKey].done = false;
  38. var oMapItem = oBxLocationHashMap[sKey];
  39. if(!oMapItem.api)
  40. continue;
  41. for(var i in oMapItem.api) {
  42. if(oMapItem.done)
  43. break;
  44. for(var j in oApi) {
  45. if(oApi[j].types.indexOf(oMapItem.api[i]) === -1)
  46. continue;
  47. var sIndex = sNameShort;
  48. switch(sKey) {
  49. case 'country':
  50. sCountry = oApi[j][sNameLong];
  51. break;
  52. case 'city':
  53. sIndex = sNameLong;
  54. sCity = oApi[j][sNameShort];
  55. break;
  56. case 'street':
  57. sIndex = sNameLong;
  58. break;
  59. }
  60. if(aName2Lenth[sKey] != undefined)
  61. sIndex = aName2Lenth[sKey];
  62. $('[name="' + oMapItem.html + '"]').val(oApi[j][sIndex]);
  63. oMapItem.done = true;
  64. }
  65. }
  66. }
  67. if(typeof(sCity) != 'undefined' && sCity.length)
  68. $('#__id_status__').html(sCity + ", " + sCountry);
  69. else
  70. $('#__id_status__').html(sCountry);
  71. };
  72. var bx_location_geolocate = function () {
  73. if (navigator.geolocation) {
  74. navigator.geolocation.getCurrentPosition(function(position) {
  75. var geolocation = {
  76. lat: position.coords.latitude,
  77. lng: position.coords.longitude
  78. };
  79. var circle = new google.maps.Circle({
  80. center: geolocation,
  81. radius: position.coords.accuracy
  82. });
  83. glBxAutocomplete__name__.setBounds(circle.getBounds());
  84. });
  85. }
  86. }
  87. $('input[name=__name__][type=checkbox]').on('change', function () {
  88. var _self = this;
  89. var callbackOnLocationReset = function (error) {
  90. for(var sKey in oBxLocationHashMap)
  91. $('[name="' + oBxLocationHashMap[sKey].html + '"]').val('');
  92. $('#__id_status__').html("<bx_text_js:_sys_location_field_label />");
  93. $(_self).prop("checked", false);
  94. $(_self).parents('.bx-switcher-cont').removeClass('on').addClass('off');
  95. }
  96. var callbackOnLocationSucess = function (position) {
  97. bx_location_reset ();
  98. $('[name="__name___lat"]').val(position.coords.latitude);
  99. $('[name="__name___lng"]').val(position.coords.longitude);
  100. var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  101. var geocoder = new google.maps.Geocoder();
  102. geocoder.geocode({'latLng': latlng}, function (results, status) {
  103. if (status == google.maps.GeocoderStatus.OK) {
  104. bx_location_process_address(results[0]['address_components']);
  105. }
  106. else {
  107. $('#__id_status__').html(status);
  108. }
  109. });
  110. }
  111. if ($(this).is(':checked')) {
  112. $('#__id_status__').html('<bx_text_js:_sys_location_determining />');
  113. if (navigator.geolocation)
  114. navigator.geolocation.getCurrentPosition(callbackOnLocationSucess, callbackOnLocationReset);
  115. else
  116. $('#__id_status__').html('<bx_text_js:_sys_location_geolocation_not_supported_by_browser />');
  117. } else {
  118. callbackOnLocationReset();
  119. }
  120. });
  121. var fInitCallback = function () {
  122. var callbackFillInAddress = function () {
  123. // Get the place details from the autocomplete object.
  124. var place = glBxAutocomplete__name__.getPlace();
  125. bx_location_reset ();
  126. $('[name="__name___lat"]').val(place.geometry.location.lat);
  127. $('[name="__name___lng"]').val(place.geometry.location.lng);
  128. bx_location_process_address(place.address_components);
  129. };
  130. $('input[name=__name__][type=text]').on('focus', function () {
  131. var sVal = $(this).val().trim();
  132. if(sVal.length)
  133. $(this).attr('bx-data', sVal);
  134. bx_location_geolocate();
  135. });
  136. $('input[name=__name__][type=text]').on('blur', function () {
  137. if(!$(this).val().length)
  138. bx_location_reset();
  139. });
  140. $('input[name=__name__][type=text]').on('input', function () {
  141. var sVal = $(this).val().trim();
  142. if($(this).attr('bx-data') != sVal)
  143. bx_location_reset();
  144. });
  145. if ($('input[name=__name__][type=text]').size()) {
  146. // Create the autocomplete object, restricting the search to geographical location types.
  147. glBxAutocomplete__name__ = new google.maps.places.Autocomplete(
  148. /** @type {!HTMLInputElement} */(document.getElementById('__id_input__'))
  149. );
  150. // When the user selects an address from the dropdown, populate the address fields in the form.
  151. glBxAutocomplete__name__.addListener('place_changed', callbackFillInAddress);
  152. }
  153. }
  154. $(document).ready(function () {
  155. if ('undefined' === typeof(google))
  156. bx_get_scripts(['https://maps.google.com/maps/api/js?libraries=places&language=__lang__&key=__key__'], fInitCallback);
  157. else
  158. fInitCallback();
  159. });
  160. </script>