Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created February 12, 2011 02:53
Show Gist options
  • Save trshafer/823440 to your computer and use it in GitHub Desktop.
Save trshafer/823440 to your computer and use it in GitHub Desktop.
gets a codified location from any search or current locationand calls a callback with that result
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
<script type="text/javascript" src="http://cdn.simplegeo.com/js/1.2/simplegeo.all.jq.min.js"></script>
<script type="text/javascript" charset="utf-8">
;var Maps = function(){
var usefulGoogleMapsMapping = { administrative_area_level_1: 'state', administrative_area_level_2: 'county', locality: 'city', postal_code: 'zip' },
geocoder = new google.maps.Geocoder(),
defaultOptions = {promptNative: true},
geoClient;
function init(options){
if(typeof options == 'undefined'){options={};}
if(options.geoKey){
geoClient = new simplegeo.ContextClient(options.geoKey);
}
if(options.promptNative != null){
defaultOptions.promptNative = options.promptNative;
}
if(options.ip != null){
defaultOptions.ip = options.ip;
}
}
function mergeOptions(options, defaultOptions){
for (attrname in defaultOptions) { if(typeof options[attrname] == 'undefined'){options[attrname] = defaultOptions[attrname];} }
return options;
}
function search(term, callback){
geocoder.geocode( {address: term}, function(result, status) {
var addressIWant = convertGoogleToUsable(result);
addressIWant.term = term;
callback(addressIWant);
});
}
// options {promptNative: (true|false), ipAddress: 'ip'}
function currentLocation(options, callback){
if(typeof callback == 'undefined'){ callback = options; options = {};} //to allow no options
options = mergeOptions(options, defaultOptions);
tryBrowserNative(options, callback) ||
tryGoogleGears(options, callback) ||
tryViaIPAddress(options, callback) ||
emptyCallback(callback);
}
function tryBrowserNative(options, callback){
if(!options.promptNative || !navigator.geolocation){return false;}
navigator.geolocation.getCurrentPosition(function(position) {
//success from browser
callbackFromPosition('navigator', position, callback);
}, function() {
// failure from browser
tryGoogleGears(options, callback) || tryViaIPAddress(options, callback) || emptyCallback(callback);
});
return true;
}
function tryGoogleGears(options, callback){
if(!google.gears){ return false;}
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
//success from gears
callbackFromPosition('gears', position, callback);
}, function() {
//failure from gears, try via IP
tryViaIPAddress(options, callback) || emptyCallback(callback);
});
return true;
}
function tryViaIPAddress(options, callback){
if(!options.ip || !geoClient){return false;}
geoClient.getLocationFromIP(options.ip, function(err, position){
if(!err){
callbackFromPosition('ip', position, callback);
}else{
emptyCallback(callback);
}
});
return true;
}
function emptyCallback(callback){
callback(null);
}
function callbackFromPosition(source, position, callback){
var l = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({'location': l}, function(result, status){
// success from google
var addressIWant = convertGoogleToUsable(result);
addressIWant.source = source;
callback(addressIWant);
});
}
function convertGoogleToUsable(result){
var rawAddress = result[0]['address_components'];
var addressIWant = {formatted_address: result[0].formatted_address};
rawAddress.forEach(function(item){
addressIWant[usefulGoogleMapsMapping[item.types[0]] || item.types[0]] = item.short_name;
});
return addressIWant;
}
return {
init: init,
search: search,
currentLocation: currentLocation,
defaultOptions: defaultOptions
};
}();
//USAGE:
//defaultOptions: {promptNative: true}
Maps.init({promptNative: true, ip: '173.228.44.97', geoKey:'simpleGeoKey'});
Maps.search('The White House', function(result){
console.log('search:', result);
});
Maps.currentLocation({promptNative: false, ip: '194.125.35.12'}, function(result){
console.log('currentLocation:', result);
});
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment