使用GMaps将超链接添加到Infowindow

时间:2012-03-15 12:34:19

标签: wordpress google-maps google-maps-markers infowindow

我正在使用Wordpress,并成功使用GMaps在我的网站上显示多个标记的地图。不幸的是,我希望将“位置名称”链接到我网站上的相关帖子。出于某种原因,我无法在不崩溃整个地图的情况下向infowindow添加超链接。

以下是正常运行的代码,只要我删除地图不再显示的strip_tags();功能。我猜它与太多"有关,但我无法弄清楚如何让它发挥作用。

    $str = '';
    foreach($loop as $p){
    //get the meta and taxonomy data
    $name = strip_tags(get_the_term_list($p, "mountains"));
    $wtr_long = get_post_meta($p,"wtr_longitude",true);
    $wtr_lat = get_post_meta($p,"wtr_latitude",true);

    //Add to Array
    $map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "' . $name .'"},~!~';
    //$map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "name"},~!~';
    }

    //Remove last three "~!~" characters from the string not to have one too many arrays when exploding
    $clean_map_string = substr($map_string, 0, -3); 

    //Now I can use explode on ~!~ to get arrays with my string
    $map_array = explode('~!~', $clean_map_string);
    ?>                        




    <!--Map implementation-->
<div id="map" style="width:880px; height: 600px; background-color:grey;"></div>

<script type="text/JavaScript">                                                     
      $("#map").gMap({
                      scrollwheel: false,
                      maptype: G_PHYSICAL_MAP,
                      markers: [
<?php
    $i = 0;
    $length = count($map_array)-1;

//Inserts all the markers
    foreach($map_array as $value){
        if( $i != $length ){
            echo $value;
                            }
        else {
            echo str_replace("},", "}],", $value);
            }
            $i++;
                                }   
    ?>

                      popup: false,
                      zoom: 2 });             
   </script>

1 个答案:

答案 0 :(得分:0)

InfoWindows通常没有标签问题。您正在尝试在那里创建一个JSON字符串,使用json_encode()而不是这种字符串函数的混合来避免字符串中的无效字符。

<?php
    $markers=array();
    foreach($loop as $p){

      $markers[]=array(
                        'latitude'  =>  get_post_meta($p,"wtr_latitude",true),
                        'longitude' =>  get_post_meta($p,"wtr_longitude",true),
                        'html'      =>  get_the_term_list($p, "mountains")
                      ); 
    } 

?> 
<!--Map implementation-->
<div id="map" style="width:880px; height: 600px; background-color:grey;"></div>

<script type="text/JavaScript">                                                     
      $("#map").gMap({
                      scrollwheel: false,
                      maptype: G_PHYSICAL_MAP,
                      markers: <?php echo json_encode($markers);?>,
                      popup: false,
                      zoom: 2 });             
</script>