Skip to content

Instantly share code, notes, and snippets.

@emilepetrone
Created November 23, 2011 19:50
Show Gist options
  • Save emilepetrone/1389705 to your computer and use it in GitHub Desktop.
Save emilepetrone/1389705 to your computer and use it in GitHub Desktop.
SimpleGeo JS Context
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.simplegeo.com/js/1.2/simplegeo.all.jq.min.js"></script>
</head>
<body>
<script type="text/javascript">
var client = new simplegeo.ContextClient('JSON_TOKEN'); //json code of simple geo
$(document).ready(function() {
var lat,lon, callback;
client.getLocationFromIP(function(err, position) {
if (err) {
alert("Couldn\'t connect to SG");
} else {
var coords = position.coords;
lat = coords.latitude;
lon = coords.longitude;
console.log(lat);
console.log(lon);
client.getContext(lat, lon, function(err, data) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(data));
for (var i = 0, ii = data.features.length; i < ii ; i++) {
// switch on the category
switch(data.features[i]["classifiers"][0]["category"]) {
// Return the Neighborhood as an example
case "Administrative":
console.log(data.features[i]["name"]);
break;
};
};
};
});
};
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment