【问题标题】:sending json object to google maps vai ajax response from php script将 json 对象发送到 google maps 来自 php 脚本的 vai ajax 响应
【发布时间】:2012-11-08 17:41:01
【问题描述】:

我被难住了。我不确定为什么我的回调函数没有解析我的 JSON 对象。 我在控制台日志中收到以下错误:

TypeError:数据未定义 for (var i = 0, len = data.features.length; i

非常感谢您的帮助!

ajax 代码:

$.ajax({
        type : 'POST',
        url  : 'timezone.php',
        dataType : 'json',
        data     : { "func" : "getFirstAndNext", epochTime : browserUTC },
        success  : function( data ) {
                console.log( data.first );
                console.log( data.next );
                sector_callback( data.first );

            // do something with data.myObject.memberVariable

        },
        error : function ( XMLHttpRequest, textStatus, errorThrown ) {
            // didn't work!
        }
    });

PHP 脚本:

    <?php
    //header('Content-Type: application/json; charset=utf-8');
    function getFileStamp($type) {
        $timeInterval = 15; // minutes
        $now = round($_POST['epochTime'] / 1000);
        $now = round(1352324181061 /1000 ); // 3:36:21
        //$now = round(1352238011.067);
        $offsetInSeconds = $timeInterval * 60;
        $offsetInMillis  = $timeInterval * 60 * 1000;

        $currentFileTime = $now - $now % ($timeInterval * 60);
        //$currentFileTime = ($now - ($now  % $offsetInMillis))/1000.0;  // this returns epoch time in $timeInterval minutes.
        $nextFileTime = $currentFileTime + $offsetInSeconds;  // next epoch time for file;
        return ($type == 0) ? $currentFileTime : $nextFileTime;
    }

        $currentFileTime = getFileStamp(0) . '.json';
        $nextFileTime = getFileStamp(1) . '.json';

        $res = array();
        $res['file1'] = file_get_contents($currentFileTime);
        $res['file2'] = file_get_contents($nextFileTime);
        echo json_encode(array("first"=>$res['file1'],"next"=>$res['file1'])); //takes contents and converts it to json object
    ?>

扇区回调函数:

    var allPolygons = [];
    function sector_callback() {
        //console.log(data);
        var bounds = new google.maps.LatLngBounds();        
        for (var i = 0, len = data.features.length; i < len; i++) {
            var coords = data.features[i].geometry.coordinates[0];
            siteNames = data.features[i].properties.Name; // added for site names
            var path = [];
        for ( var j = 0, len2 = coords.length; j < len2; j++ ){ // pull out each     set of coords and create a map object
            var pt = new google.maps.LatLng(coords[j][1], coords[j][0])
            bounds.extend(pt);
            path.push(pt);

        }

        var polygons = new google.maps.Polygon({
        path: path,
            strokeColor: "#000000",
            strokeOpacity: 0.8,
            strokeWeight: 1,
            fillColor: "#000000",
            fillOpacity: 0.35,
        map: map
        });
        createClickablePoly(polygons, siteNames);

        google.maps.event.addListener(polygons, 'mouseover', function() {
        var currentPolygon = this;

        currentPolygon.setOptions({ 
            fillOpacity: 0.45,
            fillColor: "#FF0000"
            })
        });

        google.maps.event.addListener(polygons, 'mouseout', function() {
        var currentPolygon = this;
        currentPolygon.setOptions({ 
            fillOpacity: 0.35,
            fillColor: "#000000"
            })
        });

        allPolygons.push(polygons);
        }
    }

【问题讨论】:

  • data.first 定义了吗?为什么不是sector_callback(data);
  • 我正在尝试返回两个 JSON 对象。当我 console.log(data.first) 和 colsole.log(data.next) 时,我可以在控制台中看到它们

标签: php jquery google-maps-api-3


【解决方案1】:

仅显示我在这里可以看到的内容,我看不到您的扇区回调()无论如何都可以访问数据。当 jQuery 传入数据时,如果一切正常,您的控制台应该会很好地记录这些属性。但是,当您将其中一些数据传递给您的扇区回调()时,它就会丢失。原因是你的ector_callback()要么需要读取arguments[]数组来获取它,要么你需要将函数签名改为sector_callback(data)。

在您的 jQuery 块中,传递数据而不是 data.features:

sector_callback( data );

并更改您自己的回调签名:

function sector_callback(data) {
    console.log(data); // will log the whole chunk of the server data returned

 ...

}

这是因为数据在返回到您的 jQuery 回调时处于闭包中。即使您在该闭包之外的某个地方声明了它,它也会保留本地范围,并且除非您明确传递它,否则您的其他函数不能访问它。

希望我在这里理解了问题的症结,这对我有帮助...

【讨论】:

  • 这确实有道理。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2013-02-06
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多