form_field_location.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 language="javascript" type="text/javascript" src="https://maps.google.com/maps/api/js?libraries=places&language=__lang__&key=__key__"></script>
  16. <script>
  17. var oBxLocationHashMap = {
  18. '__name___lat': '',
  19. '__name___lng': '',
  20. '__name___city': 'locality',
  21. '__name___state': 'administrative_area_level_1',
  22. '__name___country': 'country',
  23. '__name___zip': 'postal_code',
  24. '__name___street': 'route',
  25. '__name___street_number': 'street_number'
  26. };
  27. var bx_location_reset = function () {
  28. for (sKey in oBxLocationHashMap)
  29. $('[name="' + sKey + '"]').val('');
  30. };
  31. var bx_location_process_address = function (o) {
  32. var i, sKey, sCountry, sCity;
  33. for (i in o) {
  34. for (sKey in oBxLocationHashMap) {
  35. var sName = oBxLocationHashMap[sKey];
  36. if ('locality' == sName && -1 == o[i].types.indexOf(sName))
  37. sName = 'postal_town';
  38. if ('administrative_area_level_1' == sName && -1 == o[i].types.indexOf(sName))
  39. sName = 'administrative_area_level_2';
  40. if (o[i].types.indexOf(sName) > -1) {
  41. var sIndex = 'route' == sName || 'locality' == sName ? 'long_name' : 'short_name';
  42. $('[name="' + sKey + '"]').val(o[i][sIndex]);
  43. }
  44. if (o[i].types.indexOf('locality') > -1)
  45. sCity = o[i].short_name;
  46. else if (o[i].types.indexOf('country') > -1)
  47. sCountry = o[i].long_name;
  48. }
  49. }
  50. if ('undefined' != typeof(sCity) && sCity.length)
  51. $('#__id_status__').html(sCity + ", " + sCountry);
  52. else
  53. $('#__id_status__').html(sCountry);
  54. };
  55. var bx_location_geolocate = function () {
  56. if (navigator.geolocation) {
  57. navigator.geolocation.getCurrentPosition(function(position) {
  58. var geolocation = {
  59. lat: position.coords.latitude,
  60. lng: position.coords.longitude
  61. };
  62. var circle = new google.maps.Circle({
  63. center: geolocation,
  64. radius: position.coords.accuracy
  65. });
  66. glBxAutocomplete__name__.setBounds(circle.getBounds());
  67. });
  68. }
  69. }
  70. $('input[name=__name__][type=checkbox]').on('change', function () {
  71. var callbackOnLocationReset = function (error) {
  72. var sKey;
  73. for (sKey in oBxLocationHashMap)
  74. $('[name="' + sKey + '"]').val('');
  75. $('#__id_status__').html("<bx_text_js:_sys_location_undefined />");
  76. }
  77. var callbackOnLocationSucess = function (position) {
  78. bx_location_reset ();
  79. $('[name="__name___lat"]').val(position.coords.latitude);
  80. $('[name="__name___lng"]').val(position.coords.longitude);
  81. var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  82. var geocoder = new google.maps.Geocoder();
  83. geocoder.geocode({'latLng': latlng}, function (results, status) {
  84. if (status == google.maps.GeocoderStatus.OK) {
  85. bx_location_process_address(results[0]['address_components']);
  86. }
  87. else {
  88. $('#__id_status__').html(status);
  89. }
  90. });
  91. }
  92. if ($(this).is(':checked')) {
  93. $('#__id_status__').html('<bx_text_js:_sys_location_determining />');
  94. if (navigator.geolocation)
  95. navigator.geolocation.getCurrentPosition(callbackOnLocationSucess, callbackOnLocationReset);
  96. else
  97. $('#__id_status__').html('<bx_text_js:_sys_location_geolocation_not_supported_by_browser />');
  98. } else {
  99. callbackOnLocationReset();
  100. }
  101. });
  102. var fInitCallback = function () {
  103. var callbackFillInAddress = function () {
  104. // Get the place details from the autocomplete object.
  105. var place = glBxAutocomplete__name__.getPlace();
  106. bx_location_reset ();
  107. $('[name="__name___lat"]').val(place.geometry.location.lat);
  108. $('[name="__name___lng"]').val(place.geometry.location.lng);
  109. bx_location_process_address(place.address_components);
  110. }
  111. $('input[name=__name__][type=text]').on('focus', bx_location_geolocate);
  112. $('input[name=__name__][type=text]').on('blur', function () {
  113. if (!$(this).val().length)
  114. bx_location_reset ();
  115. });
  116. if ($('input[name=__name__][type=text]').size()) {
  117. // Create the autocomplete object, restricting the search to geographical location types.
  118. glBxAutocomplete__name__ = new google.maps.places.Autocomplete(
  119. /** @type {!HTMLInputElement} */(document.getElementById('__id_input__'))
  120. );
  121. // When the user selects an address from the dropdown, populate the address fields in the form.
  122. glBxAutocomplete__name__.addListener('place_changed', callbackFillInAddress);
  123. }
  124. }
  125. $(document).ready(function () {
  126. if ('undefined' === typeof(google))
  127. bx_get_scripts(['https://maps.google.com/maps/api/js?libraries=places&language=__lang__&key=__key__'], fInitCallback);
  128. else
  129. fInitCallback();
  130. });
  131. </script>