0

我正在使用 Smarty,我想使用谷歌地图,并在其上动态放置标记,我得到了以下 cod

{literal}

 <script type="text/javascript">
    //<![CDATA[

    if (GBrowserIsCompatible()) {
      // ==== first part of the select box ===
      var select_html = '<select onChange="handleSelected(this)">' +
                        '<option selected> - Select a location - <\/option>';
      // =====================================
      var gmarkers = [];
      var htmls = [];
      var i = 0;
      var rozmiar = new google.maps.Size(12,20);  
                 var punkt_startowy = new google.maps.Point(0,0);  
                var punkt_zaczepienia = new google.maps.Point(0,0);  
                 var map;
      var n=0;
      var side_bar_html = "";


      var icon = new GIcon();
      icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
      icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      icon.iconSize = new GSize(12, 20);
      icon.shadowSize = new GSize(22, 20);
      icon.iconAnchor = new GPoint(6, 20);
      icon.infoWindowAnchor = new GPoint(5, 1);      


      var ikona1 = new GIcon(icon,"http://labs.google.com/ridefinder/images/mm_20_blue.png"); 

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,ikon) {
            var options = { 
draggable: true,
title: name,
icon: ikon,
clickable:true}
        var marker = new GMarker(point,options);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;

        // ======= Add the entry to the select box =====
        select_html += '<option> ' + name + '<\/option>';
        // ==========================================================

        i++;
        map.addOverlay(marker);
        return marker;
      }


      // ======= This function handles selections from the select box ====
      // === If the dummy entry is selected, the info window is closed ==
      function handleSelected(opt) {
        var i = opt.selectedIndex - 1; 
        if (i > -1) {
          GEvent.trigger(gmarkers[i],"click");
        }
        else {
          map.closeInfoWindow();
        }
      }

        var wspolrzedne = new google.maps.LatLng(51.730430542940184,19.62158203125);

      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(51.730430542940184,19.62158203125), 6);

    {/literal}
    {foreach from=$znacznik key=key item=item}
    {foreach from=$item key=k item=v}
    //here
            createMarker(new GLatLng(51.730430542940184,19.62158203125),'{if $k == 'nazwa_obiektu'}{$v}{/if}','<div class="dymek"><div style="border-bottom:2px solid red;margin-bottom:6px;text-align:left;"><a href="http://nocleg-i.pl/{if $k == 'id'}{$v}{/if}/google_maps/" style="color:#2A86C7;font-size:13px;font-family:Verdana;font-weight:bold;">{if $k == 'nazwa_obiektu'}{$v}{/if}</a></div></strong><div style="float:left;width:125px;"><strong><a href="http://nocleg-i.pl/{if $k == 'id'}{$v}{/if}/google_maps/"><img src="http://nocleg-i.pl/uzytkownicy/miroslawa/mini_0.jpg" alt="" border="0" /></a></div>Miasto: {if $k == 'miasto'}{$v}{/if}</div></div><div style="clear:both;"></div>',ikona1);
    //here
    {/foreach}
    {/foreach}
    {literal}
            }else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

    // This Javascript is based on code provided by the
    // Community Church Javascript Team
    // http://www.bisphamchurch.org.uk/   
    // http://econym.org.uk/gmap/

    //]]>
    </script>   
    {/literal}

标签之间(//这里)这些代码是问题

createMarker(new GLatLng(51.730430542940184,19.62158203125),'{if $k == 'nazwa_obiektu'}{$v}{/if}','<div class="dymek"><div style="border-bottom:2px solid red;margin-bottom:6px;text-align:left;"><a href="http://nocleg-i.pl/{if $k == 'id'}{$v}{/if}/google_maps/" style="color:#2A86C7;font-size:13px;font-family:Verdana;font-weight:bold;">{if $k == 'nazwa_obiektu'}{$v}{/if}</a></div></strong><div style="float:left;width:125px;"><strong><a href="http://nocleg-i.pl/{if $k == 'id'}{$v}{/if}/google_maps/"><img src="http://nocleg-i.pl/uzytkownicy/miroslawa/mini_0.jpg" alt="" border="0" /></a></div>Miasto: {if $k == 'miasto'}{$v}{/if}</div></div><div style="clear:both;"></div>',ikona1);

它没有显示 ex 的变量。{if $k == 'nazwa_obiektu'}{$v}{/if} 什么也没显示,当我在其他地方执行 foreach 函数时,它会显示变量,但是如果我手动输入它会显示它有人知道如何解决它吗?

4

3 回答 3

1

检查撇号的结构。试试这个或查看{assign}以预先设置 javascript 的值,并避免撇号混淆

createMarker(
    new GLatLng(51.730430542940184,19.62158203125),
    '{if $k == "nazwa_obiektu"}{$v}{/if}',
    '<div class="dymek"><div style="border-bottom:2px solid red;margin-bottom:6px;text-align:left;"><a href=\"http://nocleg-i.pl/{if $k == "id"}{$v}{/if}/google_maps/\" style=\"color:#2A86C7;font-size:13px;font-family:Verdana;font-weight:bold;\">{if $k == "nazwa_obiektu"}{$v}{/if}</a></div></strong><div style=\"float:left;width:125px;\"><strong><a href=\"http://nocleg-i.pl/{if $k == "id"}{$v}{/if}/google_maps/\"><img src=\"http://nocleg-i.pl/uzytkownicy/miroslawa/mini_0.jpg\" alt=\"\" border=\"0\" /></a></div>Miasto: {if $k == "miasto"}{$v}{/if}</div></div><div style=\"clear:both;\"></div>',
    ikona1
);

或与 {assign}

{if $k == "nazwa_obiektu"}
    {assign var="nazwa_obiektu" value=$v}
{/if}

{if $k == "id"}
    {assign var="id" value=$v}
{/if}

{if $k == "miasto"}
    {assign var="miasto" value=$v}
{/if}

    createMarker(
        new GLatLng(51.730430542940184,19.62158203125),
        '{$nazwa_obiektu|default}',
        '<div class="dymek"><div style="border-bottom:2px solid red;margin-bottom:6px;text-align:left;"><a href=\"http://nocleg-i.pl/{$id|default}/google_maps/\" style=\"color:#2A86C7;font-size:13px;font-family:Verdana;font-weight:bold;\">{$nazwa_obiektu|default}</a></div></strong><div style=\"float:left;width:125px;\"><strong><a href=\"http://nocleg-i.pl/{$id|default}/google_maps/\"><img src=\"http://nocleg-i.pl/uzytkownicy/miroslawa/mini_0.jpg\" alt=\"\" border=\"0\" /></a></div>Miasto: {$miasto|default}</div></div><div style=\"clear:both;\"></div>',
        ikona1
    );
于 2012-07-27T14:19:16.713 回答
0

你的问题是引号:

  "{if $k == 'nazwa_obiektu'}{$v}{/if}"
//^----------use double quotes--------^
于 2012-07-27T14:15:31.530 回答
0

您需要转义引号,或使用不同的引号:

'{if $k == 'nazwa_obiektu'...}'

应该

'{if $k == \'nazwa_obiektu\'...}'

或者

'{if $k == "nazwa_obiektu"...}'
于 2012-07-27T14:16:23.400 回答