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


How to create PHP generated tooltips for ImageMapster?

我正在为我的公司绘制楼层地图,看看每个员工的办公桌在哪里,所以如果你想拜访某人,你可以很容易地提前找到他。

我已经创建了一个有很多<area><map>,现在我正在使用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>

所以外面的<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">

但工具提示并没有显示,并且在控制台中并没有错误。在firebug中,<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"}]]
}); 

我无可救药地被困在这个问题上,希望有人知道如何让它发挥作用。

在JavaScript中,areas应该只有单括号areas:[...],而不是两个嵌套的括号areas:[[...]]。根据这里的文件。所以我们只需要去掉那些额外的括号:

$('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是一个数组,因此它具有所需的括号。