Skip to content

Instantly share code, notes, and snippets.

@haldous2
Created December 16, 2014 19:09
Show Gist options
  • Save haldous2/bc447668e057531d77db to your computer and use it in GitHub Desktop.
Save haldous2/bc447668e057531d77db to your computer and use it in GitHub Desktop.
MaxMind IP location script and confidence
<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script>
<script type="text/javascript">
var onSuccess = function(location){
//alert(
// JSON.stringify(location, undefined, 4)
//);
vLat = location.location.latitude;
vLon = location.location.longitude;
vCty = location.city.names.en;
vCon = location.city.confidence;
campus = new Array();
campus['aviation'] = distance(vLat, vLon, 36.776811, -119.728209);
campus['bakersfield'] = distance(vLat, vLon, 35.352791, -119.062098);
campus['fresno'] = distance(vLat, vLon, 36.829833, -119.783895);
campus['hanford'] = distance(vLat, vLon, 36.325945, -119.647951);
campus['hesperia'] = distance(vLat, vLon, 34.420752, -117.383147);
campus['lancaster'] = distance(vLat, vLon, 34.648455, -118.148371);
campus['madera'] = distance(vLat, vLon, 36.976678, -120.082490);
campus['modesto'] = distance(vLat, vLon, 37.717470, -121.092853);
campus['ontario'] = distance(vLat, vLon, 34.069910, -117.552116);
campus['ranchocordova'] = distance(vLat, vLon, 38.593699, -121.288151);
campus['sandiego'] = distance(vLat, vLon, 32.634322, -117.078490);
campus['temecula'] = distance(vLat, vLon, 33.525832, -117.166448);
campus['visalia'] = distance(vLat, vLon, 36.328082, -119.384942);
//var index = 0;
//var value = temp[0];
var oStr = "";
for (var i in campus){
oStr = oStr + i + ' ' + campus[i] + '\n';
// if (temp[i] < value) {
// value = temp[i];
// index = i;
// }
}
alert(oStr);
};
var onError = function(error){
// location not found via IP
};
geoip2.insights(onSuccess, onError);
function distance(lat1, lon1, lat2, lon2) {
var R = 6371;
var a = 0.5 - Math.cos((lat2 - lat1) * Math.PI / 180)/2 + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * (1 - Math.cos((lon2 - lon1) * Math.PI / 180))/2;
return R * 2 * Math.asin(Math.sqrt(a));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment