Javascript 使用wp_查询查找点时,谷歌地图上没有出现标记
我使用这个插件允许用户在网站上选择他们最喜欢的旅游。使用to Cookie将帖子保存到插件提供的保存模板中。我已经编辑了这个模板,以包含一个地图,并从自定义元字段中提取坐标 完整模板可在上找到 我包含的显示地图的代码是:Javascript 使用wp_查询查找点时,谷歌地图上没有出现标记,javascript,wordpress,google-maps,google-maps-api-3,Javascript,Wordpress,Google Maps,Google Maps Api 3,我使用这个插件允许用户在网站上选择他们最喜欢的旅游。使用to Cookie将帖子保存到插件提供的保存模板中。我已经编辑了这个模板,以包含一个地图,并从自定义元字段中提取坐标 完整模板可在上找到 我包含的显示地图的代码是: <div id="map" style="width: 100%; height: 250px; position: relative; overflow: hidden; -webkit-transform: translateZ(0px); background-co
<div id="map" style="width: 100%; height: 250px; position: relative; overflow: hidden; -webkit-transform: translateZ(0px); background-color: rgb(229, 227, 223);"></div>
我在循环中使用的代码是:
while ( $loop->have_posts() ) : $loop->the_post();
if ( get_post_meta($post->ID, 'custom_latlng', true) !== '' ) : ?>
<div style="display:block;">
<script type="text/javascript">
function initialize() {
//load map
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: new google.maps.LatLng(53.3498, -6.2603),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
var savedMarkers = new Array();
<?php $saved_pos = get_post_meta($post->ID, 'custom_latlng', true);?>
function addMarker() {
var savedMarker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(<?php echo $saved_pos ?>),
icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
});
savedMarkers.push(savedMarker);
}
}
</script>
</div>
while($loop->have_posts()):$loop->the_post();
如果(获取帖子元($post->ID,'custom\u latlng',true)!='':?>
函数初始化(){
//负荷图
map=new google.maps.map(document.getElementById('map'),{
缩放:9,
中心:新google.maps.LatLng(53.3498,-6.2603),
mapTypeId:google.maps.mapTypeId.ROADMAP,
disableDefaultUI:true
});
var savedMarkers=新数组();
函数addMarker(){
var savedMarker=new google.maps.Marker({
地图:地图,
位置:新建google.maps.LatLng(),
图标:'/wp content/themes/dublin访问者中心/images/saved_icon.png',
});
savedMarker.push(savedMarker);
}
}
此时,当我查看震源时,我可以看到正在选择的点,坐标确实出现了。但是,它们不会出现在地图上。这就好像这些点出现在保存的帖子列表中,但根本不在地图上
我希望这是有道理的
循环内的欢呼声仅用纬度/经度填充数组,在循环外创建
初始化:
<div id="map" style="width: 100%; height: 250px;"></div>
<script type="text/javascript">
function initialize() {
//load map
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: new google.maps.LatLng(53.3498, -6.2603),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
//create the markers
for(var i=0;i<savedMarkers.length;++i){
savedMarkers[i] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(savedMarkers[i][0],
savedMarkers[i][1]),
icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
});
}
}
<?php
//create a php-array to store the latitudes and longitudes
$savedMarkers=array();
//use the loop to populate the array
while ( $loop->have_posts() ) : $loop->the_post();
if ( get_post_meta($post->ID, 'custom_latlng', true) !== '' ) :
$savedMarkers[]=explode(',',get_post_meta($post->ID, 'custom_latlng', true));
endif;
endwhile;
?>
//print the array as js-variable
savedMarkers= <?php echo json_encode($savedMarkers);?>;
</script>
创建标记时,请为存储infowindow内容的标记创建自定义属性(我们称该属性为content
):
您还可以在信息窗口中创建帖子链接:
$savedMarkers[]=array_merge(explode(',',get_post_meta($post->ID, 'custom_latlng', true)),
array(get_the_title(),get_permalink()));
//create the markers
for(var i=0;i<savedMarkers.length;++i){
savedMarkers[i] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(savedMarkers[i][0],
savedMarkers[i][1]),
icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
//the content(post-title)
title: '' + savedMarkers[i][2],
//post-URL
href: savedMarkers[i][3]
});
}
你能在DOM中显示JS输出吗?简单更新一下,我删除了函数addMarker()
,并得到了最后一个要显示的标记。如果您转到,保存到RHS上的收藏夹,然后保存到收藏夹,最后保存到,您将只看到一个项目,这应该是您最后保存的项目。请粘贴PHP脚本在您的问题中输出的JS。这是用于其中一个标记:。该代码没有任何错误。你是在循环中做的吗?我不能从这段代码中分辨出来。。。你是在复制整个代码吗?这似乎是为了我!现在我需要做的就是添加信息框,但我应该可以接受。非常感谢你在这方面的帮助!嘿@Dr.Molle,我想知道你能不能再快速看一下这个,看看我在信息箱中遗漏了什么?我现在打算把这些标记的标题添加为信息框。我没有js错误。我复制了您所做的操作,只是为了一个名为savedTitles的新变量,但信息框不会加载。完整的代码可以在这里找到:目前,我正在尝试打印Infobox中的任何内容,并将在Infobox工作后移到标题上。是否要在Infobox中打印完整的文章内容?文章的标题就可以了。这样,用户可以通过单击每个标记来判断它实际上与什么点相关。你有什么想法吗?嘿@Dr.Molle。这一切对我来说非常有效!再次感谢你的帮助!
google.maps.event.addListener(savedMarkers[i], 'click', function() {
infowindow.setContent(this.get('content'));
infowindow.open(this.getMap(), this);
}
);
$savedMarkers[]=array_merge(explode(',',get_post_meta($post->ID, 'custom_latlng', true)),
array(get_the_title(),get_permalink()));
//create the markers
for(var i=0;i<savedMarkers.length;++i){
savedMarkers[i] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(savedMarkers[i][0],
savedMarkers[i][1]),
icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
//the content(post-title)
title: '' + savedMarkers[i][2],
//post-URL
href: savedMarkers[i][3]
});
}
google.maps.event.addListener(savedMarkers[i], 'click', function() {
var link=document.createElement('a');
link.appendChild(document.createTextNode(this.get('title')));
link.setAttribute('href',this.get('href'));
infowindow.setContent(link);
infowindow.open(this.getMap(), this);
}
);