Advertisement
mstranieri

Find nearby

Feb 5th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.16 KB | None | 0 0
  1.  
  2. <div class="selector" id="store-chooser" style="width: 300px;">
  3.     <select id="store" class="form-control" name="store">
  4.         {foreach $stores as $store}
  5.             <option class="storeValues" value="" data-latitude="{$store.latitude}" data-longitude="{$store.longitude}">{$store.name|escape:'html':'UTF-8'}</option>
  6.         {/foreach}
  7.     </select>
  8. </div>
  9.  
  10.  
  11. {if $geolocalization == 'true'}
  12.     <script type="text/javascript">
  13.         var set = new Array();
  14.  
  15.         $( "#store .storeValues" ).each(function(index) {
  16.             var currentSet = new Array();
  17.             currentSet.push($(this).data("latitude"), $(this).data("longitude"));
  18.             set.push(currentSet);
  19.         });
  20.  
  21.         jQuery.ajax( {
  22.           url: '//freegeoip.net/json/',
  23.           type: 'POST',
  24.           dataType: 'jsonp',
  25.           success: function(location) {
  26.             var city = location.city;
  27.             var ip = location.ip;
  28.             var lat = location.latitude;
  29.             var lon = location.longitude;
  30.             var founded = UserLocation( lat, lon, set );
  31.  
  32.             console.log("trovato = " + founded);
  33.           }
  34.         } );
  35.  
  36.         function NearestCity( latitude, longitude, set ) {
  37.             var mindif = 99999;
  38.             var closest;
  39.  
  40.             for (index = 0; index < set.length; ++index) {
  41.                 var dif =  PythagorasEquirectangular( latitude, longitude, set[ index ][ 0 ], set[ index ][ 1 ] );
  42.  
  43.                 if ( dif < mindif ) {
  44.                     closest = index;
  45.                     mindif = dif;
  46.                 }
  47.             }
  48.  
  49.             return set[closest];
  50.         }
  51.  
  52.         function UserLocation( latitude, longitude, set ) {
  53.             NearestCity( latitude, longitude, set );
  54.         }
  55.  
  56.         function Deg2Rad( deg ) {
  57.            return deg * Math.PI / 180;
  58.         }
  59.  
  60.         function PythagorasEquirectangular( lat1, lon1, lat2, lon2 ) {
  61.             lat1 = Deg2Rad(lat1);
  62.             lat2 = Deg2Rad(lat2);
  63.             lon1 = Deg2Rad(lon1);
  64.             lon2 = Deg2Rad(lon2);
  65.             var R = 6371; // km
  66.             var x = (lon2-lon1) * Math.cos((lat1+lat2)/2);
  67.             var y = (lat2-lat1);
  68.             var d = Math.sqrt(x*x + y*y) * R;
  69.             return d;
  70.         }
  71.     </script>
  72. {/if}
  73.  
  74.  
  75. {if $geolocalization != 'true'}
  76.     <script type="text/javascript">
  77.         console.warn('Store Chooser: localizzazione disattivata!');
  78.     </script>
  79. {/if}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement