使用错误的方法在地图上显示来自 JSON 的信息?

Displaying information from a JSON on map using wrong method?

我正在通过一个项目在地图上显示来自第三方 api 的信息。

我不太了解控制台告诉我的内容,所以我对遇到的错误有点困惑。我已经在函数外部声明了所有变量,因此定义了 result 并且所有这些函数都是 foursquare 场所的各个方面。

未捕获的类型错误:无法读取未定义的 属性 'hasOwnProperty'

$.ajax({
        url:'https://api.foursquare.com/v2/venues/search',
        dataType: 'json',
        data: 'limit=1' +
                '&ll='+ placeItem.lat() +','+ placeItem.lng() +
                '&client_id='+ CLIENT_ID +
                '&client_secret='+ CLIENT_SECRET +
                '&v=20140806' +
                '&m=foursquare',
        async: true,

        success: function (data) {
            result = data.response.venue;
            contact = result.hasOwnProperty('contact') ? result.contact : '';
            if (contact.hasOwnProperty('formattedPhone')) {
                placeItem.phone(contact.formattedPhone || '');
            }

            location = result.hasOwnProperty('location') ? result.location : '';
            if (location.hasOwnProperty('address')) {
                placeItem.address(location.address || '');
            }

            bestPhoto = result.hasOwnProperty('bestPhoto') ? result.bestPhoto : '';
            if (bestPhoto.hasOwnProperty('prefix')) {
                placeItem.photoPrefix(bestPhoto.prefix || '');
            }
            if (bestPhoto.hasOwnProperty('suffix')) {
                placeItem.photoSuffix(bestPhoto.suffix || '');
            }

            description = result.hasOwnProperty('description') ? result.description : '';
            placeItem.description(description || '');

            rating = result.hasOwnProperty('rating') ? result.rating : '';
            placeItem.rating(rating || 'none');

            url = result.hasOwnProperty('url') ? result.url : '';
            placeItem.url(url || '');
            placeItem.canonicalUrl(result.canonicalUrl);

            // Infowindow code is in the success function so that the error message

            // Content of the infowindow
            contentString = '<div id="iWindow"><h4>' + placeItem.name() + '</h4><div id="pic"><img src="' +
                    placeItem.photoPrefix() + '110x110' + placeItem.photoSuffix() +
                    '" alt="Image Location"></div><p>Information from Foursquare:</p><p>' +
                    placeItem.phone() + '</p><p>' + placeItem.address() + '</p><p>' +
                    placeItem.description() + '</p><p>Rating: ' + placeItem.rating() +
                    '</p><p><a href=' + placeItem.url() + '>' + placeItem.url() +
                    '</a></p><p><a target="_blank" href=' + placeItem.canonicalUrl() +
                    '>Foursquare Page</a></p><p><a target="_blank" href=https://www.google.com/maps/dir/Current+Location/' +
                    placeItem.lat() + ',' + placeItem.lng() + '>Directions</a></p></div>';

            // Add infowindows
                google.maps.event.addListener(placeItem.marker, 'click', function () {
                infowindow.open(map, this);
                // Bounce animation
                placeItem.marker.setAnimation(google.maps.Animation.BOUNCE);
                setTimeout(function () {
                    placeItem.marker.setAnimation(null);
                }, 800);
                infowindow.setContent(contentString);
            });
    },

        // Alert the user on error.
        error: function (e) {
            infowindow.setContent('<h5>Foursquare data is unavailable.</h5>');
            document.getElementById("error").innerHTML = "<h4>Foursquare data is unavailable. Please try refreshing.</h4>";
    }
    });

不确定您正在调用的数据应该是什么return,但是您得到的错误表明您没有得到您期望的数据。

结果 = data.response.venue;

"venue" 不是 data.response 的已定义 属性,因此当您尝试访问 result.hasOwnProperty('contact') 时,它不知道要做什么 return 因为未定义结果。