duanditang2916 2016-03-22 00:06
浏览 105

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

I'm trying to get the distance between 2 locations through Google Maps API using the below PHP script, which apparently works (their hosted test url works, added below), but when I use the code they provide on my own server it doesn't seem to do anything... PHP is working just fine by the way, and it doesn't seem to be doing anything funny that would require me turning something on in my ini file.

I'm not sure what the issue is... been at this all day.

Any ideas? Maybe it is my .ini?

I'm not sure what I'm missing here. I'm using a shared host, but that shouldn't make a difference. It should technically work as is, any way that is why I'm here - I need some insight.


Website with source code: http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/#comment-24117

Their live demo: http://demos.codexworld.com/distance-between-two-addresses-google-maps-api-php/

My full test code:

<?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条回答 默认 最新

  • douyinglan2599 2017-12-07 16:50
    关注

    Try This - slightly modified your way of doing it..:

    <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录