谷歌地图在地图上添加文字标注

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  5.     <style type="text/css">  
  6.       html { height: 100% }  
  7.       body { height: 100%; margin: 0; padding: 0 }  
  8.       #map_canvas { height: 100% }  
  9.     </style>  
  10.     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>  
  11.     <script type="text/javascript">  
  12.       function initialize() {  
  13.         var mapOptions = {  
  14.           center: new google.maps.LatLng(31.397, 117.644),  
  15.           zoom: 10,  
  16.           mapTypeId: google.maps.MapTypeId.ROADMAP  
  17.         };  
  18.         var map = new google.maps.Map(document.getElementById("map_canvas"),  
  19.             mapOptions);  
  20.           
  21.         var overlay = new MyMarker(map,{latlng:new google.maps.LatLng(31.397, 117.644),clickFun:zoomOut});   
  22.   
  23.   
  24.       }  
  25.   
  26.   
  27.   
  28.   
  29.   
  30.   
  31. function zoomOut(){  
  32.     alert(3333);  
  33. }  
  34.   
  35.   
  36.   
  37.   
  38.       /***************自定义叠加层,可作为站点显示在地图上******************/  
  39.     function MyMarker(map, options) {     
  40.       // Now initialize all properties.     
  41.       this.latlng = options.latlng; //设置图标的位置  
  42.       this.image_ = options.image;  //设置图标的图片  
  43.       this.labelText = options.labelText || 'dd';  
  44.       this.labelClass = options.labelClass || 'shadow';//设置文字的样式  
  45.       this.clickFun = options.clickFun ;//注册点击事件  
  46.   //    this.labelOffset = options.labelOffset || new google.maps.Size(8, -33);  
  47.       this.map_ = map;      
  48.   
  49.   
  50.       this.div_ = null;     
  51.       // Explicitly call setMap() on this overlay     
  52.       this.setMap(map);   
  53.     }   
  54.     MyMarker.prototype = new google.maps.OverlayView();  
  55.     //初始化图标  
  56.     MyMarker.prototype.onAdd = function() {      
  57.         // Note: an overlay's receipt of onAdd() indicates that    
  58.         // the map's panes are now available for attaching     
  59.         // the overlay to the map via the DOM.      
  60.         // Create the DIV and set some basic attributes.    
  61.         var div = document.createElement('DIV'); //创建存放图片和文字的div  
  62.         div.style.border = "none";     
  63.         div.style.borderWidth = "0px";     
  64.         div.style.position = "absolute";  
  65.         div.style.cursor = "hand";  
  66.         div.onclick = this.clickFun ||function(){};//注册click事件,没有定义就为空函数  
  67.         // Create an IMG element and attach it to the DIV.    
  68.         var img = document.createElement("img"); //创建图片元素  
  69.         img.src = this.image_;    
  70.         img.style.width = "100%";     
  71.         img.style.height = "100%";    
  72.         //初始化文字标签  
  73.         var label = document.createElement('div');//创建文字标签  
  74.             label.className = this.labelClass;  
  75.             label.innerHTML = '<a οnclick="zoomOut()" href="#">'+this.labelText+'</a>';  
  76.             label.style.position = 'absolute';  
  77.             label.style.width = '200px';  
  78.         //  label.style.fontWeight = "bold";  
  79.             label.style.textAlign = 'left';  
  80.             label.style.padding = "2px";  
  81.             label.style.fontSize = "10px";  
  82.         //  label.style.fontFamily = "Courier New";  
  83.   
  84.   
  85.         div.appendChild(img);     
  86.         div.appendChild(label);     
  87.           
  88.         this.div_ = div;     
  89.         // We add an overlay to a map via one of the map's panes.    
  90.         // We'll add this overlay to the overlayImage pane.    
  91.         var panes = this.getPanes();    
  92.         panes.overlayMouseTarget.appendChild(div);   
  93.       }  
  94.       //绘制图标,主要用于控制图标的位置  
  95.     MyMarker.prototype.draw = function() {      
  96.           // Size and position the overlay. We use a southwest and northeast     
  97.           // position of the overlay to peg it to the correct position and size.    
  98.           // We need to retrieve the projection from this overlay to do this.    
  99.           var overlayProjection = this.getProjection();     
  100.           // Retrieve the southwest and northeast coordinates of this overlay    
  101.           // in latlngs and convert them to pixels coordinates.    
  102.           // We'll use these coordinates to resize the DIV.    
  103.           var position = overlayProjection.fromLatLngToDivPixel(this.latlng);   //将地理坐标转换成屏幕坐标  
  104.         //  var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());      
  105.           // Resize the image's DIV to fit the indicated dimensions.     
  106.           var div = this.div_;    
  107.           div.style.left =position.x-5 + 'px';    
  108.           div.style.top  =position.y-5 + 'px';    
  109.         //控制图标的大小  
  110.           div.style.width = '10px';    
  111.           div.style.height ='10px';  
  112.       }  
  113.     MyMarker.prototype.onRemove = function() {     
  114.           this.div_.parentNode.removeChild(this.div_);     
  115.           this.div_ = null;   
  116.       }  
  117.   
  118.   
  119.       //Note that the visibility property must be a string enclosed in quotes   
  120.     MyMarker.prototype.hide = function() {     
  121.           if (this.div_) {       
  122.             this.div_.style.visibility = "hidden";     
  123.           }   
  124.       }    
  125.     MyMarker.prototype.show = function() {     
  126.           if (this.div_) {       
  127.           this.div_.style.visibility = "visible";     
  128.           }   
  129.       }   
  130.       //显示或隐藏图标  
  131.     MyMarker.prototype.toggle = function() {     
  132.         if (this.div_) {       
  133.             if (this.div_.style.visibility == "hidden") {         
  134.             this.show();       
  135.             } else {         
  136.             this.hide();       
  137.             }     
  138.         }  
  139.       }    
  140.     </script>  
  141.   </head>  
  142.   <body onload="initialize()">  
  143.     <div id="map_canvas" style="width:100%; height:100%"></div>  
  144.   </body>  
  145. </html> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值