Jquery谷歌地图运输计算器不再工作

时间:2013-12-10 11:21:53

标签: javascript jquery google-maps

Google地图是否改变了允许脚本访问它的方式?我问的原因是我有几个运输计算器的网站使用jquery和json来计算使用谷歌地图的邮政编码之间的距离。

问题可以在这里看到:

http://www.sos-motorcycle-recovery.co.uk/

在“获取和即时报价”部分下,如果您输入几个邮政编码,让我们说PE219PX,然后在结束旅程NG323RJ,通常会返回总距离和成本,但它没有做任何事情,并且这也发生在另一个使用相同脚本的网站上,这让我想知道谷歌是否改变了什么?问题是我没有看到任何错误,所以我甚至不确定从哪里开始。

运行它的代码看起来像这样:

    <div class="key4-module-inner">
      <div class="module-content"><script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=AIzaSyDfnkRoxyEYmXSgY_Dne74TaBAfglq5Lxk"></script>

      <script type="text/javascript">
    <!--

    var prefix = '/en';

    var map;
    var gdir1;
    var gdir2;
    var gdir3;
    var geocoder = null;
    var addressMarker;
    var inside_of_m25 = 0;
    var sevendaydelivery = 0;
    var gd_ready_1 = false;
    var gd_ready_2 = false;
    var do_additional_calculation = true;
    var interval_id = 0;

    var start_point = "NG32 3RJ";

    function initialize() {
        if (GBrowserIsCompatible()) {      
            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(48.9225, 5.5371), 4);
        }
    }

    function estimate() {
            if ( $('#collection_post_code').val() == '' || $('#delivery_post_code').val() == '' ) {
            alert( "Please input a post code" );
            return;
        }

        map.clearOverlays();

        if ( GBrowserIsCompatible() && do_additional_calculation ) {

            gdir1 = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir1, "addoverlay", onGDirectionsLoad1);
            GEvent.addListener(gdir1, "error", handleErrors1);

            setDirections( gdir1, start_point,
                            $('#collection_post_code').val(), 
                            null, null, null, null, "en_US" );

            gdir2 = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir2, "addoverlay", onGDirectionsLoad2);
            GEvent.addListener(gdir2, "error", handleErrors2);

            setDirections( gdir2, $('#delivery_post_code').val(),
                            start_point, null, null, null, null, "en_US" );
        }

        inside_of_m25 = $('#inside_of_m25:checked').val();
        sevendaydelivery = $('#sevendaydelivery:checked').val();

        switch_tab( 'temp_block' );

        interval_id = setInterval( "finish_estimation()", 100 );
    }

    function switch_tab( tab ) {

        $('#estimator_form').hide();
        $('#content').hide();
        $('#deliverycostwrapper').hide();
        $('#temp_block').hide();
        $('#sms_email_send_status').hide();
        $('.module-title').hide();

        $( '#' + tab ).show();
    }

    function setDirections( direction, 
                            fromAddress, 
                            toAddress, 
                            nextAddress1, 
                            nextAddress2, 
                            nextAddress3, 
                            nextAddress4, 
                            locale ) {

        var query = "from: " + fromAddress;

        if ( nextAddress1 != null && nextAddress1 != "" ) {
            query += ' to: ' + nextAddress1;
        }
        if ( nextAddress2 != null && nextAddress2 != "" ) {
            query += ' to: ' + nextAddress2;
        }
        if ( nextAddress3 != null && nextAddress3 != "" ) {
            query += ' to: ' + nextAddress3;
        }
        if ( nextAddress4 != null && nextAddress4 != "" ) {
            query += ' to: ' + nextAddress4;
        }
        query += " to: " + toAddress;

        direction.load( query, { "locale": locale } );
    }

    function handleErrors(direction, dir_num) {
        if (direction.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\n Error code:  " + direction.getStatus().code + " [" + dir_num + "]");
        else if (direction.getStatus().code == G_GEO_SERVER_ERROR)
            alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + direction.getStatus().code + " [" + dir_num + "]");
        else if (direction.getStatus().code == G_GEO_MISSING_QUERY)
            alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + direction.getStatus().code + " [" + dir_num + "]");
        else if (direction.getStatus().code == G_GEO_BAD_KEY)
            alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + direction.getStatus().code + " [" + dir_num + "]");
        else if (direction.getStatus().code == G_GEO_BAD_REQUEST)
            alert("A directions request could not be successfully parsed.\n Error code: " + direction.getStatus().code + " [" + dir_num + "]");
        else alert("An unknown error occurred." + " [" + dir_num + "]");

        clear_map();

        show_view_form();
    }

    function handleErrors1() {
        handleErrors(gdir1, 1);
    }

    function handleErrors2() {
        handleErrors(gdir2, 2);
    }

    function handleErrors3() {
        handleErrors(gdir3, 3);
    }

    function show_view_form( clear ) {

        if ( clear ) {
            clear_estimator_form();
            clear_map();
        }

        switch_tab( 'estimator_form' );
    }

    function clear_map() {
        map.clearOverlays();
        map.setCenter(new GLatLng(48.9225, 5.5371), 4); 
    }

    function clear_estimator_form() {

        $('#multiple_legs').attr('checked', '');
        $('#collection_post_code').val('');
        $('#next_post_code_1').val('');
        $('#next_post_code_2').val('');
        $('#next_post_code_3').val('');
        $('#delivery_post_code').val('');
        $('#inside_of_m25').attr('checked', '');
        $('#sevendaydelivery').attr('checked', '');

        multiple_legs_change(); 
    }

    function onGDirectionsLoad(direction, weight, show_results) {
        var poly = direction.getPolyline();
        var baseUrl = "http://maps.google.com/staticmap?";

        var params = [];
        var markersArray = [];
        markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
        markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
        params.push("markers=" + markersArray.join("|"));

        var polyParams = "rgba:0x0000FF80,weight:" + weight + "|";
        var polyLatLngs = [];
        for (var j = 0; j < poly.getVertexCount(); j++) {
            polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
        }

        if ( show_results ) {

            var distance1 = '0&nbsp;mi';
            var distance2 = '0&nbsp;mi';

            if ( do_additional_calculation ) {
                distance1 = gdir1.getDistance().html;
                distance2 = gdir2.getDistance().html;
            }

            $.post( 'return.php', 
                    { distance1: distance1,
                            distance2: distance2,
                            sevendaydelivery: sevendaydelivery,
                            distance3: gdir3.getDistance().html,
                            inside_of_m25: inside_of_m25 }, 
                    function(data) {
                        var deliverycost = (data);
                        simpleCart({
            // Set Delivery Cost
            shippingFlatRate: deliverycost });
                        $('#estimator_result_form').html( '' );
                        $('#estimator_result_form').append( data );
    $('#container').show();
                        switch_tab( 'estimator_result_form' );
                    } );

            inside_of_m25 = 0;
        }
    }

    function onGDirectionsLoad1() {
        onGDirectionsLoad( gdir1, 0, false );
        gd_ready_1 = true;
    }

    function onGDirectionsLoad2() {
        onGDirectionsLoad( gdir2, 0, false );
        gd_ready_2 = true;
    }

    function onGDirectionsLoad3() {
        onGDirectionsLoad( gdir3, 5, true );
    }

    function finish_estimation() {

        if ( ( do_additional_calculation && gd_ready_1 && gd_ready_2 ) || !do_additional_calculation ) {

            map.clearOverlays();

            gdir3 = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir3, "addoverlay", onGDirectionsLoad3);
            GEvent.addListener(gdir3, "error", handleErrors3);

            setDirections( gdir3, $('#collection_post_code').val(), 
                            $('#delivery_post_code').val(), 
                            $('#next_post_code_1').val(), 
                            $('#next_post_code_2').val(),
                            $('#next_post_code_3').val(),
                            $('#next_post_code_4').val(), 
                            "en_US" );

            gd_ready_1 = false;
            gd_ready_2 = false;

            clearInterval( interval_id );
        }
    }

    function multiple_legs_change() {       
        if ( $('#multiple_legs:checked').val() == 1 ) {
            $('.multi_legs_option_1').show();
        } else {
            $('#next_post_code_1').val('');
            $('#next_post_code_2').val('');
            $('#next_post_code_3').val('');
            $('#next_post_code_4').val('');     

            $('.multi_legs_option_1').hide();
            $('.multi_legs_option_2').hide();
            $('.multi_legs_option_3').hide();
            $('.multi_legs_option_4').hide();
        }
    };

    function delivery_post_code_change() {
         $('#collection_post_code_2').val( $('#delivery_post_code').val() );
    }

    function send_message() {
        if ( $('#phone_number').val() == '' && $('#email_address').val() == '' ) {
            alert( "Please input phone number or email address" );
            return;
        }

        var sms_sent = false;
        var email_sent = false;

        $('#sms_email_send_status').html( '' );
        switch_tab( 'temp_block' );

        if ( $('#phone_number').val() != '' ) {
            $.post( 'return.php', 
                    { phone: $('#phone_number').val(),
                        collection_post_code: $('#collection_post_code').val(),
                        delivery_post_code: $('#delivery_post_code').val(),
                        next_post_code_1: $('#next_post_code_1').val(),
                        next_post_code_2: $('#next_post_code_2').val(),
                        next_post_code_3: $('#next_post_code_3').val(),
                        total_distance: $('#total_distance').html(),
                        total_cost: $('#total_cost').html() }, 
                    function(data) {
                        sms_sent = true;
                        result = $.evalJSON( data );
                        if ( result.success == 1 ) {
                            $('#sms_email_send_status').prepend( 'SMS has been sent!<br />' );
                        } else {
                            $('#sms_email_send_status').prepend( '<span style="color: red;">' + result.message + '</span><br />' );
                        }

                        if ( $('#email_address').val() != '' ) {
                            if ( email_sent ) {
                                show_sms_email_send_status();
                            }
                        } else {
                            show_sms_email_send_status();
                        }

                    } );        
        }

        if ( $('#email_address').val() != '' ) {
            $.post( 'return.php', 
                    { email: $('#email_address').val(),
                        phone: $('#phone_number').val(),
                        collection_post_code: $('#collection_post_code').val(),
                        delivery_post_code: $('#delivery_post_code').val(),
                        next_post_code_1: $('#next_post_code_1').val(),
                        next_post_code_2: $('#next_post_code_2').val(),
                        next_post_code_3: $('#next_post_code_3').val(),                 
                        total_distance: $('#total_distance').html(),
                        total_cost: $('#total_cost').html() 
                                            }, 
                    function(data) {
                        email_sent = true;
                        result = $.evalJSON( data );
                        if ( result.success == 1 ) {
                            $('#sms_email_send_status').prepend( 'Email has been sent!<br />' );
                        } else {
                            $('#sms_email_send_status').prepend( '<span style="color: red;">' + result.message + '</span><br />' );
                        }

                        if ( $('#phone_number').val() != '' ) {
                            if ( sms_sent ) {
                                show_sms_email_send_status();
                            }
                        } else {
                            show_sms_email_send_status();
                        }
                    } );
        }
    }

    function show_sms_email_send_status() {
        $('#sms_email_send_status').append( '<br /><input type="button" value="Start Again" onclick="show_view_form( true );" />' );
        switch_tab( 'sms_email_send_status' );  
    }

    function onEnter(event) {
        if ( event.keyCode == 13 ) {
            estimate();
        }
    }

    $( document ).ready(function() {
        $('#container').hide();
         $('#collection_post_code').keyup(function () {
             $('#delivery_post_code').val($(this).val());
              });  });

编辑&gt;&gt;

我刚看了谷歌API v2网站,它说:

警告:Google Maps JavaScript API的第2版不再可用。请按照指南将您的代码迁移到Google Maps JavaScript API的第3版。截至2013年11月19日,所有请求v2的应用程序都将提供v3 API的特殊包装版本。我们希望API的这个包装版本适用于大多数简单的地图,但我们强烈建议您立即迁移到v3 API。

我现在正在摸不着头脑,因为我不确定如何将它转换为API v3

0 个答案:

没有答案