PHP:谷歌地图的2个地址之间的距离?

时间:2016-03-22 00:06:08

标签: php google-maps

我尝试使用下面的PHP脚本通过Google Maps API获取两个位置之间的距离,这显然有用(他们的托管测试网址可以在下面添加),但是当我使用他们在我提供的代码时自己的服务器似乎没有做任何事情......顺便说一句,PHP工作得很好,而且它似乎没有做任何有趣的事情,需要我在我的ini文件中打开一些东西。 / p>

我不确定问题是什么......一整天都在这里。

有什么想法吗?也许是我的.ini?

我不确定我在这里失踪了什么。我使用共享主机,但这不会有所作为。它应该在技术上按原样工作,这就是我在这里的原因 - 我需要一些见解。

网站源代码: http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/#comment-24117

他们的现场演示:http://demos.codexworld.com/distance-between-two-addresses-google-maps-api-php/

我的完整测试代码:

<?php
/**
*
* Author: CodexWorld
* Function Name: getDistance()
* $addressFrom => From address.
* $addressTo => To address.
* $unit => Unit type.
*
**/

    function getDistance($addressFrom, $addressTo, $unit){
        //Change address format
        $formattedAddrFrom = str_replace(' ','+',$addressFrom);
        $formattedAddrTo = str_replace(' ','+',$addressTo);

        //Send request and receive json data
        $geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false');
        $outputFrom = json_decode($geocodeFrom);
        $geocodeTo = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false');
        $outputTo = json_decode($geocodeTo);

        //Get latitude and longitude from geo data
        $latitudeFrom = $outputFrom->results[0]->geometry->location->lat;
        $longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
        $latitudeTo = $outputTo->results[0]->geometry->location->lat;
        $longitudeTo = $outputTo->results[0]->geometry->location->lng;

        //Calculate distance from latitude and longitude
        $theta = $longitudeFrom - $longitudeTo;
        $dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);
        if ($unit == "K") {
            return ($miles * 1.609344).' km';
        } else if ($unit == "N") {
            return ($miles * 0.8684).' nm';
        } else {
            return $miles.' mi';
        }
    }

    if (!empty($_POST['addrFrom']) && !empty($_POST['addrTo'])) { 
        $addressFrom = htmlspecialchars($_POST['addrFrom']);
        $addressTo = htmlspecialchars($_POST['addrTo']);
        $distance = getDistance($addressFrom, $addressTo, "K");
        $success = true;
    }
?>

<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        <p class="distance"><span>Distance:<?php if ($success == true) { echo ' ' . $distance; } ?></span> </p>
        <p><label>Address From</label><input type="text" name="addrFrom" value=""></p>
        <p><label>Address To</label><input type="text" name="addrTo" value=""></p>
        <p><input type="submit" name="submit" value="Calculate Distance"></p>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个 - 略微修改你的做法..:

<?php
/**
* Author: CodexWorld
* Function Name: getDistance()
* $addressFrom => From address.
* $addressTo => To address.
* $unit => Unit type.
*
**/

    function getDistance($addressFrom, $addressTo, $unit){
        //Change address format
        $formattedAddrFrom = str_replace(' ','+',$addressFrom);
        $formattedAddrTo = str_replace(' ','+',$addressTo);

        //Send request and receive json data
        $geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false');
        $outputFrom = json_decode($geocodeFrom);
        $geocodeTo = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false');
        $outputTo = json_decode($geocodeTo);

        //Get latitude and longitude from geo data
        $latitudeFrom = $outputFrom->results[0]->geometry->location->lat;
        $longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
        $latitudeTo = $outputTo->results[0]->geometry->location->lat;
        $longitudeTo = $outputTo->results[0]->geometry->location->lng;

        //Calculate distance from latitude and longitude
        $theta = $longitudeFrom - $longitudeTo;
        $dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);
        if ($unit == "K") {
            return ($miles * 1.609344).' km';
        } else if ($unit == "N") {
            return ($miles * 0.8684).' nm';
        } else {
            return $miles.' mi';
        }
    }

    if (isset($_POST['submit'])) {
        $addressFrom = htmlspecialchars($_POST['addrFrom']);
        $addressTo = htmlspecialchars($_POST['addrTo']);
        $distance = getDistance($addressFrom, $addressTo, "K");
        echo $distance;    
    }

?>

<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        <p><label>Address From</label><input type="text" name="addrFrom" value=""></p>
        <p><label>Address To</label><input type="text" name="addrTo" value=""></p>
        <p><input type="submit" name="submit" value="Calculate Distance"></p>
    </form>
</body>
</html>