0

For now it is working with alert but if I want to add the result wrapped in a div it doesn't work. Can anyone please explain to me how shall I do this?

<html>
</head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script>
var distance;
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

//document.getElementById("distance").innerHTML = calcDistance(p1, p2);
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>
</head>
    </body>
        <div id="distance"></div>
    </body>
</html>
Markus Hayner
  • 2,869
  • 2
  • 28
  • 60
  • How did you try to show it in the DIV? – Zoltán Tamási Feb 15 '16 at 10:15
  • 1
    @ZoltánTamási — There's a commented out line (which would have produced an error message in the Console that the OP should have quoted when they wrote the question instead of just saying 'it doesn't work') – Quentin Feb 15 '16 at 10:16
  • 2
    Try by putting your `script` after `html` – ozil Feb 15 '16 at 10:19
  • 1
    You code does not work since it gets called before the div exists. You should call your code after the DOM is ready in document.onload. – phw Feb 15 '16 at 10:21
  • 1
    put your script after the html and comment out your document... etc line, i cannot see how this is duplicate anyway ... this is happening more often lately, closing questions without reading them – Vitaliy Terziev Feb 15 '16 at 10:21
  • Thanks guys. Just before closing the body tag is working. How I couldn't think about it. Anyway be blessed. – Markus Hayner Feb 15 '16 at 10:25
  • put your external and custom script after everything in body tag.... it will solve your problem :) – Akshay Kumar Feb 15 '16 at 10:26

0 Answers0