Skip to content

Instantly share code, notes, and snippets.

@alanleard
Last active December 14, 2015 01:59
Show Gist options
  • Save alanleard/5009974 to your computer and use it in GitHub Desktop.
Save alanleard/5009974 to your computer and use it in GitHub Desktop.
Geolocation returning County
Ti.Geolocation.purpose = "Locate your current county";
Ti.Geolocation.getCurrentPosition(function(e){
if(Ti.Geolocation.locationServicesAuthorization != 2){
var lat = e.coords.latitude;
var lng = e.coords.longitude;
getCounty({county:"County", url:"http://labs.silverbiology.com/countylookup/lookup.php?cmd=findCounty&DecimalLatitude="+lat+"&DecimalLongitude="+lng});
} else {
var alertDialog = Ti.UI.createAlertDialog({
title:"Please enter your city.",
style: Titanium.UI.iPhone.AlertDialogStyle.PLAIN_TEXT_INPUT
});
alertDialog.show();
alertDialog.addEventListener("click", function(e){
getCounty({url:"http://api.sba.gov/geodata/all_links_for_city_of/"+e.text+"/CA.json", county:"county_name"})
})
}
});
function getCounty(args){
Ti.API.info(args.url);
var xhr = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
var response = JSON.parse(this.responseText);
if(args.county == "county_name"){
alert(response[0][args.county]);
}else{
alert(response[args.county]);
}
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e);
alert(e);
},
timeout : 20000 // in milliseconds
});
// Prepare the connection.
xhr.open("GET", args.url);
// Send the request.
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment