我正在运行一个for循环来遍历所有国家/地区并使用来自世界银行API的GDP标记。
我能够运行for循环并从Google Maps Geocoding获取所有坐标,但我不能将带有GDP值的文本放在InfoWindows中。
按下任何标记时,只显示津巴布韦。这是为什么?我该如何解决?非常感谢。
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(25, 25),
});
var url = 'http://api.worldbank.org/countries/all/indicators/NY.GDP.MKTP.KD.ZG?date=2015&per_page=264&format=jsonp&prefix=Getdata';
var query_url = url;
var script = document.createElement('script');
script.src = query_url;
document.getElementsByTagName('head')[0].appendChild(script);
}
window.Getdata = function(data) {
var country_gdp = data[1].map(function(item) {
return {
country: item.country.value,
value: item.value,
}
});
for (i = 0; i < country_gdp.length; i++) {
GDP_country_growth = country_gdp[i];
var xhttp = new XMLHttpRequest();
xhttp.addEventListener('load', processResponse);
xhttp.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + GDP_country_growth.country + '&key=YOURAPIKEYHERE');
xhttp.send();
var processResponse = function() {
var coordinates = JSON.parse(this.response);
console.log(coordinates);
var latLng = new google.maps.LatLng(coordinates.results[0].geometry.location.lat, coordinates.results[0].geometry.location.lng);
var contentString = 'The GDP of ' + GDP_country_growth.country + ' is ' + GDP_country_growth.value;
var infowindow = new google.maps.InfoWindow({
content: contentString,
});
var marker = new google.maps.Marker({
position: latLng,
map: map,
contentString: contentString
});
marker.addListener('click', function() {
infowindow.setContent(this.contentString);
infowindow.open(map, this);
map.setCenter(this.getPosition());
});
}
}
}
答案 0 :(得分:0)
要解决infowindow数据与地理编码位置的关联,一个选项是使用函数闭包(下面的函数在GDP_country_growth上得到闭包,这是用于地理编码请求的数组元素):
function processCountry(GDP_country_growth) {
var xhttp = new XMLHttpRequest();
var processResponse = function() {
var coordinates = JSON.parse(this.response);
console.log(coordinates);
var latLng = new google.maps.LatLng(coordinates.results[0].geometry.location.lat, coordinates.results[0].geometry.location.lng);
var contentString = 'The GDP of ' + GDP_country_growth.country + ' is ' + GDP_country_growth.value;
var infowindow = new google.maps.InfoWindow({
content: contentString,
});
var marker = new google.maps.Marker({
position: latLng,
map: map,
contentString: contentString
});
marker.addListener('click', function() {
infowindow.setContent(this.contentString);
infowindow.open(map, this);
map.setCenter(this.getPosition());
});
}
xhttp.addEventListener('load', processResponse);
xhttp.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + GDP_country_growth.country);
xhttp.send();
}
代码段
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(25, 25),
});
var url = 'http://api.worldbank.org/countries/all/indicators/NY.GDP.MKTP.KD.ZG?date=2015&per_page=264&format=jsonp&prefix=Getdata';
var query_url = url;
var script = document.createElement('script');
script.src = query_url;
document.getElementsByTagName('head')[0].appendChild(script);
}
window.Getdata = function(data) {
var country_gdp = data[1].map(function(item) {
return {
country: item.country.value,
value: item.value,
}
});
for (i = 0; i < country_gdp.length; i++) {
(function(country_info) {
setTimeout(function() {
processCountry(country_info);
}, 1000 * i);
})(country_gdp[i])
}
}
google.maps.event.addDomListener(window, "load", initMap);
function processCountry(GDP_country_growth) {
var xhttp = new XMLHttpRequest();
var processResponse = function() {
var coordinates = JSON.parse(this.response);
console.log(coordinates);
var latLng = new google.maps.LatLng(coordinates.results[0].geometry.location.lat, coordinates.results[0].geometry.location.lng);
var contentString = 'The GDP of ' + GDP_country_growth.country + ' is ' + GDP_country_growth.value;
var infowindow = new google.maps.InfoWindow({
content: contentString,
});
var marker = new google.maps.Marker({
position: latLng,
map: map,
contentString: contentString
});
marker.addListener('click', function() {
infowindow.setContent(this.contentString);
infowindow.open(map, this);
map.setCenter(this.getPosition());
});
}
xhttp.addEventListener('load', processResponse);
xhttp.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + GDP_country_growth.country);
xhttp.send();
}
&#13;
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
&#13;