在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来说是很实用的,本文讲述在Arcgis for Js中,用两种不同的方式来实现该效果。

为了有个直观的概念,先给大家看看实现后的效果:

百度地图的效果

效果1

效果2

直观的看到了效果,下面说说在Arcgis for Js中实现的两种方式。在实现给效果的时候,有layer的两个事件,mouse-over和mouse-out事件,鼠标经过显示对象名称,鼠标移除清除显示。

1、通过TextSymbol和GraphicMarkerSymbol实现

通过这种方式显示是直接用Arcgis的方式实现的,实现的代码如下,效果为效果2:

  1. function mouseOverLayer(e){
  2. map.setMapCursor("pointer");
  3. console.log(e.graphic);
  4. var font  = new esri.symbol.Font();
  5. font.setSize("10pt");
  6. font.setFamily("微软雅黑");
  7. var cpoint = event.graphic.geometry;
  8. var text = new esri.symbol.TextSymbol(event.graphic.attributes.name);
  9. text.setFont(font);
  10. text.setColor(new dojo.Color([0,0,0,100]));
  11. text.setOffset(20,-35);
  12. pmsTextBg.setOffset(20,-30);
  13. var textLength=event.graphic.attributes.name.length;
  14. pmsTextBg.setWidth(textLength*13.5+5);
  15. var bgGraphic = new esri.Graphic(cpoint, pmsTextBg);
  16. showTextLayer.add(bgGraphic);
  17. var labelGraphic = new esri.Graphic(cpoint,text);
  18. showTextLayer.add(labelGraphic);
  19. };
  20. function mouseOutLayer(){
  21. map.graphics.clear();
  22. showTextLayer.clear();
  23. map.setMapCursor("default");
  24. }
function mouseOverLayer(e){
map.setMapCursor("pointer");
console.log(e.graphic);
var font = new esri.symbol.Font();
font.setSize("10pt");
font.setFamily("微软雅黑");
var cpoint = event.graphic.geometry;
var text = new esri.symbol.TextSymbol(event.graphic.attributes.name);
text.setFont(font);
text.setColor(new dojo.Color([0,0,0,100]));
text.setOffset(20,-35); pmsTextBg.setOffset(20,-30);
var textLength=event.graphic.attributes.name.length;
pmsTextBg.setWidth(textLength*13.5+5);
var bgGraphic = new esri.Graphic(cpoint, pmsTextBg);
showTextLayer.add(bgGraphic);
var labelGraphic = new esri.Graphic(cpoint,text);
showTextLayer.add(labelGraphic); };
function mouseOutLayer(){
map.graphics.clear();
showTextLayer.clear();
map.setMapCursor("default");
}

2、直接用div显示

通过获取鼠标点位置或者几何体位置,将位置转换为屏幕坐标,将信息用div的形式展示出来,代码如下,效果为效果1:

  1. function mouseOverLayer(e){
  2. map.setMapCursor("pointer");
  3. console.log(e.graphic.attributes);
  4. var scrPt = map.toScreen(e.graphic.geometry);
  5. console.log(scrPt);
  6. var textDiv = dojo.doc.createElement("div");
  7. dojo.attr(textDiv,{
  8. "id":"text"
  9. });
  10. dojo.style(textDiv, {
  11. "left": scrPt.x+10 + "px",
  12. "top": scrPt.y+10 + "px",
  13. "position": "absolute",
  14. "z-index":99,
  15. "background":"#fcffd1",
  16. "font-size":"10px",
  17. "border":"1px solid #0096ff",
  18. "padding": "0.1em 0.3em 0.1em",
  19. "font-size": "11px",
  20. "border-radius": "3px",
  21. "box-shadow": "0 0 0.75em #777777"
  22. });
  23. textDiv.innerHTML =e.graphic.attributes.name;
  24. dojo.byId("map").appendChild(textDiv);
  25. };
  26. function mouseOutLayer(e){
  27. map.setMapCursor("default");
  28. dojo.byId("map").removeChild(dojo.byId("text"));
  29. };
            function mouseOverLayer(e){
map.setMapCursor("pointer");
console.log(e.graphic.attributes);
var scrPt = map.toScreen(e.graphic.geometry);
console.log(scrPt);
var textDiv = dojo.doc.createElement("div");
dojo.attr(textDiv,{
"id":"text"
});
dojo.style(textDiv, {
"left": scrPt.x+10 + "px",
"top": scrPt.y+10 + "px",
"position": "absolute",
"z-index":99,
"background":"#fcffd1",
"font-size":"10px",
"border":"1px solid #0096ff",
"padding": "0.1em 0.3em 0.1em",
"font-size": "11px",
"border-radius": "3px",
"box-shadow": "0 0 0.75em #777777"
});
textDiv.innerHTML =e.graphic.attributes.name;
dojo.byId("map").appendChild(textDiv);
};
function mouseOutLayer(e){
map.setMapCursor("default");
dojo.byId("map").removeChild(dojo.byId("text"));
};

比较:

以上两种方式都可实现相同的效果,但就实现的难易程度,第二种比第一种简单,在实现的美观程度上,第二种比第一种更好调整与控制,在实现效率上,第二种比第一种好一点,可是,就在与地图的结合上,很显然,第二种比第一种稍微差一点。

原文地址:http://blog.csdn.net/gisshixisheng/article/details/41889345

声明:个人学习,不做非法盈利

Arcgis for Js之鼠标经过显示对象名的实现的更多相关文章

  1. (转)Arcgis for Js之鼠标经过显示对象名的实现

    http://blog.csdn.net/gisshixisheng/article/details/41889345 在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来 ...

  2. CSS或者JS实现鼠标悬停显示另一元素

    想达到鼠标悬停到元素a上,显示另一个元素b,可以通过css实现也可以通过js实现.js:写两个函数:mouseenter,mouseleave,例如:其中 $("#a").mous ...

  3. Arcgis for JS扩展GraphicLayer实现区域对象的聚类统计与展示

    功能需求: 分省市统计并展示全国雨量站的数目与位置. 常规做法: 分省市雨量站的数目通过统计表的形式在页面端展示,位置根据XY坐标信息将雨量站标绘在图上. 优化做法: 去掉统计图的展示方式,直接将各省 ...

  4. JS实现鼠标悬浮,显示内容

    其实就是增加title属性

  5. 鼠标经过显示二级菜单的js特效

    本文章来给大家推荐一个不错的鼠标经过显示二级菜单js特效效果,有需要了解的朋友可以参考一下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  6. (转载)arcgis for js - 解决加载天地图和WMTS服务,WMTS服务不显示的问题,以及wmts服务密钥。

    1 arcgis加载天地图和wmts服务 arcgis for js加载天地图的例子网上有很多,这里先不写了,后期有空再贴代码,这里主要分析下WMTS服务为什么不显示,怎么解决. 条件:这里的WMTS ...

  7. (转)Arcgis for JS之对象捕捉

    http://blog.csdn.net/gisshixisheng/article/details/44098615 在web操作,如绘制或者测量的时候,为了精确,需要捕捉到某一图层的对象,在此,讲 ...

  8. js(jquery)鼠标移入移出事件时,出现闪烁、隐藏显示隐藏显示不停切换的情况

    <script> $(".guanzhu").hover(function(){ $(".weixinTop").show(); },functio ...

  9. arcgis for js学习之Draw类

    arcgis for js学习之Draw类 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...

随机推荐

  1. 04->python字典

    字典dict 1.数据类型分类(按可变和不可变)     不可变数据类型:数字.bool.str.tuple     可变数据类型:list.dict.set 2.dict的组成     key:va ...

  2. 【笔记】Django的ORM之多对多表的增和删

    [笔记]Django的ORM之多对多表的增和删 Django ORM 多对多  一 昨日补充:外键关联 外键在ORM中的关联方式: 与数据表相关的类都放到models.py文件中 class Book ...

  3. DropDownList年份的添加

    http://blog.sina.com.cn/s/blog_4b9e030e01007sc3.html

  4. JS函数调用分析过程

  5. java——调用一个静态方法的时候有没有执行这个类的构造方法,以及这个类中的静态常量?

    尝试一下: public class Try { final static int a = 1; public Try() { System.out.print("构造方法"); ...

  6. firewall 端口转发

    centos 7 使用背景:某次新购阿里云服务器安装nginx后配置80转8080的内部转发 systemctl status firewalld ---查看守护进程状态systemctl start ...

  7. python 使用csv.reader和csv.writer读写文件并转换成dataframe格式

    import csv import pandas as pd ###csv.reader用法 ''' f=open(r"C:\Users\admin\pycdtest\wanyue\yuee ...

  8. nexus 私服相关的配置

    1 上传到nexus的配置 1 settings.xml <server> <id>releases</id> <username>admin</ ...

  9. ruby逻辑判断符号

    puts true and false  #相当于 (puts true) and false Use &&/|| for boolean expressions, and/or fo ...

  10. pat1019. General Palindromic Number (20)

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...