将 Google 地图与 WordPress 集成 - 信息窗口未打开

发布于 2024-11-01 10:21:05 字数 2229 浏览 0 评论 0原文

我目前正在尝试在我的 WordPress 博客的主页上合并一个地图,它将根据帖子内容和自定义字段位置在地图上显示自定义图标。我非常接近,除了信息窗口之外一切都正常。我没有收到任何错误,也没有窗口。非常感谢您的帮助。

我查看了类似的问题并尝试了一些方法,但没有任何效果。

这是我的代码:

<script type="text/javascript">

function initialize() {
// setup map
var latlng = new google.maps.LatLng(34.109739, -118.351936);
var myOptions = {
  zoom: 10,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

var infowindow = new google.maps.InfoWindow();


// icons
var bootIcon = '/images/icons/bootIcon.png';
var hikingIcon = '/images/icons/hikingIconSm.png';

// Init post Data
var i = 0;
var hikingPositions = new Array();
var hikingMarkers = new Array();
var hikingBlurbs = new Array();

<?php $hiking_query = new WP_Query('category_name=hiking_ctg&posts_per_page=4');
        while ($hiking_query->have_posts()) : $hiking_query->the_post();?>
            <?php $geoLoc = get_post_meta($post->ID, 'longlat', true); ?>

            // Set Post data
            hikingPositions[i] = new google.maps.LatLng(<?php echo $geoLoc; ?>);
            hikingMarkers[i] = new google.maps.Marker({
              position: hikingPositions[i],
              map: map,
              icon: hikingIcon,
              title:"<?php the_title(); ?>"
            });

            hikingBlurbs[i] = '<div id="content"><h1 id="firstHeading" class="firstHeading"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1><div id="bodyContent"><p>.</p></div></div>'; 

            i++;

<?php endwhile; ?>


// Assign data to map
for(var j=0, marker; j < hikingMarkers.length; j++)
 {
     // To add the marker to the map, call setMap();
     hikingMarkers[j].setMap(map);  
     marker = hikingMarkers[j]; 

     google.maps.event.addListener(marker, 'click', function() {
         infowindow.setContent(hikingBlurbs[j]);
         infowindow.open(map,this);
     });
 }
}

$(document).ready(function() {
 initialize();
});

</script>

我还有很长的路要走才能完成我正在寻找的所有功能,但对于这个构建,这是唯一不起作用的事情。

提前致谢。

问候, 混沌将军

I am currently trying to incorporate a map on the homepage of my wordpress blog which will display a custom icon on a map based on a post content and customfield location. I am very close and got everything working except the infowindow. I get no errors and no window. Your help is much appreciated.

I looked at similar questions and tried a few things but nothing worked.

Here is my code:

<script type="text/javascript">

function initialize() {
// setup map
var latlng = new google.maps.LatLng(34.109739, -118.351936);
var myOptions = {
  zoom: 10,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

var infowindow = new google.maps.InfoWindow();


// icons
var bootIcon = '/images/icons/bootIcon.png';
var hikingIcon = '/images/icons/hikingIconSm.png';

// Init post Data
var i = 0;
var hikingPositions = new Array();
var hikingMarkers = new Array();
var hikingBlurbs = new Array();

<?php $hiking_query = new WP_Query('category_name=hiking_ctg&posts_per_page=4');
        while ($hiking_query->have_posts()) : $hiking_query->the_post();?>
            <?php $geoLoc = get_post_meta($post->ID, 'longlat', true); ?>

            // Set Post data
            hikingPositions[i] = new google.maps.LatLng(<?php echo $geoLoc; ?>);
            hikingMarkers[i] = new google.maps.Marker({
              position: hikingPositions[i],
              map: map,
              icon: hikingIcon,
              title:"<?php the_title(); ?>"
            });

            hikingBlurbs[i] = '<div id="content"><h1 id="firstHeading" class="firstHeading"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1><div id="bodyContent"><p>.</p></div></div>'; 

            i++;

<?php endwhile; ?>


// Assign data to map
for(var j=0, marker; j < hikingMarkers.length; j++)
 {
     // To add the marker to the map, call setMap();
     hikingMarkers[j].setMap(map);  
     marker = hikingMarkers[j]; 

     google.maps.event.addListener(marker, 'click', function() {
         infowindow.setContent(hikingBlurbs[j]);
         infowindow.open(map,this);
     });
 }
}

$(document).ready(function() {
 initialize();
});

</script>

I still have a long way to finish all the functionality I am looking for, but for this build, this is the only thing not working.

Thanks in advance.

Regards,
GeneralChaos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-11-08 10:21:05

我确信有更优雅的解决方案,但这是有效的。

我注意到我使用的事件处理程序仅在循环结束时接收索引,因此我将标记更改为“this”并向标记对象添加“内容”值。

在标记之前定义内容数组:

hikingBlurbs[i] = '<div id="infowindowContent"><h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1><div id="bodyContent"><p>.</p></div></div>';

使用新的内容变量定义标记:

// Set Post data
hikingPositions[i] = new google.maps.LatLng(<?php echo $geoLoc; ?>);

hikingMarkers[i] = new google.maps.Marker({
   position: hikingPositions[i],
   map: map,
   icon: hikingIcon,
   title:"<?php the_title(); ?>",
   content: hikingBlurbs[i]
});

现在添加事件侦听器并进行一些简单的更改:

// Assign data to map
for(marker in hikingMarkers)
 {
  google.maps.event.addListener(hikingMarkers[marker], 'mouseover', function() {
           infowindow.setContent(this.content);
           infowindow.open(map,this);
           });
 } 

如果您发现更好的方法或有任何疑问,请告诉我。

此致,
混沌将军

I am sure there are more elegant solutions to this, but this worked.

I noticed that the event handler I was using was receiving the index only when the loop was over so I changed the marker to 'this' and added an 'content' value to the marker object.

Define the content array before the marker:

hikingBlurbs[i] = '<div id="infowindowContent"><h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1><div id="bodyContent"><p>.</p></div></div>';

Define your marker with the new content variable:

// Set Post data
hikingPositions[i] = new google.maps.LatLng(<?php echo $geoLoc; ?>);

hikingMarkers[i] = new google.maps.Marker({
   position: hikingPositions[i],
   map: map,
   icon: hikingIcon,
   title:"<?php the_title(); ?>",
   content: hikingBlurbs[i]
});

Now add the event listener with some simple changes:

// Assign data to map
for(marker in hikingMarkers)
 {
  google.maps.event.addListener(hikingMarkers[marker], 'mouseover', function() {
           infowindow.setContent(this.content);
           infowindow.open(map,this);
           });
 } 

Let me know if you see a better way of doing this or you have any questions.

Best Regards,
GeneralChaos

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文