本文介绍了Google Maps API信息窗口仅显示数组中的最后一条语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码.添加标记效果很好,但标记的正确信息窗口却不行.(标记中的标题也是如此)
I have this code. Adding Markers work great but right infowindow to markers don't.. (title in marker too)
对于所有标记,它仅从数组infoUser返回最后一条语句.
It's returning only last statements from array infoUser for all markers.
我知道..我正在复制一个问题,但是我尝试使用很多已回答的问题来解决它,但这是行不通的.
I know.. I'm duplicating a question but I tried fix it with a lot of answered questions but it doesn't work.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 49.7437895, lng: 15.3360726}
});
//array
var infoUser = [
['Germany','Maj','TUC','20','300'],
['Czech Republic','admin','Bla','50','400'],
['Italy','HOOOO','BUC','1','50'],
['Italy','aaaaa','aaa','10','20'],
['Germany','adminek','hopinek','50','1000']
];
var infoWindow = new google.maps.InfoWindow();
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
var markers = [];
for (var x = 0; x < infoUser.length; x++) {
var adresa = infoUser[x][0],
name = infoUser[x][1],
firstname = infoUser[x][2],
age = infoUser[x][3],
price = infoUser[x][4];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+adresa, null, function (data) {
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
var marker = new google.maps.Marker({
position: latlng,
title: adresa,
map: map
}); markers.push(marker); markerCluster.addMarker(marker);
var content = 'Adresa: ' + adresa + 'username: ' + name;
google.maps.event.addListener(marker,'click', (function(marker, content, infoWindow){
return function() {
infoWindow.setContent(content);
infoWindow.open(map,marker);
};
})(marker, content, infoWindow));
});
}
}
推荐答案
您需要对地址解析器调用(adresa
和name
)以及信息窗口content
/marker
/外的数据进行函数关闭infoWindow
:
You need function closure on the data outside the geocoder call (adresa
and name
) as well as the infowindow content
/marker
/infoWindow
:
代码段:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: 49.7437895,
lng: 15.3360726
}
});
//array
var infoUser = [
['Germany', 'Maj', 'TUC', '20', '300'],
['Czech Republic', 'admin', 'Bla', '50', '400'],
['Italy', 'HOOOO', 'BUC', '1', '50'],
['Italy', 'aaaaa', 'aaa', '10', '20'],
['Germany', 'adminek', 'hopinek', '50', '1000']
];
var infoWindow = new google.maps.InfoWindow();
var markerCluster = new MarkerClusterer(map, markers, {
maxZoom: 18,
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
var markers = [];
for (var x = 0; x < infoUser.length; x++) {
var adresa = infoUser[x][0],
name = infoUser[x][1],
firstname = infoUser[x][2],
age = infoUser[x][3],
price = infoUser[x][4];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + adresa, null, function(adresa, name) {
return function(data) {
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
var marker = new google.maps.Marker({
position: latlng,
title: adresa,
map: map
});
markers.push(marker);
markerCluster.addMarker(marker);
var content = 'Adresa: ' + adresa + '<br>username: ' + name;
google.maps.event.addListener(marker, 'click', (function(marker, content, infoWindow) {
return function() {
infoWindow.setContent(content);
infoWindow.open(map, marker);
};
})(marker, content, infoWindow));
}
}(adresa, name));
}
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/src/markerclusterer.js"></script>
<div id="map"></div>
这篇关于Google Maps API信息窗口仅显示数组中的最后一条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!