Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created June 2, 2013 22:57
Show Gist options
  • Save IQAndreas/5695249 to your computer and use it in GitHub Desktop.
Save IQAndreas/5695249 to your computer and use it in GitHub Desktop.
Cleaned up version of this awful code: https://gist.github.com/IQAndreas/5692007
// This code sets up the input box to auto-complete
$("#step1-search-address-input").autocomplete({
source: map.getAutocompleteData,
select: map.onAutocompleteSelect
});
// This code is contained in the Map class
Map.prototype.initialize = function()
{
console.log("initialize", this);
var mapOptions =
{
backgroundColor: '000',
center: new google.maps.LatLng(this.lat, this.lng),
zoom: 7,
mapTypeId: google.maps.MapTypeId.HYBRID,
//disableDefaultUI: true,
mapTypeControl: false,
streetViewControl: false,
rotateControl: false // Is this disabled by default anyway?
};
this.div = $("#map-canvas")[0];
this.reference = new google.maps.Map(this.div, mapOptions);
this.geocoder = new google.maps.Geocoder();
this.geocoderCache = { };
console.log("map initialized");
}
Map.prototype.getAutocompleteData = function(request, response)
{
// All code in this function is from
// http://gerger.co/blog/2011/02/17/google-maps-api-v3-location-search-with-jquery-autocomplete-plugin/
// (though, it was cleaned up drastically, because he is a dirty JavaScript developer)
var term = request.term;
if (map.geocoderCache[term])
{
response(map.geocoderCache[term]);
return;
}
map.geocoder.geocode( {'address': term }, onAddressResult);
function onAddressResult(results, status)
{
if (status != google.maps.GeocoderStatus.OK)
{ return console.log("[onAddressResult] Google maps error",'status=',status); }
console.log("onAddressResult", results);
if (results.length <= 0)
{
//No results found
console.log("No results found for", request.term);
}
else
{
//var searchLoc = results[0].geometry.location;
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
var latlng = new google.maps.LatLng(lat, lng);
var bounds = results[0].geometry.bounds;
map.setCenter(lat, lng, true);
if (bounds) // For some reason, some queries don't return bounds
map.reference.fitBounds(bounds);
map.geocoder.geocode({'latLng': latlng}, onLatLngResult);
}
}
function onLatLngResult(results, status)
{
if (status != google.maps.GeocoderStatus.OK)
{ return console.log("[onLatLngResult] Google maps error",'status=',status); }
console.log("onLatLngResult", results);
if (results[1]) {
data = formatResults(results);
map.geocoderCache[term] = data;
response(data);
}
}
function formatResults(results)
{
return jQuery.map(results, function(loc) {
return {
label : loc.formatted_address,
value : loc.formatted_address,
loc : loc,
bounds : loc.geometry.bounds
}
});
}
}
Map.prototype.onAutocompleteSelect = function(event, ui)
{
//var pos = ui.item.position;
//var lct = ui.item.locType;
console.log("onAutocompleteSelect");
var bounds = ui.item.bounds;
map.selectedAddress = ui.item.loc.formatted_address;
if (bounds)
{
map.reference.fitBounds(bounds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment