Php 在谷歌地图和方向中获取距离和时间

Php 在谷歌地图和方向中获取距离和时间,php,jquery,google-maps,google-maps-api-3,google-maps-api-2,Php,Jquery,Google Maps,Google Maps Api 3,Google Maps Api 2,我正在使用谷歌地图开发一个简单的计程车票价计算程序,从谷歌地图中获取方向 下面是代码: HTML(map.php) 用户将输入两个不同的位置(从和到),然后计算距离和时间。。现在,我怎样才能得到距离和时间的值,这样我就可以用它来计算票价了 谢谢您的帮助。您应该使用。它可以计算2个(或更多)点之间的时间和距离。看一看我已经计算了时间和距离,并显示了结果,效果很好。但问题是我无法得到距离和时间的价值。就像谷歌地图中的get direction:[link]@JaybeeJohnBalog你说的“我已

我正在使用谷歌地图开发一个简单的计程车票价计算程序,从谷歌地图中获取方向

下面是代码:

HTML(map.php)

用户将输入两个不同的位置(从和到),然后计算距离和时间。。现在,我怎样才能得到距离和时间的值,这样我就可以用它来计算票价了


谢谢您的帮助。

您应该使用。它可以计算2个(或更多)点之间的时间和距离。看一看

我已经计算了时间和距离,并显示了结果,效果很好。但问题是我无法得到距离和时间的价值。就像谷歌地图中的get direction:[link]@JaybeeJohnBalog你说的“我已经计算了时间和距离”和“我无法得到距离和时间的值”是什么意思?对我来说似乎是矛盾的。你能详细说明一下吗?我的意思是,代码可以很好地输出时间和距离加上其他结果。。但似乎所有的结果都是一样的,我只想抓住时间和距离的价值。。请参见此示例..[link]您的代码使用的是不推荐的。您应该尽快升级到v3。我如何升级。?。你能给我看一些教程吗。请我只是一个使用javascript的新手。v2文档的最顶端写了以下内容,并提供了一个警告:Google Maps javascript API的第2版不再可用。请按照指南将代码迁移到Google Maps JavaScript API的第3版。截至2013年11月19日,所有请求v2的应用程序都将使用v3api的特殊包装版本。我们强烈建议您立即迁移到V3API。
<html lang="en">
<head>
    <title>Demo GPS plugin</title>
<style>
    #map {
        width: 500px;
        height: 200px;
    }
</style>
<script type="text/javascript" src="jquery.js"></script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=AIzaSyC7DlktbkGzLS0dvYqRJaGtbXG6mmoGUhA" type="text/javascript"></script>
<script type="text/javascript" src="gps.jquery.js"></script>
<script type="text/javascript">
$(function() {

$("#map").googleMap().load();

});
</script>
</head>
<body onunload="GUnload()">
    <div id="directions"></div>
    <div id="map"></div>

    <form action="directions.php" method="post">
        <p><b>From: </b><input id="start" type="text"/>
        <b>To: </b><input id="end" type="text" /></p>
        <input name="submit" id="getdirections" type="submit" value="Get Directions" />
    </form>

</body>
</html>
(function($) {

    $.GoogleMapObjectDefaults = {        
        zoomLevel: 14,
    imagewidth: 50,
    imageheight: 50,
    start: '#start',
        end: '#end',
    directions: 'directions',
        submit: '#getdirections',
    tooltip: 'false',
    image: 'false'
    };

    function GoogleMapObject(elementId, options) {
        /* private variables */
        this._inited = false;
        this._map = null;
        this._geocoder = null;

        /* Public properties */
        this.ElementId = elementId;
        this.Settings = $.extend({}, $.GoogleMapObjectDefaults, options || '');
    }

    $.extend(GoogleMapObject.prototype, {
        init: function() {
            if (!this._inited) {
                if (GBrowserIsCompatible()) {
                    this._map = new GMap2(document.getElementById(this.ElementId));
                    this._map.addControl(new GSmallMapControl());
                    this._geocoder = new GClientGeocoder();
                }

                this._inited = true;
            }
        },
        load: function() {
            //ensure existence
            this.init();

            if (this._geocoder) {
                //"this" will be in the wrong context for the callback
                var zoom = this.Settings.zoomLevel;
                var center = this.Settings.center;
        var width = this.Settings.imagewidth;
        var height = this.Settings.imageheight;
                var map = this._map;

        if (this.Settings.tooltip != 'false') {
            var customtooltip = true;
            var tooltipinfo = this.Settings.tooltip;
        }

        if (this.Settings.image != 'false') {
            var customimage = true;
            var imageurl = this.Settings.image;
        }

                this._geocoder.getLatLng(center, function(point) {
                    if (!point) { alert(center + " not found"); }
                    else {
                        //set center on the map
                        map.setCenter(point, zoom);

            if (customimage == true) {
                //add the marker
                var customiconsize = new GSize(width, height);
                var customicon = new GIcon(G_DEFAULT_ICON, imageurl);
                customicon.iconSize = customiconsize;
                var marker = new GMarker(point, { icon: customicon });
                map.addOverlay(marker);
            } else {
                var marker = new GMarker(point);
                map.addOverlay(marker);
            }

            if(customtooltip == true) {
                marker.openInfoWindowHtml(tooltipinfo);
            }
                    }
                });
            }


            //make this available to the click element
            $.data($(this.Settings.submit)[0], 'inst', this);

            $(this.Settings.submit).click(function(e) {
                e.preventDefault();
                var obj = $.data(this, 'inst');
        var outputto = obj.Settings.directions;
                var from = $(obj.Settings.start).val();
                var to = $(obj.Settings.end).val();
        map.clearOverlays();
        var gdir = new GDirections(map, document.getElementById(outputto));
        gdir.load("from: " + from + " to: " + to);

                //open the google window
                //window.open("http://maps.google.com/maps?saddr=" + from + "&daddr=" + to, "GoogleWin", "menubar=1,resizable=1,scrollbars=1,width=750,height=500,left=10,top=10");
            });

            return this;
        }
    });

    $.extend($.fn, {
        googleMap: function(options) {
            // check if a map was already created
            var mapInst = $.data(this[0], 'googleMap');
            if (mapInst) {
                return mapInst;

            }


            //create a new map instance
            mapInst = new GoogleMapObject($(this).attr('id'), options);
            $.data(this[0], 'googleMap', mapInst);

            return mapInst;

        }
    });
})(jQuery);