使用Google Maps,PHP和MySQL从A点到B点的距离 [英] Distance from point A to B using Google Maps, PHP and MySQL

查看:109
本文介绍了使用Google Maps,PHP和MySQL从A点到B点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请帮忙。我只需要一个简单的脚本。我有一个表单发布到另一个页面。两个表单域是:

fromaddress和toaddress



我需要的是一个脚本,它显示了距离在公里和使用谷歌地图的时间。我发现了几十个脚本,但我无法正常工作。

 < script type =text / javascriptsrc = //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;

函数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);
}
});


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

但我如何显示距离和时间。最佳选择是将它的值设为php:

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

非常感谢您的帮助。

解决方案

您需要为此使用不同的API。首先,不要使用JS而是使用PHP,这里是适合你的代码片段;)

  $ from = Więckowskiego72,Łó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=假);
$ data = json_decode($ data);

$ time = 0;
$ distance = 0;

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

echoTo:。$ data-> destination_addresses [0];
回声< br />;
echoFrom:。$ data-> origin_addresses [0];
回声< br />;
echoTime:。$ time。seconds;
回声< br />;
echoDistance:。$ distance。meters;

输出:

 收件人:Gazowa 1,91-076波兰Łódź
来自:Więckowskiego72,波兰罗兹
时间:206秒
距离:1488米


Can anyone please help. I just need a simple script. I have a form posted to another page. two formfields are:

fromaddress and toaddress

The only thing I need is a script that shows me the distance in km and the time it takes using google maps. 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:

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

Thanks a lot for helping.

解决方案

You need to use different API for that. First of all, don't use JS but PHP, here's the code snippet that should work for you ;)

$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

这篇关于使用Google Maps,PHP和MySQL从A点到B点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆