Skip to content

Instantly share code, notes, and snippets.

@philbar
Last active August 29, 2015 14:11
Show Gist options
  • Save philbar/880285d3655981c94b84 to your computer and use it in GitHub Desktop.
Save philbar/880285d3655981c94b84 to your computer and use it in GitHub Desktop.
geolocation.html
<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script>
<script type="text/javascript">
var onSuccess = function(location){
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 wCmp = "";
var value = null;
for (var i in campus){
if (campus[i] < value || value == null) {
value = campus[i];
wCmp = i;
}
}
setCookie('geoCampus', wCmp, 30);
setCookie('geoCity', vCty, 30);
setCookie('geoConfidence', vCon, 30);
setGeoValue(wCmp, vCon);
};
var onError = function(error){
// location not found via IP
};
onload = function() {
if (getCookie('geoCampus') && getCookie('geoConfidence')){
setGeoValue(getCookie('geoCampus'), getCookie('geoConfidence'));
}else{
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));
}
function setGeoValue(pCampus, pConfidence){
objGeoCampus = document.getElementById('geo_campus');
objGeoConfidence = document.getElementById('geo_confidence');
if (objGeoCampus && objGeoConfidence){
objGeoCampus.value = pCampus;
objGeoConfidence.value = pConfidence;
}
}
function setCookie(cname, cvalue, exdays) {
// setCookie("cookie.name", "cookie.data", number of days);
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
</script>
@haldous2
Copy link

It appears that maxmind isn't using the function calls you are proposing. Their api calls only accept 3 inputs: success, error and options function. without further adieu, an updated script

<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