如何为ImageMapster创建PHP生成的工具提示?

时间:2014-09-26 13:54:50

标签: javascript php jquery imagemapster

我正在为我的公司制作地板地图,看看每位员工的办公桌位置,所以如果你想拜访某人,你可以事先找到他。

我创建了一个<map>,其中包含很多<area>个,现在我正在使用ImageMapster来突出显示一个表并显示有关员工的一些信息(照片,名称,位置等(小名片))在工具提示中。

因为在mapster初始化中手动更改areas真的不是最佳的,所以我想用PHP加载工具提示的标题。

到目前为止我已经做到了这一点:

<div class="mapster-map">
    <img src="images/floor_2.png" border="0" width="1300" height="1300" usemap="map_floor_2" alt="" />
    <map name="map_floor_2" id="ImageMap-map_floor_2">
        <?php
            $found = array();
            foreach ($tables as $t) {
                $user = $map->getSeatedEmployee($t['id']);
                if (!empty($user)) {
                    $found[] = array('key'=>$t['id'], 'toolTip'=>$user['jmeno'] . ' ' . $user['prijmeni']);
                }
                echo '<area id="' . $t['id'] . '" coords="' . $t['coords'] . '" shape="' . $t['shape'] . '" alt=""
                title="' . (!empty($user) ? $user['name'] . ' ' . $user['surname'] : '') . '" href="' . (!empty($user) ? 'user_detail.php?id=' . $user['id'] : '#') . '" />';
            }
            $found = json_encode($found);
        ?>
    </map>
</div>
<script>
    $('img[usemap]').mapster({
        mapKey: 'id',
        fillColor: 'EE1C23',
        fillOpacity: 0.65,
        showToolTip: true,
        areas:[<?php echo $found ?>]
    });
</script>

所以outup <area>看起来像这样

<area id="2-13-2" href="user_detail.php?id=1" title="Adam Jones" alt="" shape="rect" coords="274,269,356,337">
<area id="2-13-4" href="user_detail.php?id=2" title="Bon Jovi" alt="" shape="rect" coords="189,269,271,337">
<area id="2-13-6" href="user_detail.php?id=3" title="Charles Copperfield" alt="" shape="rect" coords="104,269,186,337">
<area id="2-13-8" href="#" title="" alt="" shape="rect" coords="013,269,081,353">
<area id="2-13-1" href="user_detail.php?id=4" title="Christina Davis" alt="" shape="rect" coords="274,390,356,458">

但是工具提示没有显示,在控制台中没有错误。在萤火虫中,<script>看起来像这样:

$('img[usemap]').mapster({
    mapKey: 'id',
    fillColor: 'EE1C23',
    fillOpacity: 0.65,
    showToolTip: true,
    areas:[[{"key":"2-13-2","toolTip":"Adam Jones"},{"key":"2-13-1","toolTip":"Bon Jovi"},{"key":"2-13-1","toolTip":"Charles Copperfield"},{"key":"2-13-1","toolTip":"Christina Davis"}]]
}); 

我绝望地坚持这一点,希望有人知道如何使这项工作。

1 个答案:

答案 0 :(得分:2)

在您的JavaScript areas中,除了两个嵌套的areas:[...]之外,我应该只有一个括号areas:[[...]]。根据文件here。所以我们只需要摆脱那些额外的括号:

$('img[usemap]').mapster({
    ... ,
    areas:[{"key":"2-13-2","toolTip":"Adam Jones"},{"key":"2-13-1","toolTip":"Bon Jovi"},{"key":"2-13-1","toolTip":"Charles Copperfield"},{"key":"2-13-1","toolTip":"Christina Davis"}]
}); 

我们可以通过在JavaScript中删除它们来实现这一目的:

areas:[<?php echo $found ?>]

areas: <?php echo $found ?>

由于$found是一个数组,因此需要括号。