﻿       var minDistance = -1;
       var activePoi = 0;
       /** Converts numeric degrees to radians */
    if (typeof(Number.prototype.toRad) === "undefined") {
      Number.prototype.toRad = function() {
        return this * Math.PI / 180;
      }
    }

       // report errors to user
       function errorHandler(error) {
         switch (error.code) {
          case error.PERMISSION_DENIED:
           // alert("Could not get position as permission was denied.");
            break;
          case error.POSITION_UNAVAILABLE:
          //  alert("Could not get position as this information is not available at this time.");
            break;
           case error.TIMEOUT:
          //   alert("Attempt to get position timed out.");
            break;
           default:
        //    alert("Sorry, an error occurred. Code: " + error.code + " Message: " + error.message);
            break;
           }
}
    function CalcDistance(lat1,lon1,lat2,lon2,id) {
        var R = 6371; // km
        var dLat = (lat2 - lat1).toRad();
        var dLon = (lon2 - lon1).toRad();
        var lat1 = lat1.toRad();
        var lat2 = lat2.toRad();

        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c;

        if (minDistance == -1) {
            minDistance = d;
            activePoi = id;
        }
        else if (minDistance > d) {
        minDistance = d;
            activePoi = id;
        }
       
    }
