Skip to content

Instantly share code, notes, and snippets.

@jonrohan
Created January 21, 2011 23:05
Show Gist options
  • Save jonrohan/790607 to your computer and use it in GitHub Desktop.
Save jonrohan/790607 to your computer and use it in GitHub Desktop.
auto-fill-form.html
<!DOCTYPE html>
<html>
<head>
<title>Autofill Form with SimpleGeo</title>
</head>
<body style="padding:20px 50px;">
<h3>Example getting user's context using getLocation and pre-populating the form</h3>
<form action="." method="post">
<p>
<label for="name">Name</label><br/>
<input id="name" name="name" autofocus/>
</p>
<p>
<label for="address">Address</label><br/>
<input id="address" name="address"/>
</p>
<p>
<label for="city">City</label><br/>
<input id="city" name="city"/>
</p>
<p>
<label for="state">State</label><br/>
<input id="state" name="state"/>
</p>
<p>
<label for="zipcode">Zipcode</label><br/>
<input id="zipcode" name="zipcode"/>
</p>
<p>
<label for="country">Country</label><br/>
<input id="country" name="country"/>
</p>
<p>
<button>Submit</button>
</p>
</form>
<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.context.jq.min.js"></script>
<script type="text/javascript">
var client = new simplegeo.ContextClient('9QwstD6wHTZwPauttPwuksJqaCMGuhKe');
$(document).ready(function() {
client.getLocation(function(err, position) {
// get the user's context for the found locaiton
client.getContext(position.coords.latitude, position.coords.longitude,
function(err, data) {
// we have an error
if (err)
(typeof console == "undefined") ? alert(err) : console.error(err);
else {
for (var i = 0, ii = data.features.length; i < ii ; i++) {
// switch on the category
switch(data.features[i]["classifiers"][0]["category"]) {
// Country
case "National":
if (data.features[i]["abbr"]&&data.features[i]["abbr"]!="")
$("#country").val(data.features[i]["abbr"]);
else
$("#country").val(data.features[i]["name"]);
break;
// City
case "Municipal":
$("#city").val(data.features[i]["name"]);
break;
// Urban Area
case "Urban Area":
if(!$("#city").val()&&$("#city").val()=="")
$("#city").val(data.features[i]["name"]);
break;
// State
case "Subnational":
if (data.features[i]["abbr"]&&data.features[i]["abbr"]!="")
$("#state").val(data.features[i]["abbr"]);
else
$("#state").val(data.features[i]["name"]);
break;
// Zipcode
case "Postal Code":
$("#zipcode").val(data.features[i]["name"]);
break;
}
}
}
});
});
});
</script>
</body>
</html>
@ritujangid
Copy link

shubham saini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment