我正在使用Wordpress中一系列帖子的元数据生成地图。地图和点生成很好但是当我点击这些点时,infowindow总是在同一个地方生成,而不是在我点击的点上生成。有人可以帮忙解决这个问题,我在v2中工作正常但是我无法弄明白它。
使用以下内容在循环中生成地图:
function initialize(){
var myLatlng = new google.maps.LatLng(45, -123);
var myOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('travel_map'), myOptions);
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$my_post = get_post($post_id);
$mapTitle = $my_post->post_title;
$lat = get_post_meta($post->ID, 'latitude', true);
$long = get_post_meta($post->ID, 'longitude', true);
?>
var contentString = 'test';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $lat . ", " . $long ?>),
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
<?php endwhile; ?>
<?php endif; ?>
}
答案 0 :(得分:2)
http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/
此处介绍如何在页面上使用多个标记和多个infowindows。
这是实施:
<script type="text/javascript">
var infowindow = null;
var markers = [];
function initialize(){
var myLatlng = new google.maps.LatLng(45, -123);
var myOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('travel_map'), myOptions);
infowindow = new google.maps.InfoWindow({
content: "holding..."
});
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$my_post = get_post($post_id);
$mapTitle = $my_post->post_title;
$lat = get_post_meta($post->ID, 'latitude', true);
$long = get_post_meta($post->ID, 'longitude', true);
?>
var marker = new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $lat . ", " . $long ?>),
map: map,
html: "<b><?php echo $mapTitle; ?></b><br><br><?php echo $my_post; ?>"
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(this.html);
infowindow.open(map, this);
});
<?php endwhile; ?>
<?php endif; ?>
}
</script>