google map使用自定义Marker在地图上添加文字标示

google map默认的标示GMarker,只能使用图片不能使用文字。但是在实际中,我们不可避免的需要在地图上标示文字信息。例如地名等。Google 地图 API 使我们可以通过扩展GMarker实现自定义的GMarker的子类LabelMarker。

 

 1  google.maps.LabelMarker = function (latlng, options)
 2
  {
 3
      this.latlng =  latlng;
 4
      this.labelText = options.labelText || '' ;
 5
      this.labelClass = options.labelClass || 'writeb' ;
 6
      this.labelOffset = options.labelOffset || new google.maps.Size(8-33 );
 7
      options.icon = options.icon ||  getTextIcon();
 8
      google.maps.Marker.apply(this , arguments);
 9
  }
10
 
11  google.maps.LabelMarker.prototype = new google.maps.Marker(new google.maps.LatLng(00 ));
12
 
13  google.maps.LabelMarker.prototype.initialize = function (map){
14
      google.maps.Marker.prototype.initialize.call(this , map);
15
      
16
      var label = document.createElement('div' );
17
      label.className = this .labelClass;
18
      label.innerHTML = this .labelText;
19
      label.style.position = 'absolute' ;
20
      label.style.width = '48px' ;
21
      map.getPane(G_MAP_MARKER_PANE).appendChild(label);
22
      
23
      this.map =  map;
24
      this.label =  label;
25
  }
26
 
27  google.maps.LabelMarker.prototype.redraw = function (force){
28
      google.maps.Marker.prototype.redraw.call(this , map);
29
      
30
      if(! force)
31
      {
32
          return ;
33
      }
34
      
35
      var point = this.map.fromLatLngToDivPixel(this .latlng);
36
      var z = google.maps.Overlay.getZIndex(this .latlng.lat());
37
      
38
      this.label.style.left = (point.x + this.labelOffset.width) + 'px' ;
39
      this.label.style.top = (point.y + this.labelOffset.height) + 'px' ;
40
      this.label.style.zIndex = z + 1 ;
41
  }
42
 
43  google.maps.LabelMarker.prototype.remove = function (){
44
      this.label.parentNode.removeChild(this .label);
45
      this.label = null ;
46
      google.maps.Marker.prototype.remove.call(this );
47
  }
48
 
49  function  getTextIcon()
50
  {
51
      var icon = new  google.maps.Icon();
52
      icon.image = "/js/map/img/mapts.gif" ;
53
      icon.iconSize = new GSize(4840 );
54
      icon.iconAnchor = new GPoint(040 );
55
      icon.infoWindowAnchor = new GPoint(51 );
56
      return  icon;
57
  }

 在页面上调用的代码:

 

1  var marker = new  google.maps.LabelMarker(map.getCenter(), { 
2
      labelText:'我在这'
3      });
4
          
5
  map.addOverlay(marker);

 

现在就会在地图上显示我们自定义的GMarker标识了。

 

http://www.cnblogs.com/hyl8218/archive/2009/12/26/1632524.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值