PHP+Mysql+jQuery实现地图区域数据统计-展示数据_Helloweba

我们要在地图上有限的区块内展示更多的信息,更好的办法是通过地图交互来实现。本文将给大家讲解通过鼠标滑动到地图指定省份区域,在弹出的提示框中显示对应省份的数据信息。适用于数据统计和地图区块展示等场景。

查看演示 下载源码

本文紧接本站上一篇文章:PHP+Mysql+jQuery实现地图区域数据统计-载入数据,在原文实例基础上新加提示框展示数据功能,如果您对地图绘制和数据载入不太了解,建议先阅读本站上篇文章的介绍。

HTML

首先在head部分载入raphael.js库文件和chinamapPath.js路径信息文件,本文不重复写下,唯一不同的地方是需要在body中加一个div#tip,用来展示地图信息的提示框。

<code class="html"><span class="html__tag_start">&lt;div</span>&nbsp;<span class="html__attr_name">id</span>=<span class="html__attr_value">"map"</span><span class="html__tag_start">&gt;</span><span class="html__tag_end">&lt;/div&gt;</span>&nbsp;<br><span class="html__tag_start">&lt;div</span>&nbsp;<span class="html__attr_name">id</span>=<span class="html__attr_value">"tip"</span><span class="html__tag_start">&gt;</span><span class="html__tag_end">&lt;/div&gt;</span>&nbsp;<br></code>

jQuery

通过调用raphael绘制出中国地图,然后载入统计数据,由于地图区块小,我们不在地图载入的时候就将数据显示在地图区块上了,我们通过鼠标交互实现将数据信息更好的展示给用户。当鼠标滑向省份区块时,通过e.clientX和e.clientY定位鼠标坐标,然后通过jquery的css()方法定位提示框div#tip,并且将对应省份的的名称和活跃用户数加到提示框里并展现出来,请看代码:

<code class="js">$(<span class="js__operator">function</span>()<span class="js__brace">{</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;$.get(<span class="js__string">"json.php"</span>,<span class="js__operator">function</span>(json)<span class="js__brace">{</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;......<span class="js__sl_comment">//这里省略代码若干</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__statement">var</span>&nbsp;i=<span class="js__num">0</span>;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__statement">for</span>&nbsp;(<span class="js__statement">var</span>&nbsp;state&nbsp;<span class="js__operator">in</span>&nbsp;china)&nbsp;<span class="js__brace">{</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;china[state][<span class="js__string">'path'</span>].color&nbsp;=&nbsp;Raphael.getColor(<span class="js__num">0.9</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="js__operator">function</span>&nbsp;(st,&nbsp;state)&nbsp;<span class="js__brace">{</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__statement">var</span>&nbsp;prodata&nbsp;=&nbsp;data[i];&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__statement">var</span>&nbsp;fillcolor&nbsp;=&nbsp;colors[arr[i]];&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.attr(<span class="js__brace">{</span>fill:fillcolor<span class="js__brace">}</span>);<span class="js__sl_comment">//填充背景色</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xOffset&nbsp;=&nbsp;<span class="js__num">70</span>;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yOffset&nbsp;=&nbsp;<span class="js__num">180</span>;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.hover(<span class="js__operator">function</span>(e)<span class="js__brace">{</span><span class="js__sl_comment">//鼠标滑向</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.animate(<span class="js__brace">{</span>fill:&nbsp;<span class="js__string">"#fdd"</span>,&nbsp;stroke:&nbsp;<span class="js__string">"#eee"</span><span class="js__brace">}</span>,&nbsp;<span class="js__num">500</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R.safari();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(<span class="js__string">"#tip"</span>).css(<span class="js__brace">{</span><span class="js__string">"top"</span>:(e.clientY-xOffset)+<span class="js__string">"px"</span>,<span class="js__string">"left"</span>:(e.clientX-yOffset)+<span class="js__string">"px"</span><span class="js__brace">}</span>).fadeIn(<span class="js__string">"fast"</span>)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.html(<span class="js__string">"&lt;h4&gt;"</span>+china[state][<span class="js__string">'name'</span>]+<span class="js__string">"&lt;/h4&gt;&lt;p&gt;活跃用户数:"</span>+prodata+<span class="js__string">"&lt;/p&gt;"</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>,<span class="js__operator">function</span>()<span class="js__brace">{</span><span class="js__sl_comment">//鼠标离开</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.animate(<span class="js__brace">{</span>fill:&nbsp;fillcolor,&nbsp;stroke:&nbsp;<span class="js__string">"#eee"</span><span class="js__brace">}</span>,&nbsp;<span class="js__num">500</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R.safari();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(<span class="js__string">"#tip"</span>).hide();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st.mousemove(<span class="js__operator">function</span>(e)<span class="js__brace">{</span><span class="js__sl_comment">//鼠标移动</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(<span class="js__string">"#tip"</span>).css(<span class="js__brace">{</span><span class="js__string">"top"</span>:(e.clientY-xOffset)+<span class="js__string">"px"</span>,<span class="js__string">"left"</span>:(e.clientX-yOffset)+<span class="js__string">"px"</span><span class="js__brace">}</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R.safari();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>)(china[state][<span class="js__string">'path'</span>],&nbsp;state);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="js__brace">}</span>);&nbsp;<br><span class="js__brace">}</span>);&nbsp;<br></code>

以上代码可以看出,通过jQuery的hover()鼠标滑向省份区块时,调用弹出提示框,并将数据载入显示在提示框中,而值得关注的是,我们还需要加一个效果,就是鼠标在省份区块上移动mousemove()的时候,也应该调用提示框跟随鼠标一起移动,否则的话当鼠标在一个省份区块内滑动的话提示框位置不会变化,这样会影响体验效果,小小的改动可以提升用户体验。

最后,如果您需要定制提示框的效果的话,可以设置提示框的CSS样式,本例简单的CSS代码如下:

<code class="css"><span class="css__id">#tip</span>{<span class="css__property">position:</span><span class="css__value">absolute</span>;&nbsp;<span class="css__property">width:</span><span class="css__number">180px</span>;&nbsp;<span class="css__property">border:</span><span class="css__number">1px</span>&nbsp;<span class="css__value">solid</span>&nbsp;<span class="css__color">#d3d3d3</span>;&nbsp;<span class="css__property">background:</span><span class="css__color">#fff</span>;<span class="css__property">display:</span><span class="css__value">none</span>;&nbsp;<br>-moz-border-radius:<span class="css__number">5px</span>;&nbsp;-webkit-border-radius:<span class="css__number">5px</span>;&nbsp;<span class="css__property">overflow:</span><span class="css__value">hidden</span>;&nbsp;border-radius:<span class="css__number">5px</span>;&nbsp;<br>-moz-box-shadow:<span class="css__number">1px</span>&nbsp;<span class="css__number">1px</span>&nbsp;<span class="css__number">2px</span>&nbsp;rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>);&nbsp;-webkit-box-shadow:<span class="css__number">1px</span>&nbsp;<span class="css__number">1px</span>&nbsp;<span class="css__number">2px</span>&nbsp;rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>);&nbsp;&nbsp;<br>box-shadow:<span class="css__number">1px</span>&nbsp;<span class="css__number">1px</span>&nbsp;<span class="css__number">2px</span>&nbsp;rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>);}&nbsp;<br><span class="css__id">#tip</span>&nbsp;<span class="css__element">h4</span>{<span class="css__property">height:</span><span class="css__number">28px</span>;&nbsp;<span class="css__property">line-height:</span><span class="css__number">28px</span>;&nbsp;<span class="css__property">padding-left:</span><span class="css__number">6px</span>;&nbsp;<span class="css__property">background:</span><span class="css__color">#f0f0f0</span>}&nbsp;<br><span class="css__id">#tip</span>&nbsp;<span class="css__element">p</span>{<span class="css__property">line-height:</span><span class="css__number">24px</span>;&nbsp;<span class="css__property">padding:</span><span class="css__number">2px</span>&nbsp;<span class="css__number">4px</span>}&nbsp;</code>

来源URL:http://www.helloweba.com/view-blog-244.html