Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div class="selector" id="store-chooser" style="width: 300px;">
- <select id="store" class="form-control" name="store">
- {foreach $stores as $store}
- <option class="storeValues" value="" data-latitude="{$store.latitude}" data-longitude="{$store.longitude}">{$store.name|escape:'html':'UTF-8'}</option>
- {/foreach}
- </select>
- </div>
- {if $geolocalization == 'true'}
- <script type="text/javascript">
- var set = new Array();
- $( "#store .storeValues" ).each(function(index) {
- var currentSet = new Array();
- currentSet.push($(this).data("latitude"), $(this).data("longitude"));
- set.push(currentSet);
- });
- jQuery.ajax( {
- url: '//freegeoip.net/json/',
- type: 'POST',
- dataType: 'jsonp',
- success: function(location) {
- var city = location.city;
- var ip = location.ip;
- var lat = location.latitude;
- var lon = location.longitude;
- var founded = UserLocation( lat, lon, set );
- console.log("trovato = " + founded);
- }
- } );
- function NearestCity( latitude, longitude, set ) {
- var mindif = 99999;
- var closest;
- for (index = 0; index < set.length; ++index) {
- var dif = PythagorasEquirectangular( latitude, longitude, set[ index ][ 0 ], set[ index ][ 1 ] );
- if ( dif < mindif ) {
- closest = index;
- mindif = dif;
- }
- }
- return set[closest];
- }
- function UserLocation( latitude, longitude, set ) {
- NearestCity( latitude, longitude, set );
- }
- function Deg2Rad( deg ) {
- return deg * Math.PI / 180;
- }
- function PythagorasEquirectangular( lat1, lon1, lat2, lon2 ) {
- lat1 = Deg2Rad(lat1);
- lat2 = Deg2Rad(lat2);
- lon1 = Deg2Rad(lon1);
- lon2 = Deg2Rad(lon2);
- var R = 6371; // km
- var x = (lon2-lon1) * Math.cos((lat1+lat2)/2);
- var y = (lat2-lat1);
- var d = Math.sqrt(x*x + y*y) * R;
- return d;
- }
- </script>
- {/if}
- {if $geolocalization != 'true'}
- <script type="text/javascript">
- console.warn('Store Chooser: localizzazione disattivata!');
- </script>
- {/if}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement