I have a Google Maps Geocoder and I am looking to refactor it. The user enters address details into a form where at the bottom there is the option to show on map.
- First the map is hidden
- The user clicks
show_map
checkbox - Map-area is shown
- Map is initialized
The codeAddress()
function is used if the user edits the address and wants to find it again. The showCoords()
function passes the longitude and latitude through to the form for processing. I am looking for a best practice to refactor this code.
function initialize() {
geocoder = new google.maps.Geocoder();
codeAddress();
map = new google.maps.Map(document.getElementById('map-canvas'));
marker = new google.maps.Marker({
map: map,
position: map.getCenter(),
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function() {
point = marker.getPosition();
map.panTo(point);
showCoords(point);
});
google.maps.event.addListener(map, 'dragend', function() {
point = marker.getPosition();
marker.setPosition(map.getCenter());
showCoords(point);
});
} /* END initialize */
function codeAddress(){
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
map.setZoom(15);
marker.setPosition(map.getCenter());
showCoords();
}else{
alert('Geocode was not successful for the following reason: ' + status)
}
});
}
function showCoords(point){
point = marker.getPosition();
document.getElementById("lat").value = point.lat().toFixed(5);
document.getElementById("lng").value = point.lng().toFixed(5);
}