简体   繁体   English

使用谷歌地图,PHP和MySQL从A点到B点的距离

[英]Distance from point A to B using Google Maps, PHP and MySQL

Can anyone please help. 谁能请帮忙。 I just need a simple script. 我只需要一个简单的脚本。 I have a form posted to another page. 我有一张表格张贴到另一页。 two formfields are: 两个表格是:

fromaddress and toaddress fromaddress和toaddress

The only thing I need is a script that shows me the distance in km and the time it takes using google maps. 我唯一需要的是一个脚本,它显示了以km为单位的距离以及使用谷歌地图所需的时间。 I have found dozens of scripts but I cannot get it working. 我找到了几十个脚本,但我无法使用它。 The map is already there with this code which seems to work perfect. 地图已经存在,这个代码看起来很完美。

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?key=AIzaSyDdDwV6_l5n2bS3gM6NBCla3RFLtIFc_HE&sensor=false"></script><script type="text/javascript">
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;

    function initialize() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var myOptions = {
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        directionsDisplay.setMap(map);

        var start = "<? echo $_POST[fromaddress]; ?>";
        var end = "<? echo $_POST[toaddress]; ?>";
        var request = {
            origin:start, 
            destination:end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

    }

    $(document).ready(function(){
        initialize();
    });        
</script>

But how do i display the distance and the time. 但是我如何显示距离和时间。 Best option is to get it in a php value like: 最好的选择是使用php值来获取它:

$distance = some-code;
$time = some-code-too;

Thanks a lot for helping. 非常感谢您的帮助。

You need to use different API for that. 您需要使用不同的API。 First of all, don't use JS but PHP, here's the code snippet that should work for you ;) 首先,不要使用JS而是使用PHP,这里的代码片段应该适合你;)

$from = "Więckowskiego 72, Łódź";
$to = "Gazowa 1, Łódź";

$from = urlencode($from);
$to = urlencode($to);

$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data);

$time = 0;
$distance = 0;

foreach($data->rows[0]->elements as $road) {
    $time += $road->duration->value;
    $distance += $road->distance->value;
}

echo "To: ".$data->destination_addresses[0];
echo "<br/>";
echo "From: ".$data->origin_addresses[0];
echo "<br/>";
echo "Time: ".$time." seconds";
echo "<br/>";
echo "Distance: ".$distance." meters";

Output: 输出:

To: Gazowa 1, 91-076 Łódź, Poland
From: Więckowskiego 72, Łódź, Poland
Time: 206 seconds
Distance: 1488 meters

You can calculate the complete route duration and distance by adding up all the legs: 您可以通过累加所有腿来计算完整的路线持续时间和距离:

  function computeTotals(result) {
  var totalDist = 0;
  var totalTime = 0;
  var myroute = result.routes[0];
  for (i = 0; i < myroute.legs.length; i++) {
    totalDist += myroute.legs[i].distance.value;
    totalTime += myroute.legs[i].duration.value;
  }
  totalDist = totalDist / 1000.
  document.getElementById("total").innerHTML = "total distance="+totalDist.toFixed(2) + " km ("+(totalDist*0.621371).toFixed(2)+" miles), time="+totalTime+" seconds";
  }

call it like this: 这样叫:

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            computeTotals(response);
        }
    });

working example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM