I've written a script with the Google Maps JavaScript API v3.
I've ran it through JSHint and everything seems to be valid.
I'm wondering if my script has room for improvement.
Any suggestions are very much welcome!
(function () {
'use strict';
var map,
markers = [];
function initMap() {
var paris = new google.maps.LatLng(48.856614, 2.3522219000000177),
options = {
// required
center: paris,
zoom: 10,
// disable all user interaction
disableDefaultUI: true,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
scrollwheel: false,
draggable: false,
zoomControl: false,
disableDoubleClickZoom: true
};
map = new google.maps.Map(document.getElementById('map'), options);
addMarker(paris, false);
// center map responsively
google.maps.event.addDomListener(window, 'resize', function () {
var center = map.getCenter();
google.maps.event.trigger(map, 'resize');
map.setCenter(center);
});
}
function geocodeAddress() {
var geocoder = new google.maps.Geocoder(),
addressInp = document.getElementById('address-inp');
geocoder.geocode({ 'address': addressInp.value }, function (results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
alert('Geocode was not successful for the following reason: ' + status);
return;
}
map.fitBounds(results[0].geometry.viewport);
map.setCenter(results[0].geometry.location);
deleteAllMarkers();
addMarker(results[0].geometry.location, true);
addressInp.value = results[0].formatted_address;
});
}
function addMarker(location, animateDrop) {
var marker = new google.maps.Marker({
map: map,
position: location
});
if (animateDrop) {
marker.setAnimation(google.maps.Animation.DROP);
}
google.maps.event.addListener(marker, 'click', function () {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
});
markers.push(marker);
}
function deleteAllMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
}
google.maps.event.addDomListener(window, 'load', initMap);
window.onload = function () {
var addressInp = document.getElementById('address-inp'),
searchBtn = document.getElementById('search-btn');
addressInp.onkeydown = function (e) {
if (e.keyCode === 13) {
searchBtn.click();
addressInp.blur();
}
};
searchBtn.onclick = function () {
geocodeAddress();
};
};
})();