GetDistance Javascript not returning nothing whilst having some values for as parameters

二次信任 提交于 2019-12-12 04:07:00

问题


I am trying to get a result from this function. However, nothing is being returned onto my page, just "This example calls a function which performs a calculation, and returns the result:". I need this function to get every postcode in the database from the properties tables and find the distance. Also, one postcode is set always going to be the same so I need to set that and the other postcode is going to be a postcode from the properties table.

<?php

    include ("../connect.php");

    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    </head>

    <body>
    <p>This example calls a function which performs a calculation, and returns the result:</p>

    <p id="demo"></p>

    <script type="text/javascript">
    var x = getDistance(52.482799000000000, -2.000643000000000, 52.48463500000000, -1.980759000000000);
    function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {  
        $earth_radius = 6371;  

        $postLat = deg2rad($latitude2 - $latitude1);  
        $postLon = deg2rad($longitude2 - $longitude1);  

        $a = sin($postLat/2) * sin($postLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($postLon/2) * sin($postLon/2);  
        $c = 2 * asin(sqrt($a));  
        $d = $earth_radius * $c;  
          //$distance_miles = $km * 0.621371; //distance in miles 
        return $d;
        document.getElementById("demo").innerHTML = x; 

    }
    </script>

    </body>
    </html>

回答1:


Where are you using the x variable?

I would expect something like this:

<p>This example calls a function which performs a calculation, and returns the result:
<div id="result">
</div>
</p>

var x = getDistance(52.482799000000000, -2.000643000000000, 52.48463500000000, -1.980759000000000);
$('#result').text(x);


来源:https://stackoverflow.com/questions/36234233/getdistance-javascript-not-returning-nothing-whilst-having-some-values-for-as-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!