| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <div class="bx-form-input-location-cont bx-clearfix">
- __input__
- <bx_if:auto_input>
- <div id="__id_status__" class="bx-def-margin-sec-left">__location_string__</div>
- </bx_if:auto_input>
- </div>
- <input type="hidden" name="__name___lat" value="__lat__" />
- <input type="hidden" name="__name___lng" value="__lng__" />
- <input type="hidden" name="__name___country" value="__country__" />
- <input type="hidden" name="__name___state" value="__state__" />
- <input type="hidden" name="__name___city" value="__city__" />
- <input type="hidden" name="__name___zip" value="__zip__" />
- <input type="hidden" name="__name___street" value="__street__" />
- <input type="hidden" name="__name___street_number" value="__street_number__" />
- <script>
- var oBxLocationHashMap = {
- lat: {html: '__name___lat', api: '', done: false},
- lng: {html: '__name___lng', api: '', done: false},
- city: {html: '__name___city', api: ['locality', 'sublocality', 'sublocality_level_1', 'postal_town'], done: false},
- state: {html: '__name___state', api: ['administrative_area_level_1', 'administrative_area_level_2'], done: false},
- country: {html: '__name___country', api: ['country'], done: false},
- zip: {html: '__name___zip', api: ['postal_code'], done: false},
- street: {html: '__name___street', api: ['route'], done: false},
- street_number: {html: '__name___street_number', api: ['street_number'], done: false}
- };
- var bx_location_reset = function() {
- for(sKey in oBxLocationHashMap)
- $('[name="' + oBxLocationHashMap[sKey].html + '"]').val('');
- };
- var bx_location_process_address = function(oApi) {
- var sCountry, sCity;
- var sNameShort = '__api_field_name_short__';
- var sNameLong = '__api_field_name_long__';
- var aName2Lenth = __api_field_name_2_length__;
- aName2Lenth = jQuery.extend({}, aName2Lenth != undefined ? aName2Lenth : {});
- for(var sKey in oBxLocationHashMap) {
- oBxLocationHashMap[sKey].done = false;
- var oMapItem = oBxLocationHashMap[sKey];
- if(!oMapItem.api)
- continue;
- for(var i in oMapItem.api) {
- if(oMapItem.done)
- break;
- for(var j in oApi) {
- if(oApi[j].types.indexOf(oMapItem.api[i]) === -1)
- continue;
- var sIndex = sNameShort;
- switch(sKey) {
- case 'country':
- sCountry = oApi[j][sNameLong];
- break;
- case 'city':
- sIndex = sNameLong;
- sCity = oApi[j][sNameShort];
- break;
- case 'street':
- sIndex = sNameLong;
- break;
- }
- if(aName2Lenth[sKey] != undefined)
- sIndex = aName2Lenth[sKey];
- $('[name="' + oMapItem.html + '"]').val(oApi[j][sIndex]);
- oMapItem.done = true;
- }
- }
- }
- if(typeof(sCity) != 'undefined' && sCity.length)
- $('#__id_status__').html(sCity + ", " + sCountry);
- else
- $('#__id_status__').html(sCountry);
- };
- var bx_location_geolocate = function () {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var geolocation = {
- lat: position.coords.latitude,
- lng: position.coords.longitude
- };
- var circle = new google.maps.Circle({
- center: geolocation,
- radius: position.coords.accuracy
- });
- glBxAutocomplete__name__.setBounds(circle.getBounds());
- });
- }
- }
- $('input[name=__name__][type=checkbox]').on('change', function () {
- var _self = this;
- var callbackOnLocationReset = function (error) {
- for(var sKey in oBxLocationHashMap)
- $('[name="' + oBxLocationHashMap[sKey].html + '"]').val('');
- $('#__id_status__').html("<bx_text_js:_sys_location_field_label />");
- $(_self).prop("checked", false);
- $(_self).parents('.bx-switcher-cont').removeClass('on').addClass('off');
- }
- var callbackOnLocationSucess = function (position) {
- bx_location_reset ();
- $('[name="__name___lat"]').val(position.coords.latitude);
- $('[name="__name___lng"]').val(position.coords.longitude);
- var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
- var geocoder = new google.maps.Geocoder();
- geocoder.geocode({'latLng': latlng}, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- bx_location_process_address(results[0]['address_components']);
- }
- else {
- $('#__id_status__').html(status);
- }
- });
- }
- if ($(this).is(':checked')) {
- $('#__id_status__').html('<bx_text_js:_sys_location_determining />');
- if (navigator.geolocation)
- navigator.geolocation.getCurrentPosition(callbackOnLocationSucess, callbackOnLocationReset);
- else
- $('#__id_status__').html('<bx_text_js:_sys_location_geolocation_not_supported_by_browser />');
- } else {
- callbackOnLocationReset();
- }
- });
- var fInitCallback = function () {
- var callbackFillInAddress = function () {
- // Get the place details from the autocomplete object.
- var place = glBxAutocomplete__name__.getPlace();
- bx_location_reset ();
- $('[name="__name___lat"]').val(place.geometry.location.lat);
- $('[name="__name___lng"]').val(place.geometry.location.lng);
-
- bx_location_process_address(place.address_components);
- };
- $('input[name=__name__][type=text]').on('focus', function () {
- var sVal = $(this).val().trim();
- if(sVal.length)
- $(this).attr('bx-data', sVal);
- bx_location_geolocate();
- });
- $('input[name=__name__][type=text]').on('blur', function () {
- if(!$(this).val().length)
- bx_location_reset();
- });
- $('input[name=__name__][type=text]').on('input', function () {
- var sVal = $(this).val().trim();
- if($(this).attr('bx-data') != sVal)
- bx_location_reset();
- });
- if ($('input[name=__name__][type=text]').size()) {
- // Create the autocomplete object, restricting the search to geographical location types.
- glBxAutocomplete__name__ = new google.maps.places.Autocomplete(
- /** @type {!HTMLInputElement} */(document.getElementById('__id_input__'))
- );
- // When the user selects an address from the dropdown, populate the address fields in the form.
- glBxAutocomplete__name__.addListener('place_changed', callbackFillInAddress);
- }
- }
-
- $(document).ready(function () {
- if ('undefined' === typeof(google))
- bx_get_scripts(['https://maps.google.com/maps/api/js?libraries=places&language=__lang__&key=__key__'], fInitCallback);
- else
- fInitCallback();
- });
- </script>
|