我正尝试从API获取数据并将其存储在地图标记中。我确实将调试器发现正在获取数据并将其存储在$ .getJSON内的变量,并且当我尝试在其外部访问它时返回的未定义<。 / p>
这是代码的一部分
var Location = function(data) {
var self = this;
this.name = data.name;
this.lat = data.lat;
this.long = data.long;
this.street ;
this.city ;
this.visible = ko.observable(true);
var foursquareURL = 'https://api.foursquare.com/v2/venues/search?ll='+ this.lat + ',' + this.long + '&client_id=' + clientID + '&client_secret=' + clientSecret + '&v=20180909' + '&query=' + this.name;
$.getJSON(foursquareURL).done (function(data) {
var results = data.response.venues[0];
this.street = results.location.formattedAddress[0];
this.city = results.location.formattedAddress[1];
}).fail(function() {
alert("There was an error with the Foursquare API call. Please refresh the page and try again to load Foursquare data.");
});
this.contentString = '<div class="info-window-content"><div class="title"><b>' + data.name + "</b></div>" +
'<div class="content">' + this.street + "</div>" +
'<div class="content">' + this.city + "</div>";
addMarkerToGroup(group, {lat:data.lat, lng:data.long},self.contentString);
// addInfoBubble(map);
};
function addMarkerToGroup(group, coordinate, html) {
var marker = new H.map.Marker(coordinate);
// add custom data to the marker
marker.setData(html);
group.addObject(marker);
}
我需要的是this.street和this.city。,但是它会不断获取每个查询的数据,然后返回未定义的数据。