// custom geocoder which calls both Google maps geocoder and Google Ajax local search apis // returns normalized result set // use like so: // MyApp.geocoder.getLocations('1060 W Addison St, Chicago, IL', function (results) { // // results is an array of result objects pulled from both Google Maps and Google Ajax APIS // // with normalized properties // }); // Uses jQuery's $.map() and $.merge() functions MyApp = {}; // namespace for app MyApp.geocoder = { getLocations: function (query, onComplete) { var geocoder = new GClientGeocoder(); var found = []; // getLocations from geocoder geocoder.getLocations(query, function (response) { if (response.Status.code == 200) { found = $.map(response.Placemark, function (n, i) { var obj = {}; obj.title = null; obj.address = n.address; obj.accuracy = n.AddressDetails.Accuracy; obj.lat = n.Point.coordinates[1]; obj.lng = n.Point.coordinates[0]; obj.source = 'geocoder'; return obj; }); } // get additional locations from localSearch var localSearch = new google.search.LocalSearch(); localSearch.setNoHtmlGeneration(); localSearch.setResultSetSize(GSearch.LARGE_RESULTSET); localSearch.setSearchCompleteCallback(this, MyApp.geocoder.processLocalSearchResult, [localSearch, found, onComplete]); localSearch.execute(query); }); }, processLocalSearchResult: function (ls, found, onComplete) { var results = $.merge(found, $.map(ls.results, function (n,i) { var obj = {}; obj.title = n.titleNoFormatting; if (n.addressLines) { obj.address = n.addressLines.join(', '); } obj.accuracy = n.accuracy; obj.lat = n.lat; obj.lng = n.lng; obj.source = 'local search'; return obj; }) ); onComplete.call(this, results); } };