Q:

Google maps API - compute distance between clicked area and const location

I would like to compute distance between a clicked area and const lat lng location. 
I would like to combine these two pieces of code, but somehow I am not able to do it.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = HERE I WOULD LIKE TO HAVE A POSITION OF THE PLACED MARKER;

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
</script>

and
google.maps.event.addListener(map, 'click', function(event){
placeMarker(event.latLng);
});

function placeMarker(location) {
var marker = new google.maps.Marker({
    position: location, 
    map: map,
    draggable: true
});
}

A:

It looks like you just need to move the code that alerts the distance into the correct place...
google.maps.event.addListener(map, 'click', function(event){
    placeMarker(event.latLng);

    var p1 = new google.maps.LatLng(45.463688, 9.18814);

    setTimeout(function() {
        alert(calcDistance(p1, event.latLng));
    }, 500);
});

