我在数据库中通过lat和long在地图上显示标记。我现在想要显示在信息窗口中的其他行,与用户lat和long相对应。例如。他们的名字,身份证。我该怎么做呢?
var infowindow = new google.maps.InfoWindow({
content: " .$member" + ".$memberID" (<<< something like that)
marker = new google.maps.Marker({
position: new google.maps.LatLng(dis.latitude,dis.longitude),
map: maps
}))
infowindow.open(window.googleMap,marker);
答案 0 :(得分:0)
看起来你有正确的想法。为此,您需要将数据库中的值存储在某些变量中。与您在数据库中使用lat和long所做的类似。此外,通常建议首先创建一个字符串,其中包含您想要进入infowindow的所有内容。让我们假设您有一个名为member和ID的变量,它们保存您在infowindow中所需的数据库中的值。你可以这样做:
var contentString = 'some content' + member + 'other content' + ID;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
marker = new google.maps.Marker({
position: new google.maps.LatLng(dis.latitude,dis.longitude),
map: map
}));
marker.addListener('click', function() {
infowindow.open(map, marker);
});
这将使您在单击标记时显示信息窗口。如果您希望在将标记放入地图的同时显示它,则不需要将标记添加到标记中,您只需调用:
infowindow.open(map.marker)
创建标记后直接。我在这里用JSFiddle演示了这个,我已经注释掉了为标记添加一个监听器的代码,以便立即显示信息窗口。请注意,您需要插入自己的API密钥才能生效。请看这里的小提琴:
https://jsfiddle.net/c01xxg29/
这里还有关于infowindows的好文档:
https://developers.google.com/maps/documentation/javascript/infowindows#introduction
我希望这有帮助!