Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created September 18, 2012 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmurphey/3744730 to your computer and use it in GitHub Desktop.
Save rmurphey/3744730 to your computer and use it in GitHub Desktop.
// repetitive code!
function selectedLocation() {
var selected = document.getElementById("countyLocation").value;
if (selected == "Ireland") {
var Ireland = new google.maps.LatLng(53.527248, -8.327637);
map.setCenter(Ireland);
map.setZoom(6);
}
if (selected == "Clare") {
var Clare = new google.maps.LatLng(52.988337, -9.102173);
map.setCenter(Clare);
map.setZoom(8);
}
if (selected == "Dublin") {
var Dublin = new google.maps.LatLng(53.399707, -6.262207);
map.setCenter(Dublin);
map.setZoom(9);
}
}
function selectedLocation() {
var selected = document.getElementById("countyLocation").value;
var locations = {
"Ireland" : {
lat : 53.527248,
lng : -8.327637,
zoom : 6
},
"Clare" : {
lat : 52.988337,
lng : -9.102173,
zoom : 8
},
"Dublin" : {
lat : 53.399707,
lng : -6.262207,
zoom : 9
}
};
var location = locations[ selected ];
var center = new google.maps.LatLng(location.lat, location.lng);
map.setCenter(center);
map.setZoom(location.zoom);
}
@BlessYAHU
Copy link

Nice. Love using hash tables like this, as opposed to switch statements.

@wthit56
Copy link

wthit56 commented Nov 23, 2012

A couple of bugs:

  • selected should get "countryLocation" instead of "countyLocation" (before and after)
  • the locations object should be outside of the function, otherwise it would be created every time the function is called.

@juniovitorino
Copy link

I guess that you should test if location isn't undefined before instantiate a new Google Maps LatLng.

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