谷歌地图标记加载最后一条记录
我正在尝试向谷歌地图添加一组标记,每个标记都有一个信息窗口。我使用 php 和 javascript 的混合来执行此操作,php 从数据库获取所有记录,然后 javascript 使用从数据库获取的坐标将所有标记添加到地图上。
这是我到目前为止所得到的:
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(<?=$lat?>, <?=$long?>);
var settings = {
zoom: 12,
center: latlng,
mapTypeControl: false,
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var postcodeLogo = new google.maps.MarkerImage('/images/postcode_marker.png',
new google.maps.Size(32,37),
new google.maps.Point(0,0),
new google.maps.Point(15,34)
);
var propertyLogo = new google.maps.MarkerImage('/images/property_marker.png',
new google.maps.Size(32,37),
new google.maps.Point(0,0),
new google.maps.Point(15,34)
);
var infowindow = new google.maps.InfoWindow();
<?
foreach ($results as $grid_refs)
{
?>
var propPos = new google.maps.LatLng(<?=$grid_refs['grid_ref']?>);
propMarker = new google.maps.Marker({
position: propPos,
map: map,
icon: propertyLogo,
title:"<?=$grid_refs['sd_name']?>",
zIndex: 3});
var contentString = '<div style="font-weight:bold;"><?=$grid_refs['sd_name']?></div><div><img src="www.imagelink.com/<?=$grid_refs['image_filename']?>"></div><div><a href="http://www.pagelink-<?=$grid_refs['property_id'];?>/" target="-blank">See full details</a></div>';
google.maps.event.addListener(propMarker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,this);
});
<?
}
?>
var positionPos = new google.maps.LatLng(<?=$lat?>, <?=$long?>);
var positionMarker = new google.maps.Marker({
position: positionPos,
map: map,
icon: postcodeLogo,
title:"<?=$_GET['searchpostcode']?>",
zIndex: 3});
}
</script>
positionMarker 是用户输入他们想要搜索的邮政编码的地方。 propMarker 标记了地图上随后显示的所有地点。
在代码中显示了所有信息,但是在地图上,每个信息窗口都会打开最后检索到的记录 - 我希望它加载与该标记相关的信息。
我看过这个问题:
Google 地图 infoWindow 仅加载最后一条记录在标记上,
这将起作用,但我似乎无法获得与 php foreach 一起使用的 js for 循环,我已经在 php foreach 内部和外部尝试过它。
我认为我需要的是 forearch 循环,以便在每次创建标记时更改变量和 contentString 的名称,但我似乎可以让它工作?
非常感谢任何帮助!
I am trying to add a group of markers to google maps each with a infomation window. I am using a mix of php and javascript to do this, the php gets all the records from the database and then the javascript adds all the markers to the map with the coordinates taken from the database.
Here is what I have so far:
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(<?=$lat?>, <?=$long?>);
var settings = {
zoom: 12,
center: latlng,
mapTypeControl: false,
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var postcodeLogo = new google.maps.MarkerImage('/images/postcode_marker.png',
new google.maps.Size(32,37),
new google.maps.Point(0,0),
new google.maps.Point(15,34)
);
var propertyLogo = new google.maps.MarkerImage('/images/property_marker.png',
new google.maps.Size(32,37),
new google.maps.Point(0,0),
new google.maps.Point(15,34)
);
var infowindow = new google.maps.InfoWindow();
<?
foreach ($results as $grid_refs)
{
?>
var propPos = new google.maps.LatLng(<?=$grid_refs['grid_ref']?>);
propMarker = new google.maps.Marker({
position: propPos,
map: map,
icon: propertyLogo,
title:"<?=$grid_refs['sd_name']?>",
zIndex: 3});
var contentString = '<div style="font-weight:bold;"><?=$grid_refs['sd_name']?></div><div><img src="www.imagelink.com/<?=$grid_refs['image_filename']?>"></div><div><a href="http://www.pagelink-<?=$grid_refs['property_id'];?>/" target="-blank">See full details</a></div>';
google.maps.event.addListener(propMarker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,this);
});
<?
}
?>
var positionPos = new google.maps.LatLng(<?=$lat?>, <?=$long?>);
var positionMarker = new google.maps.Marker({
position: positionPos,
map: map,
icon: postcodeLogo,
title:"<?=$_GET['searchpostcode']?>",
zIndex: 3});
}
</script>
The positionMarker is where the user enters the postcode they want to search by. The propMarker marks all of the places on the map which then show.
In the code all the information is showing, however on the map each info window opens with the last retrieved record - where I want it to load the information relevant to that marker.
I have looked at this question:
Google Maps infoWindow only loading last record on markers
which will work, but I cant seem to get a js for loop working with the php foreach, I have tried it within the php foreach and outside of.
I THINK what I need is the forearch loop to change the name of the variable and contentString each time it creates a marker, but I can seem to get this working?
Any help much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的情况是,您循环所有标记,但在最后一次迭代时,您设置到 contentString 中的内容将成为随后用于所有单击事件的内容。
相反,将其委托给另一个函数(或使用闭包),例如
然后添加一个新函数:
What happens is that you loop round all the markers, but at the very last iteration, that content that you've set into contentString becomes what's then used for all the click events.
Instead, delegate this off to another function (or use a closure), e.g.
Then add a new function: