javascriptphpjqueryimagemapster

How to create PHP generated tooltips for ImageMapster?


I'm working on a floor map for my company, to see where each employee has its desk, so if you wish to visit someone, you can easily find him beforehand.

I have created a <map> with a lot of <area>s and now I'm using ImageMapster to highligth a table and to display some information about an employee (photo, name, position etc. (small business card)) in a tooltip.

And because it is really not optimal to manually change areas in mapster initialization, I want to load captions of tooltips by PHP.

I've made this so far:

<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>

So the outup <area>'s looks like this

<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">

But tooltips are not displaying, and in console there is no error. In firebug the <script> looks like this:

$('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"}]]
}); 

I'm hopelessly stuck on this, hope someone has an idea how to make this work.


Solution

  • In your JavaScript areas should only have single brackets areas:[...] other than two nested ones areas:[[...]]. According to the documentation here. So we just need to get rid of those extra brackets:

    $('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"}]
    }); 
    

    We can do this by removing them in the JavaScript here:

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

    To

    areas: <?php echo $found ?>
    

    Since $found is an array, it has the brackets needed.