【问题标题】:Adding Hyperlinks to Infowindow using GMaps使用 GMaps 将超链接添加到 Infowindow
【发布时间】:2012-03-31 23:07:51
【问题描述】:

我正在使用 Wordpress,并且成功地在我的网站上使用 GMaps 显示了带有多个标记的地图。不幸的是,我希望将“位置名称”超链接到我网站上的相关帖子。但由于某种原因,我无法在不破坏整个地图的情况下向信息窗口添加超链接。

下面是功能代码,一旦我删除了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>

【问题讨论】:

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


    【解决方案1】:

    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>
    

    【讨论】:

    • 感谢您的帮助@Dr.Molle。这就像一个魅力!您在变量中切换了经度和纬度,这可能是我做过的最简单的修复。
    • 哦,对不起,上面已经修复了
    猜你喜欢
    • 2013-05-26
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多