0

我有一个起点和终点的纬度和经度,例如跟随。

1->Start:23.03234692854866,72.45964050292969 End:23.02823945776033,72.52281188964844 2-> Start:23.02823945776033,72.52281188964844 End:23.037402105360147,72.58907318115234 3->Start:23.037402105360147,72.58907318115234 End:23.036770218630025,72.6309585571289

这里,第一个的结束值是第二个的开始值(见上限值的粗体部分)。

问题是当我尝试插入它时,它没有按我的意愿插入。它是这样插入的:

在此处输入图像描述

意味着,这里有很多事情发生了变化。就像最后插入的第一个数据一样。
那么问题是什么?

JAVASCRIPT CODE:

function save_waypoints()
{var ac;
var ad;
var k="1";
    for (var m=0;m<tot_marker;m++)
    {   
        ac +=k+"->Start:"+lat_arr[m].location+","+lon_arr[m].location+"&nbsp;&nbsp;&nbsp;End:"+lat_arr[m+1].location+","+lon_arr[m+1].location+","+k+"====<br>";
        document.getElementById('e').innerHTML=ac;

        var w=[],wp;
        var rleg = directionsDisplay.directions.routes[0].legs[m];
        /*data.start = {'lat': rleg.start_location.lat(), 'lng':rleg.start_location.lng()}
        data.end = {'lat': rleg.end_location.lat(), 'lng':rleg.end_location.lng()}*/
        data.start = {'lat': lat_arr[m].location, 'lng':lon_arr[m].location}
        data.end = {'lat': lat_arr[m+1].location, 'lng':lon_arr[m+1].location}
        var wp = rleg.via_waypoints
        for(var i=0;i<wp.length;i++)w[i] = [wp[i].lat(),wp[i].lng()]
        data.waypoints = w;
        var str = JSON.stringify(data)

        var jax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
        jax.open('POST','process.php');
        jax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        jax.send('command=save&mapdata='+str)
        jax.onreadystatechange = function(){ if(jax.readyState==4) {
            if(jax.responseText.indexOf('bien')+1)alert('Updated');
            else alert(jax.responseText)
        }}
        k++;
    }
}

PHP

<? ob_start(); header('Cache-Control: no-store, no-cache, must-revalidate');

    mysql_connect('localhost','cron','1234');
    mysql_select_db('mapdir');

    if($_REQUEST['command']=='save')
    {
        $data = $_REQUEST['mapdata'];
        //$query = "update mapdir set value='$data'";
        $query = "insert into `mapdir`(`value`) values('$data')";
        if(mysql_query($query))die('bien');
        die(mysql_error());
    }

    if($_REQUEST['command']=='fetch')
    {
        $query = "select value from mapdir";
        /*if(!($res = mysql_query($query)))die(mysql_error());      
        $rs = mysql_fetch_array($res,1);
        die($rs['value']);*/
        $qry=mysql_query($query);
        /*$rs = mysql_fetch_array($qry,1);
        echo $rs['value'];*/
        $ja = array();
        while($rs = mysql_fetch_array($qry))
        {
            array_push($ja,$rs['value']);       
        }       
        echo json_encode($ja);
    }
?>

I am aware of deprecated function.

4

2 回答 2

0

从我看到你有一个数组溢出

   data.start = {'lat': lat_arr[m].location, 'lng':lon_arr[m].location}
    data.end = {'lat': lat_arr[m+1].location, 'lng':lon_arr[m+1].location}

这会导致这样的问题......还是我错了?

于 2013-10-24T12:13:05.617 回答
0

我试图遵循您的代码并且遇到了一些困难,所以这是一个替代解决方案(如果您愿意):

脚本:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

// Function
function save_waypoints(from, to)
{
    // Call Google Maps Directions API
    $.get('http://maps.googleapis.com/maps/api/directions/json?origin='+ from +'&destination='+ to +'&sensor=false', function(result)
    {
        // Check Status
        if (result.status == "OK")
        {
            // Parse Steps
            for (var i = 0; i < result.routes[0].legs[0].steps.length; i++)
            {
                // Load Way Point
                var waypoint = result.routes[0].legs[0].steps[i];
                var waypoint_str = JSON.stringify({
                    'start': waypoint.start_location,
                    'end': waypoint.end_location
                });

                // Save Way Point
                $.ajax({
                    type: "POST",
                    url: 'process.php?command=save',
                    data: { 'mapdata': waypoint_str },
                    success: function() {
                        alert('Successfully saved')
                    },
                    error: function() {
                        alert('Failed to save');
                    }
                });
            }
        }
        else
        {
            // Error
            alert('Failed to load waypoints');
        }
    });
}

// Usage
save_waypoints('Toronto', 'Montreal');

</script>

每个waypoint_str看起来像这样:

{"start":{"lat":43.6533103,"lng":-79.3827675},"end":{"lat":43.6557259,"lng":-79.3837332}}

API 文档:https ://developers.google.com/maps/documentation/directions/

于 2013-10-24T13:52:40.527 回答