JSON 从远程响应中检索数据

JSON Data retrieval from Remote Response

我正在做一个项目,我需要在 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"
        }
]

现在这是无法工作的远程 URL 代码,我收到了响应,但是当我尝试 return 数据时,它无法 return 预期数据设置如上例,无法在 Google 地图中显示标记。

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 数据和 return 数据时让我知道上面代码的问题,那就太好了。

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

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

function onReceiveData(data) {
 // you will get your data here.
}

// your code.
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]));
                onReceiveData(data.ds_petAlert[1]); 
           });  
},

回调功能对我不起作用。但是找到了解决办法。我没有调用 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);
                }
            });
        },