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() {

map: map,
position: map.getCenter(),
draggable: true
});

point = marker.getPosition();
map.panTo(point);
showCoords(point);
});

point = marker.getPosition();
marker.setPosition(map.getCenter());
showCoords(point);
});

} /* END initialize */

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);
}


The main change I would make is to reduce the responsibility of codeAddress() to do just the geocoding. If it successfully looks up the address, then it can pass the location to a callback.

I don't have enough context to tell whether geocoder, map, and marker are global variables, or whether all of this code lies inside some other scope. Out of habit, I've converted them into local variables. You decide whether that's right for you. The point variables in your drag handlers should definitely be made local, though!

function initialize() {

map: map,
position: map.getCenter(),
draggable: true
});

var point = marker.getPosition();
map.panTo(point);
showCoords(point);
});

var point = marker.getPosition();
marker.setPosition(map.getCenter());
showCoords(point);
});

map.setCenter(results[0].geometry.location);
map.setZoom(15);
marker.setPosition(map.getCenter());
showCoords();
});
}