从远程响应中检索JSON数据

时间:2017-03-19 07:46:55

标签: javascript jquery json parsing google-maps-markers

我正在开展一个项目,我需要在Google地图中获取JSON响应并显示标记。当我尝试从.json测试文件获取JSON响应时,它工作正常(代码在下面提供)。但是,当我尝试类似的代码从远程URL获取JSON响应时,它不起作用。我不知道我在哪里失踪。

使用geoLocationDataSet.json测试文件的代码。

getPetsLocations: function (lat, lng, max) {

            return $.getJSON("data/geoLocationDataSet.json") ;

           },

在geoLocationDataSet.json文件中输入数据集:

[
    {
        "type":"LOST",
        "alertID":"320",
        "petID":"11534",
        "dateTime":"03\/14\/2017",
        "petName":"Samson",
        "ownerName":"TEMP_Haywood",
        "reward":"100",
        "distance":"7",
        "latitude":"27.9506",
        "longitude":"-82.3672",
        "place":"Greater Palm River Point CDC 33619",
        "image":null
    },
    {
        "type":"LOST",
        "alertID":"328",
        "petID":"11132",
        "dateTime":"03\/14\/2017",
        "petName":"Tucker",
        "ownerName":"TEMP_Phyliss",
        "reward":"0",
        "distance":"12",
        "latitude":"27.9506",
        "longitude":"-82.2872",
        "place":"Villa Place 33510",
        "image":"https:\/\/s3.amazonaws.com\/petimage\/58c857b9c7ee8"
        }
]

现在这是远程网址的代码,它不起作用,我得到了响应,但当我尝试返回数据时,它无法返回上述示例中的预期数据集,而不是能够在谷歌地图中显示标记。

getPetsLocations: function (lat, lng, max) {
            //alert('Before call..');
            $.getJSON("http://dev.mywaggintales.com/pets/d_lostfound.php?mce=27.939224,-82.462295&mra=25&mem=chholloway@me.com&mac=1234", function(data){
                alert(JSON.stringify(data.ds_petAlert[1]));
                return data.ds_petAlert[1]; 
           });  
},

我不确定我错过的地方。如果你看一下它会很好,当我解析JSON数据并返回它时,让我知道上面代码的问题。

2 个答案:

答案 0 :(得分:0)

这是您在远程URL中传递的callback函数。您不能只返回数据,因为您实际上并未调用回调函数。

使用回调函数的一种简单方法是在外部创建一个新函数:

terms_of_service

答案 1 :(得分:0)

回拨功能对我不起作用。但找到了解决方案。我没有调用getPetsLocations函数,而是在initMap函数中添加了一个Ajax调用。代码如下。 - 谢谢。

initMap: function (position) {
            //Delcare function variables
            var myOptions,
                mapObj = _mapObj,
                mapElem = _mapElem,
                pin,
                locations = [],
                latlng;

            _mapElem = mapElem; //Cache DOM element

            // Use Google API to get the location data for the current coordinates
            //latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            latlng = new google.maps.LatLng(27.9506, -82.4572);

            myOptions = {
                zoom: 11,
                center: latlng,
                mapTypeControl: false,
                navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };


            mapObj = new google.maps.Map(mapElem, myOptions);
            _mapObj = mapObj; //Cache at app level

            pin = [
                {
                    position: latlng,
                    title: "New York"
                }
            ];

            _private.addMarkers(pin, mapObj);
            // Get stores nearby
            $.ajax({
                type: "GET",
                url: "http://dev.mywaggintales.com/pets/d_lostfound.php?mce=27.939224,-82.462295&mra=25&mem=chholloway@me.com&mac=1234",
                success: function (data) {
                    setTimeout(function () {
                        //do what you need here
                        var result1 = JSON.parse(data);
                        var result = result1.ds_petAlert[1];
                        //options.success(lostPets);

                        var len = result.length,
                            pinImage = new google.maps.MarkerImage(
                                "images/lostPet.png",
                                new google.maps.Size(49, 49),
                                new google.maps.Point(0, 202));

                        for (var i = 0; i < len; i++) {
                            locations.push({
                                title: result[i].petName + ", " + result[i].place,
                                position: new google.maps.LatLng(result[i].latitude, result[i].longitude),
                                icon: pinImage,
                                animation: google.maps.Animation.DROP
                            });
                        }
                    _private.addMarkers(locations, mapObj);
                    }, 1500);
                },
                error: function (err) {
                    alert("Error reading the data");
                    options.error(err);
                }
            });
        },