Advertisement
metalx1000

GPS Distance Calculator

Oct 2nd, 2015
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <title>GPS Distance Calculator</title>
  5.   <meta charset="utf-8">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">
  7.   <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  9.   <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  10.   <script>
  11. //:::  Passed to function:                                                    :::
  12. //:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
  13. //:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
  14. //:::    unit = the unit you desire for results                               :::
  15. //:::           where: 'M' is statute miles (default)                         :::
  16. //:::                  'K' is kilometers                                      :::
  17. //:::                  'N' is nautical miles  
  18. //:::                  'F' is feet
  19.     function getDis(lat1, lon1, lat2, lon2, unit) {
  20.       var radlat1 = Math.PI * lat1/180
  21.       var radlat2 = Math.PI * lat2/180
  22.       var radlon1 = Math.PI * lon1/180
  23.       var radlon2 = Math.PI * lon2/180
  24.       var theta = lon1-lon2
  25.       var radtheta = Math.PI * theta/180
  26.       var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  27.       dist = Math.acos(dist)
  28.       dist = dist * 180/Math.PI
  29.       dist = dist * 60 * 1.1515
  30.       if (unit=="K") { dist = dist * 1.609344 }
  31.       if (unit=="N") { dist = dist * 0.8684 }
  32.       if (unit=="F") { dist = dist * 5280 }      
  33.       return dist
  34.     }
  35.  
  36.     $(document).ready(function(){
  37.       $("#calc").click(function(){
  38.         $("#list").html("");
  39.         var gps1 = $("#loc1").val().split(",");
  40.         var gps2 = $("#loc2").val().split(",");
  41.         var miles = getDis(gps1[0],gps1[1],gps2[0],gps2[1]);
  42.         $("#list").append('<li class="list-group-item">Miles: ' + miles + '</li>');
  43.  
  44.         var kilometers = getDis(gps1[0],gps1[1],gps2[0],gps2[1],"K");
  45.         $("#list").append('<li class="list-group-item">Kilometers: ' + kilometers + '</li>');
  46.  
  47.         var feet = getDis(gps1[0],gps1[1],gps2[0],gps2[1],"F");
  48.         $("#list").append('<li class="list-group-item">Feet: ' + feet + '</li>');
  49.       });
  50.     });
  51.  
  52.   </script>
  53. </head>
  54. <body>
  55. <div class="container">
  56.   <h2 id="title">Distance Calculator</h2>
  57.   <label for="loc1">GPS#1:</label>
  58.   <input type="text" class="form-control" id="loc1" placeholder="26.239289, -81.583377">
  59.   <label for="loc2">GPS#2:</label>
  60.   <input type="text" class="form-control" id="loc2" placeholder="26.239289, -81.583366">
  61.   <br>
  62.   <button id="calc" type="button" class="btn btn-primary btn-block">Calculate</button>
  63.   <hr>
  64.   <ul class="list-group" id="list">
  65.   </ul>
  66. </div>
  67.  
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement