[转载]如何利用【百度地图API】,制作房产酒店地图?(下) – 酸奶小妹 – 博客园.
摘要:
很多房产网、旅游酒店网上,都有一个列表,鼠标经过列表上的数据时,地图上就会打开相应的信息窗口。
如何实现这一功能呢?
快来学习吧。
———————————————————————————————–
一、制作列表
接着上一篇文章来讲,我们已经拥有了一张能显示自定义标注,和信息窗口的地图了。现在,我们来加上列表。
首先,需要写上一段htm,来展示列表。
<div style="float: left; width: 200px; height: 340px; padding: 0pt 10px 0pt 0pt; line-height: 1.8em;"> <ul> <li><span style="float: right;">120元</span><a href="#">如家快捷酒店</a></li> <li><span style="float: right;">2370元</span><a href="#">昆仑大厦</a></li> <li><span style="float: right;">50元</span><a href="#">华夏银行</a></li> <li><span style="float: right;">16元</span><a href="#">成都小吃</a></li> <li><span style="float: right;">300元</span><a href="#">锦绣大饭店</a></li> <li><span style="float: right;">180元</span><a href="#">七天快捷酒店</a></li> <li><span style="float: right;">9元</span><a href="#">中央民族大学</a></li> <li><span style="float: right;">3300元</span><a href="#">昌平汽车专修学院</a></li> <li><span style="float: right;">20元</span><a href="#">百度大厦</a></li> <li><span style="float: right;">1000元</span><a href="#">海尔电器销售点</a></li> </ul> </div>
对了,不要忘记给列表中的链接加上样式。
二、打开对应的信息窗口
因为这里没有用到对应的marker,不能用上一篇文章中的打开信息窗口的方法了。
我们需要在地图上打开信息窗口。
需要两个参数,第一是信息窗口的id,第二是信息窗口的坐标。如下:
function openMyWin(id,p){ map.openInfoWindow(id,p); }
最后,只需要对列表中的数据,加上打开对应信息窗口的js语句即可。
如下,当鼠标滑过时,打开信息一的窗口。
onmouSEOver=”openMyWin(infoWindow1,point[1])”
三、截图
四、源代码
为了方便大家学习,以下代码包含了上一章的全部代码,并做了改进。
全部源代码如下:
酷讯酒店地图 <script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script> <!-- a{text-decoration:none;color:#6ce;font-size:14px;} a:hover{text-decoration:underline;} --> <div style="float: left; width: 200px; height: 340px; padding: 0pt 10px 0pt 0pt; line-height: 1.8em;"> <ul> <li><span style="float: right;">120元</span><a onmouseover="openMyWin(infoWindow1,point[1])" href="#">如家快捷酒店</a></li> <li><span style="float: right;">2370元</span><a onmouseover="openMyWin(infoWindow2,point[2])" href="#">昆仑大厦</a></li> <li><span style="float: right;">50元</span><a onmouseover="openMyWin(infoWindow3,point[3])" href="#">华夏银行</a></li> <li><span style="float: right;">16元</span><a onmouseover="openMyWin(infoWindow4,point[4])" href="#">成都小吃</a></li> <li><span style="float: right;">300元</span><a onmouseover="openMyWin(infoWindow5,point[5])" href="#">锦绣大饭店</a></li> <li><span style="float: right;">180元</span><a onmouseover="openMyWin(infoWindow6,point[6])" href="#">七天快捷酒店</a></li> <li><span style="float: right;">9元</span><a onmouseover="openMyWin(infoWindow7,point[7])" href="#">中央民族大学</a></li> <li><span style="float: right;">3300元</span><a onmouseover="openMyWin(infoWindow8,point[8])" href="#">昌平汽车专修学院</a></li> <li><span style="float: right;">20元</span><a onmouseover="openMyWin(infoWindow9,point[9])" href="#">百度大厦</a></li> <li><span style="float: right;">1000元</span><a onmouseover="openMyWin(infoWindow0,point[0])" href="#">海尔电器销售点</a></li> </ul> </div> <script type="text/javascript">// <![CDATA[ var map = new BMap.Map("container"); var point = new BMap.Point(116.404, 39.915); map.centerAndZoom(point, 14); var myIcon = new BMap.Icon("http://dev.baidu.com/wiki/static/map/API/examples/images/Mario.png", new BMap.Size(32, 70), { //小车图片 imageOffset: new BMap.Size(0, 0) //图片的偏移量。为了是图片底部中心对准坐标点。 }); var point = [new BMap.Point(116.411776,39.942833),new BMap.Point(116.320791,40.003682),new BMap.Point(116.275186,39.896095),new BMap.Point(116.425098,39.946249),new BMap.Point(116.359823,39.984761),new BMap.Point(116.316479,39.98323),new BMap.Point(116.385986,39.946124),new BMap.Point(116.427545,40.00796),new BMap.Point(116.446965,39.911603),new BMap.Point(116.454579,39.946652)]; //10个坐标点 var marker1 = new BMap.Marker(point[1],{icon:myIcon}); // 创建10个标注 var marker2 = new BMap.Marker(point[2],{icon:myIcon}); var marker3 = new BMap.Marker(point[3],{icon:myIcon}); var marker4 = new BMap.Marker(point[4],{icon:myIcon}); var marker5 = new BMap.Marker(point[5],{icon:myIcon}); var marker6 = new BMap.Marker(point[6],{icon:myIcon}); var marker7 = new BMap.Marker(point[7],{icon:myIcon}); var marker8 = new BMap.Marker(point[8],{icon:myIcon}); var marker9 = new BMap.Marker(point[9],{icon:myIcon}); var marker0 = new BMap.Marker(point[0],{icon:myIcon}); map.addOverlay(marker1); // 将标注添加到地图中 map.addOverlay(marker2); map.addOverlay(marker3); map.addOverlay(marker4); map.addOverlay(marker5); map.addOverlay(marker6); map.addOverlay(marker7); map.addOverlay(marker8); map.addOverlay(marker9); map.addOverlay(marker0); map.setViewport(point); //调整地图的最佳视野为显示标注数组point var opts1 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">如家快捷酒店</span>'}; var opts2 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">昆仑大厦</span>'}; var opts3 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">华夏银行</span>'}; var opts4 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">成都小吃</span>'}; var opts5 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">锦绣大饭店</span>'}; var opts6 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">七天快捷酒店</span>'}; var opts7 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">中央民族大学</span>'}; var opts8 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">昌平汽车专修学院</span>'}; var opts9 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">百度大厦</span>'}; var opts0 = {offset : new BMap.Size(0, -25), title : '<span style="font-size:14px;color:#0A8021">海尔电器销售点</span>'}; var infoWindow1 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市朝阳区高碑店小学旁</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts1); // 创建信息窗口对象,引号里可以书写任意的html语句。 var infoWindow2 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市海淀区紫竹院123号</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts2); var infoWindow3 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市海淀区紫竹院123号</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts3); var infoWindow4 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市海淀区紫竹院123号</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts4); var infoWindow5 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市朝阳区高碑店小学旁</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts5); var infoWindow6 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市大钟寺沧澜大厦</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts6); var infoWindow7 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市朝阳区高碑店小学旁</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts7); var infoWindow8 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市哇哈哈路鲜鱼一条街</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts8); var infoWindow9 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市朝阳区高碑店小学旁</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts9); var infoWindow0 = new BMap.InfoWindow(" <div style='line-height:1.8em;font-size:12px;'><b>地址:</b>北京市朝阳区高碑店小学旁</br><b>电话:</b>010-59921010</br><b>口碑:</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' href='#'>详情>></a></div> ", opts0); marker1.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow1);}); marker2.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow2);}); marker3.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow3);}); marker4.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow4);}); marker5.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow5);}); marker6.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow6);}); marker7.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow7);}); marker8.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow8);}); marker9.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow9);}); marker0.addEventListener("mouseover", function(){this.openInfoWindow(infoWindow0);}); function openMyWin(id,p){ map.openInfoWindow(id,p); } // ]]></script>