Created
September 18, 2012 18:07
-
-
Save rmurphey/3744730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
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.
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
Nice. Love using hash tables like this, as opposed to switch statements.